Skip to content

Commit 0652a0b

Browse files
authored
Dart Debug Extension MV3 scaffold (#1732)
* Scaffold for the MV3 Dart Debug Extension * Clean up * More clean up
1 parent 3b31443 commit 0652a0b

File tree

11 files changed

+263
-0
lines changed

11 files changed

+263
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
compiled/
3+
extension_key.txt
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
targets:
2+
$default:
3+
builders:
4+
build_web_compilers|entrypoint:
5+
options:
6+
dart2js_args:
7+
- -O1 # Note: Change to -04 for minified JS.
8+
- --csp
9+
generate_for:
10+
- web/**.dart
11+
mv3_extension|client_js_copy_builder:
12+
enabled: true
13+
14+
builders:
15+
client_js_copy_builder:
16+
import: "tool/copy_builder.dart"
17+
builder_factories:
18+
- copyBuilder
19+
build_extensions:
20+
{
21+
"web/{{}}.dart.js": ["compiled/{{}}.dart.js"],
22+
"web/{{}}.png": ["compiled/{{}}.png"],
23+
"web/manifest.json": ["compiled/manifest.json"],
24+
}
25+
auto_apply: none
26+
build_to: source
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: mv3_extension
2+
publish_to: none
3+
version: 1.30.0
4+
homepage: https://github.com/dart-lang/webdev
5+
description: >-
6+
A Chrome extension for Dart debugging.
7+
8+
environment:
9+
sdk: '>=2.12.0 <3.0.0'
10+
11+
dependencies:
12+
js: ^0.6.1+1
13+
14+
dev_dependencies:
15+
build: ^2.0.0
16+
build_web_compilers: ^3.0.0
17+
build_runner: ^2.0.6
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Chromium Authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style license that can be
5+
# found in the LICENSE file.
6+
7+
# INSTRUCTIONS:
8+
9+
# Builds the unminifed dart2js app (see DDC issue: https://github.com/dart-lang/sdk/issues/49869):
10+
# ./tool/build_extension.sh
11+
12+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
13+
echo "Building dart2js-compiled extension to /compiled directory."
14+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
15+
dart run build_runner build web --output build --release
16+
17+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
18+
echo "Updating manifest.json in /compiled directory."
19+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
20+
dart tool/update_dev_files.dart
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:build/build.dart';
6+
7+
/// Factory for the build script.
8+
Builder copyBuilder(_) => _CopyBuilder();
9+
10+
class _CopyBuilder extends Builder {
11+
@override
12+
Map<String, List<String>> get buildExtensions => {
13+
"web/{{}}.dart.js": ["compiled/{{}}.dart.js"],
14+
"web/{{}}.png": ["compiled/{{}}.png"],
15+
"web/manifest.json": ["compiled/manifest.json"],
16+
};
17+
18+
@override
19+
void build(BuildStep buildStep) async {
20+
final inputAsset = buildStep.inputId;
21+
final allowedOutputs = buildStep.allowedOutputs;
22+
23+
if (allowedOutputs.length != 1) {
24+
return;
25+
}
26+
27+
final outputAsset = allowedOutputs.first;
28+
await _copyBinaryFile(buildStep,
29+
inputAsset: inputAsset, outputAsset: outputAsset);
30+
}
31+
32+
Future<void> _copyBinaryFile(
33+
BuildStep buildStep, {
34+
required AssetId inputAsset,
35+
required AssetId outputAsset,
36+
}) {
37+
return buildStep.writeAsBytes(
38+
outputAsset, buildStep.readAsBytes(inputAsset));
39+
}
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
void main() async {
8+
_updateManifestJson();
9+
}
10+
11+
/// Adds the Googler extension key.
12+
Future<void> _updateManifestJson() async {
13+
final manifestJson = File('compiled/manifest.json');
14+
final extensionKeyTxt = File('extension_key.txt');
15+
final extensionKey = await extensionKeyTxt.exists()
16+
? await extensionKeyTxt.readAsString()
17+
: null;
18+
_transformDevFile(manifestJson, (line) {
19+
if (_matchesKey(line: line, key: 'name')) {
20+
return [
21+
line,
22+
if (extensionKey != null)
23+
_newKeyValue(
24+
oldLine: line,
25+
newKey: 'key',
26+
newValue: extensionKey,
27+
),
28+
];
29+
} else {
30+
return [line];
31+
}
32+
});
33+
}
34+
35+
Future<void> _transformDevFile(
36+
File devFile, List<String> Function(String) transformLine) async {
37+
final lines = devFile.readAsLinesSync();
38+
final newLines = <String>[];
39+
for (final line in lines) {
40+
newLines.addAll(transformLine(line));
41+
}
42+
final content = newLines.joinWithNewLine();
43+
return devFile.writeAsStringSync(content);
44+
}
45+
46+
bool _matchesKey({required String line, required String key}) {
47+
return line.trimLeft().startsWith('"$key":');
48+
}
49+
50+
String _newKeyValue({
51+
required String oldLine,
52+
String? newKey,
53+
String? newValue,
54+
}) {
55+
final lineStart = oldLine.leftPadding();
56+
final key = newKey != null ? '"$newKey": ' : '';
57+
final value = newValue != null ? '"$newValue"' : '';
58+
final lineEnd = oldLine.trim().endsWith(',') ? ',' : '';
59+
return '$lineStart$key$value$lineEnd';
60+
}
61+
62+
extension LeftPaddingExtension on String {
63+
String leftPadding() {
64+
String padding = '';
65+
int idx = 0;
66+
while (idx < length && this[idx] == ' ') {
67+
padding += ' ';
68+
idx++;
69+
}
70+
return padding;
71+
}
72+
}
73+
74+
extension JoinExtension on List<String> {
75+
String joinWithNewLine() {
76+
return '${join('\n')}\n';
77+
}
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
@JS()
6+
library background;
7+
8+
import 'package:js/js.dart';
9+
10+
import 'chrome_api.dart';
11+
import 'web_api.dart';
12+
13+
void main() {
14+
console.log('Running Dart Debug Extension.');
15+
// Detect clicks on the Dart Debug Extension icon.
16+
chrome.action.onClicked.addListener(allowInterop((_) {
17+
console.log('Detected click on the Dart Debug Extension icon.');
18+
}));
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:js/js.dart';
6+
7+
@JS()
8+
external Chrome get chrome;
9+
10+
@JS()
11+
@anonymous
12+
class Chrome {
13+
external Action get action;
14+
}
15+
16+
@JS()
17+
@anonymous
18+
class Action {
19+
// https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked
20+
external OnClickedHandler get onClicked;
21+
}
22+
23+
@JS()
24+
@anonymous
25+
class OnClickedHandler {
26+
external void addListener(void Function(Tab tab) callback);
27+
}
28+
29+
@JS()
30+
@anonymous
31+
class Tab {
32+
external int get id;
33+
external String get url;
34+
}
4.63 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "[MV3] Dart Debug Extension",
3+
"version": "1.0",
4+
"manifest_version": 3,
5+
"action": {
6+
"default_icon": "dart_dev.png"
7+
},
8+
"background": {
9+
"service_worker": "background.dart.js"
10+
}
11+
}

0 commit comments

Comments
 (0)