Chrome扩展程序 - 将文件从localhost加载到content_scripts

时间:2015-10-06 02:47:25

标签: javascript google-chrome google-chrome-extension

我正在开发Chrome扩展程序,我需要将javascript文件加载到内容脚本,但该文件通过webpack-dev-server提供。所以它只能在localhost上接近。

我试图改变我的manifest.json:

"content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": [
        "http://localhost:3000/scripts/content_bundle.js"
      ],
      "run_at": "document_end",
      "all_frames": false
    }

但是我在Chrome扩展程序窗口中出错:

enter image description here

1 个答案:

答案 0 :(得分:2)

只能在"content_scripts"部分中指定本地文件。

解决方案:

  • "permissions": ["http://localhost:3000/scripts/*", "tabs"]添加到manifest.json
  • background script(或更好an event page script)中使用XMLHttpRequest(有很多示例)下载脚本
  • 将其保存在chrome.storage.locallocalStorage中(这样您就可以在存储的每个扩展程序中加载它而无需重新加载)
  • 注入脚本: