2015-10-06 92 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