Chrome content scripts in Dart

别说谁变了你拦得住时间么 提交于 2019-12-07 00:12:42

I'm not familiar with chrome extension content scripts myself, but from looking at the docs, it seems like they operate in a more restricted world then regular browser scripts. From the docs, they can't:

  • Use chrome.* APIs (except for parts of chrome.extension)
  • Use variables or functions defined by their extension's pages
  • Use variables or functions defined by web pages or by other content scripts

It could be that the dart2js output is running afoul of one of these rules. You can try compiling the dart code with the --disallow-unsafe-eval option. It's used for scripts that need to run in an CSP environment. You might also look in devtools to see if there are any helpful errors -

I did some debugging and I believe there is a bug in dart2js for the chrome.dart.

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.

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].
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!