Using Dart in Chrome extension content script does

2019-05-25 22:15发布

问题:

I am trying to write a Chrome extension using Dart. So far everything goes well except for the content script --- the "main" function in the content script dart file does not seem to run.

To be more specific, first of all Dartium cannot be used since giving a dart file in the "js" rule in the manifest caused Dartium to complain; I next tried to compile the dart file (with csp: true) then make the manifest to include the compiled js file directly --- then I'm stuck, it seems that no matter what I try, the (compiled) "main" function just does not run.

Any suggestions?

Update:

The manifest file:

{
  "manifest_version": 2,
  "name": "Assistant",
  "description": "Assists you with various tasks.",
  "version": "1.0",
  "minimum_chrome_version": "26.0",
  "permissions": ["<all_urls>", "storage"],

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": [
        "packages/browser/dart.js",
        "dart_content_script.dart.js"
      ],
      "run_at": "document_start",
      "all_frames": false
    }
  ],

  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": "bulb.png"
  },

  "background": {
    "page": "background.html"
  }
}

The content script dart file:

void main() {
  print('main done');
}

The pubspec.yaml:

name: AssistentExtension

dependencies:
  browser: any
  chrome: any

dev_dependencies:
  unittest: '>=0.10.0'

transformers:
- $dart2js:
    csp: true

In the Chrome developer console, I can find the string "main done" meaning that the "main" function is indeed included in the compiled js, but nothing is printed meaning it is not run.

回答1:

I had the same problem like yours. At that time I looked into the console logs, a log saying chrome package is missing (but i don't know what cause it), so I manually added it back to build folder, then it worked so I can see my logs written in main().

When I run test_ext that come with official chrome pub, I get a different error message in console log, again I solved it and then the test_ext sample is running well too.

So, my advise is take a look at the console log, it might help. You can open console for extension by right click on popup UI of extension and select 'Inspect Element' to open it.