2015-10-06 36 views
1

我正在开发Chrome扩展程序,我需要将JavaScript文件加载到内容脚本,但该文件正通过webpack-dev-server进行提供。所以它只能在本地主机上使用。Chrome扩展程序 - 从localhost加载文件到content_scripts

我试图改变我的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

只有本地文件可以在"content_scripts"部分中指定。

解决方案:

  • 添加"permissions": ["http://localhost:3000/scripts/*", "tabs"]到的manifest.json
  • 下载使用脚本的XMLHttpRequest(有很多例子),在您的background script(或更好an event page script)需要
  • 将它保存在chrome.storage.local时或localStorage(所以你可以加载它的每一个扩展从存储没有redownloading启动)
  • 注入脚本:
+0

谢谢。你会推荐什么背景脚本? –

+0

@LadislavMaxa,它应该包含实际执行答案中列出的事情的代码。 – wOxxOm

+0

@wOxxOm有趣的方法,我没有看到它记录在任何地方。如何在注入.js脚本的变化中引入热重载?或者你的解决方案已经解决了这个问题? – Steve06

相关问题