Dart中的Chrome内容脚本 [英] Chrome content scripts in Dart
问题描述
是否可以使用dart作为chrome扩展内容脚本?下面似乎没有调用任何东西在main()
Is it possible to use dart for chrome extension content scripts? The following does not seem to call anything in main()
import 'dart:html';
import 'package:js/js.dart' as js;
void main() {
js.context.alert('Hello from Dart via JavaScript');
window.console.log("START!!!");
window.alert("alert");
}
manifest.json ...
manifest.json...
"content_scripts": [
{
"matches": [
"http://docs.google.com/*",
"https://docs.google.com/*"
],
"js": [
"packages/browser/dart.js",
"packages/browser/interop.js",
"packages/js/dart_interop.js",
"out.js"
],
"run_at" : "document_idle",
"all_frames" : false
}
],
推荐答案
我做了一些调试,我相信有一个bug dart2js为chrome.dart。
I did some debugging and I believe there is a bug in dart2js for the chrome.dart.
在content.dart.js的末尾是启动main函数的代码。我添加了(); 到这两个函数,并且更改后调用了main函数。
At the end of the content.dart.js is the code that starts the main function. I added (); to both functions and the main function was called after the change.
5显示,但不是#6。更改后,将调用所有来自1-6的console.log调用,并显示main函数中的print()。
Before the change console.log calls from 1-5 were displayed, but not #6. After the change all console.log calls from 1-6 are called and the print() in main function is displayed.
// BEGIN invoke [main].
(function(callback) {
console.log('Begin invoke main 1');
if (typeof document === "undefined") {
callback(null);
return;
}
console.log('Begin invoke main 2');
if (document.currentScript) {
callback(document.currentScript);
return;
}
console.log('Begin invoke main 3');
var scripts = document.scripts;
function onLoad(event) {
for (var i = 0; i < scripts.length; ++i)
scripts[i].removeEventListener("load", onLoad, false);
callback(event.target);
}
console.log('Begin invoke main 4');
for (var i = 0; i < scripts.length; ++i)
scripts[i].addEventListener("load", onLoad, false);
console.log('Begin invoke main 5');
})(); // <-- Added
(function(currentScript) {
console.log('Begin invoke main 6');
init.currentScript = currentScript;
if (typeof dartMainRunner === "function")
dartMainRunner(T.main, []);
else
T.main([]);
})(); // <-- Added
// END invoke [main].
这篇关于Dart中的Chrome内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!