Skip to content

Commit daa154d

Browse files
authored
Reset MV3 extension to nice starting point (#1773)
1 parent 98a6142 commit daa154d

File tree

11 files changed

+96
-442
lines changed

11 files changed

+96
-442
lines changed

dwds/debug_extension_mv3/web/background.dart

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,30 @@
55
@JS()
66
library background;
77

8-
import 'dart:html';
9-
108
import 'package:js/js.dart';
119

1210
import 'chrome_api.dart';
11+
import 'messaging.dart';
1312

1413
void main() {
15-
// Detect clicks on the Dart Debug Extension icon.
16-
chrome.action.onClicked.addListener(allowInterop((_) async {
17-
await _createDebugTab();
18-
await _executeInjectorScript();
19-
}));
20-
}
21-
22-
Future<Tab> _createDebugTab() async {
23-
final url = chrome.runtime.getURL('debug_tab.html');
24-
final tabPromise = chrome.tabs.create(TabInfo(
25-
active: false,
26-
pinned: true,
27-
url: url,
28-
));
29-
return promiseToFuture<Tab>(tabPromise);
14+
_registerListeners();
3015
}
3116

32-
Future<void> _executeInjectorScript() async {
33-
final tabId = await _getTabId();
34-
if (tabId != null) {
35-
chrome.scripting.executeScript(
36-
InjectDetails(
37-
target: Target(tabId: tabId), files: ['iframe_injector.dart.js']),
38-
/*callback*/ null,
39-
);
40-
}
17+
void _registerListeners() {
18+
chrome.runtime.onMessage.addListener(allowInterop(_handleRuntimeMessages));
4119
}
4220

43-
Future<int?> _getTabId() async {
44-
final query = QueryInfo(active: true, currentWindow: true);
45-
final tabs = List<Tab>.from(await promiseToFuture(chrome.tabs.query(query)));
46-
final tab = tabs.isNotEmpty ? tabs.first : null;
47-
return tab?.id;
21+
void _handleRuntimeMessages(
22+
dynamic jsRequest, MessageSender sender, Function sendResponse) async {
23+
if (jsRequest is! String) return;
24+
25+
interceptMessage(
26+
message: jsRequest,
27+
expectedSender: Script.detector,
28+
expectedRecipient: Script.background,
29+
expectedType: MessageType.dartAppReady,
30+
messageHandler: (_) {
31+
// Update the icon to show that a Dart app has been detected:
32+
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
33+
});
4834
}

dwds/debug_extension_mv3/web/chrome_api.dart

Lines changed: 11 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ external Chrome get chrome;
1111
@anonymous
1212
class Chrome {
1313
external Action get action;
14-
external Debugger get debugger;
1514
external Runtime get runtime;
16-
external Scripting get scripting;
17-
external Tabs get tabs;
1815
}
1916

17+
/// chrome.action APIs
18+
/// https://developer.chrome.com/docs/extensions/reference/action
19+
2020
@JS()
2121
@anonymous
2222
class Action {
23-
// https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked
23+
external void setIcon(IconInfo iconInfo, Function? callback);
24+
2425
external OnClickedHandler get onClicked;
2526
}
2627

@@ -32,27 +33,20 @@ class OnClickedHandler {
3233

3334
@JS()
3435
@anonymous
35-
class Debugger {
36-
// https://developer.chrome.com/docs/extensions/reference/debugger/#method-attach
37-
external void attach(
38-
Debuggee target, String requiredVersion, Function? callback);
39-
40-
// https://developer.chrome.com/docs/extensions/reference/debugger/#method-sendCommand
41-
external void sendCommand(Debuggee target, String method,
42-
Object? commandParams, Function? callback);
36+
class IconInfo {
37+
external String get path;
38+
external factory IconInfo({String path});
4339
}
4440

41+
/// chrome.runtime APIs:
42+
/// https://developer.chrome.com/docs/extensions/reference/runtime
43+
4544
@JS()
4645
@anonymous
4746
class Runtime {
48-
// https://developer.chrome.com/docs/extensions/reference/runtime/#method-sendMessage
4947
external void sendMessage(
5048
String? id, Object? message, Object? options, Function? callback);
5149

52-
// https://developer.chrome.com/docs/extensions/reference/runtime/#method-getURL
53-
external String getURL(String path);
54-
55-
// https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
5650
external OnMessageHandler get onMessage;
5751
}
5852

@@ -63,32 +57,6 @@ class OnMessageHandler {
6357
void Function(dynamic, MessageSender, Function) callback);
6458
}
6559

66-
@JS()
67-
@anonymous
68-
class Scripting {
69-
// https://developer.chrome.com/docs/extensions/reference/scripting/#method-executeScript
70-
external executeScript(InjectDetails details, Function? callback);
71-
}
72-
73-
@JS()
74-
@anonymous
75-
class Tabs {
76-
// https://developer.chrome.com/docs/extensions/reference/tabs/#method-query
77-
external Object query(QueryInfo queryInfo);
78-
79-
// https://developer.chrome.com/docs/extensions/reference/tabs/#method-create
80-
external Object create(TabInfo tabInfo);
81-
}
82-
83-
@JS()
84-
@anonymous
85-
class Debuggee {
86-
external int get tabId;
87-
external String get extensionId;
88-
external String get targetId;
89-
external factory Debuggee({int tabId, String? extensionId, String? targetId});
90-
}
91-
9260
@JS()
9361
@anonymous
9462
class MessageSender {
@@ -98,41 +66,9 @@ class MessageSender {
9866
external factory MessageSender({String? id, String? url, Tab? tab});
9967
}
10068

101-
@JS()
102-
@anonymous
103-
class TabInfo {
104-
external bool? get active;
105-
external bool? get pinned;
106-
external String? get url;
107-
external factory TabInfo({bool? active, bool? pinned, String? url});
108-
}
109-
110-
@JS()
111-
@anonymous
112-
class QueryInfo {
113-
external bool get active;
114-
external bool get currentWindow;
115-
external factory QueryInfo({bool? active, bool? currentWindow});
116-
}
117-
11869
@JS()
11970
@anonymous
12071
class Tab {
12172
external int get id;
12273
external String get url;
12374
}
124-
125-
@JS()
126-
@anonymous
127-
class InjectDetails {
128-
external Target get target;
129-
external List<String>? get files;
130-
external factory InjectDetails({Target target, List<String> files});
131-
}
132-
133-
@JS()
134-
@anonymous
135-
class Target {
136-
external int get tabId;
137-
external factory Target({int tabId});
138-
}

dwds/debug_extension_mv3/web/dart.png

2.99 KB
Loading

dwds/debug_extension_mv3/web/debug_tab.dart

Lines changed: 0 additions & 59 deletions
This file was deleted.

dwds/debug_extension_mv3/web/debug_tab.html

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 detector;
7+
8+
import 'dart:html';
9+
import 'package:js/js.dart';
10+
11+
import 'chrome_api.dart';
12+
import 'messaging.dart';
13+
14+
void main() {
15+
_registerListeners();
16+
}
17+
18+
void _registerListeners() {
19+
document.addEventListener('dart-app-ready', _onDartAppReadyEvent);
20+
}
21+
22+
void _onDartAppReadyEvent(Event event) {
23+
_sendMessageToBackgroundScript(
24+
type: MessageType.dartAppReady,
25+
body: 'Dart app ready!',
26+
);
27+
}
28+
29+
void _sendMessageToBackgroundScript({
30+
required MessageType type,
31+
required String body,
32+
}) {
33+
final message = Message(
34+
to: Script.background,
35+
from: Script.detector,
36+
type: type,
37+
body: body,
38+
);
39+
chrome.runtime.sendMessage(
40+
/*id*/ null,
41+
message.toJSON(),
42+
/*options*/ null,
43+
/*callback*/ null,
44+
);
45+
}

dwds/debug_extension_mv3/web/iframe.dart

Lines changed: 0 additions & 75 deletions
This file was deleted.

dwds/debug_extension_mv3/web/iframe.html

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)