Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 17 additions & 31 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,30 @@
@JS()
library background;

import 'dart:html';

import 'package:js/js.dart';

import 'chrome_api.dart';
import 'messaging.dart';

void main() {
// Detect clicks on the Dart Debug Extension icon.
chrome.action.onClicked.addListener(allowInterop((_) async {
await _createDebugTab();
await _executeInjectorScript();
}));
}

Future<Tab> _createDebugTab() async {
final url = chrome.runtime.getURL('debug_tab.html');
final tabPromise = chrome.tabs.create(TabInfo(
active: false,
pinned: true,
url: url,
));
return promiseToFuture<Tab>(tabPromise);
_registerListeners();
}

Future<void> _executeInjectorScript() async {
final tabId = await _getTabId();
if (tabId != null) {
chrome.scripting.executeScript(
InjectDetails(
target: Target(tabId: tabId), files: ['iframe_injector.dart.js']),
/*callback*/ null,
);
}
void _registerListeners() {
chrome.runtime.onMessage.addListener(allowInterop(_handleRuntimeMessages));
}

Future<int?> _getTabId() async {
final query = QueryInfo(active: true, currentWindow: true);
final tabs = List<Tab>.from(await promiseToFuture(chrome.tabs.query(query)));
final tab = tabs.isNotEmpty ? tabs.first : null;
return tab?.id;
void _handleRuntimeMessages(
dynamic jsRequest, MessageSender sender, Function sendResponse) async {
if (jsRequest is! String) return;

interceptMessage(
message: jsRequest,
expectedSender: Script.detector,
expectedRecipient: Script.background,
expectedType: MessageType.dartAppReady,
messageHandler: (_) {
// Update the icon to show that a Dart app has been detected:
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
});
}
86 changes: 11 additions & 75 deletions dwds/debug_extension_mv3/web/chrome_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ external Chrome get chrome;
@anonymous
class Chrome {
external Action get action;
external Debugger get debugger;
external Runtime get runtime;
external Scripting get scripting;
external Tabs get tabs;
}

/// chrome.action APIs
/// https://developer.chrome.com/docs/extensions/reference/action

@JS()
@anonymous
class Action {
// https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked
external void setIcon(IconInfo iconInfo, Function? callback);

external OnClickedHandler get onClicked;
}

Expand All @@ -32,27 +33,20 @@ class OnClickedHandler {

@JS()
@anonymous
class Debugger {
// https://developer.chrome.com/docs/extensions/reference/debugger/#method-attach
external void attach(
Debuggee target, String requiredVersion, Function? callback);

// https://developer.chrome.com/docs/extensions/reference/debugger/#method-sendCommand
external void sendCommand(Debuggee target, String method,
Object? commandParams, Function? callback);
class IconInfo {
external String get path;
external factory IconInfo({String path});
}

/// chrome.runtime APIs:
/// https://developer.chrome.com/docs/extensions/reference/runtime

@JS()
@anonymous
class Runtime {
// https://developer.chrome.com/docs/extensions/reference/runtime/#method-sendMessage
external void sendMessage(
String? id, Object? message, Object? options, Function? callback);

// https://developer.chrome.com/docs/extensions/reference/runtime/#method-getURL
external String getURL(String path);

// https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
external OnMessageHandler get onMessage;
}

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

@JS()
@anonymous
class Scripting {
// https://developer.chrome.com/docs/extensions/reference/scripting/#method-executeScript
external executeScript(InjectDetails details, Function? callback);
}

@JS()
@anonymous
class Tabs {
// https://developer.chrome.com/docs/extensions/reference/tabs/#method-query
external Object query(QueryInfo queryInfo);

// https://developer.chrome.com/docs/extensions/reference/tabs/#method-create
external Object create(TabInfo tabInfo);
}

@JS()
@anonymous
class Debuggee {
external int get tabId;
external String get extensionId;
external String get targetId;
external factory Debuggee({int tabId, String? extensionId, String? targetId});
}

@JS()
@anonymous
class MessageSender {
Expand All @@ -98,41 +66,9 @@ class MessageSender {
external factory MessageSender({String? id, String? url, Tab? tab});
}

@JS()
@anonymous
class TabInfo {
external bool? get active;
external bool? get pinned;
external String? get url;
external factory TabInfo({bool? active, bool? pinned, String? url});
}

@JS()
@anonymous
class QueryInfo {
external bool get active;
external bool get currentWindow;
external factory QueryInfo({bool? active, bool? currentWindow});
}

@JS()
@anonymous
class Tab {
external int get id;
external String get url;
}

@JS()
@anonymous
class InjectDetails {
external Target get target;
external List<String>? get files;
external factory InjectDetails({Target target, List<String> files});
}

@JS()
@anonymous
class Target {
external int get tabId;
external factory Target({int tabId});
}
Binary file added dwds/debug_extension_mv3/web/dart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 0 additions & 59 deletions dwds/debug_extension_mv3/web/debug_tab.dart

This file was deleted.

10 changes: 0 additions & 10 deletions dwds/debug_extension_mv3/web/debug_tab.html

This file was deleted.

45 changes: 45 additions & 0 deletions dwds/debug_extension_mv3/web/detector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@JS()
library detector;

import 'dart:html';
import 'package:js/js.dart';

import 'chrome_api.dart';
import 'messaging.dart';

void main() {
_registerListeners();
}

void _registerListeners() {
document.addEventListener('dart-app-ready', _onDartAppReadyEvent);
}

void _onDartAppReadyEvent(Event event) {
_sendMessageToBackgroundScript(
type: MessageType.dartAppReady,
body: 'Dart app ready!',
);
}

void _sendMessageToBackgroundScript({
required MessageType type,
required String body,
}) {
final message = Message(
to: Script.background,
from: Script.detector,
type: type,
body: body,
);
chrome.runtime.sendMessage(
/*id*/ null,
message.toJSON(),
/*options*/ null,
/*callback*/ null,
);
}
75 changes: 0 additions & 75 deletions dwds/debug_extension_mv3/web/iframe.dart

This file was deleted.

9 changes: 0 additions & 9 deletions dwds/debug_extension_mv3/web/iframe.html

This file was deleted.

Loading