2016-04-21 57 views
2

在注入腳本中使用命令Chrome擴展注入腳本得到錯誤

chrome.tabs.executeScript(
    null, {file: "dialog.js"}); 

拋出錯誤

未選中runtime.lastError在運行tabs.executeScript:無法訪問URL的「鉻devtools內容:/ /devtools/bundled/inspector.html?& remoteBase = https://chrom ... om/serve_file/@ 4fc366553993dd1524b47a280fed49d8ec28421e/& dockSide = undocked「。擴展清單必須請求訪問此主機的權限。 在onNativeMessage(鉻 - 延伸://knldjmfmopnpolahpmmgbagdohdnhkik/background.js:31:5)

manifiest.json 
{ 
    "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB", 
    "name": "TerminusProLink", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "Link to ProLaw App", 
    "background": { 
    "scripts": [ "background.js", "background.html"] 
    }, 
    "content_scripts": [ 
    { 
     "all_frames": true, 
     "js": [ "jquery-1.5.1.js", "jquery-ui-1.8.11.js", "content.js" ], 
     "matches": [ "http://*/*", "https://*/*" ] 
    } 
    ], 

    "permissions": [ 
    "background", "tabs", "http://*/*", "https://*/*", 

    ] 
} 

任何具有一個溶液請建議。

+0

添加"web_accessible_resources": ["dialog.js"]順便說一句,你可以趕上lastError:https://stackoverflow.com/a/45603880/632951 – Pacerier

回答

2

請明確設置tabId參數executeScript,其中by default將是當前窗口的活動選項卡。

如果您無法直接獲取tabId,請使用chrome.tabs.query來查詢選項卡狀態。

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 
    for(var i = 0; i<tabs.length;i++) { 
     chrome.tabs.executeScript(tabs[i].id, {"file": "dialog.js"}); 
    } 
}); 

而且不要忘了在manifest.json

+0

謝謝,這是工作。但需要將消息傳遞給dialog.js。如何將消息傳遞給dialog.js方法? –

+0

@VishwajeetBose,如果你的意思是在調用'executeScript'時傳遞參數,看看http://stackoverflow.com/questions/17567624/pass-parameter-using-executescript-chrome –

+0

使用後,甚至不執行dialog.js碼。 chrome.tabs.executeScript(tabs [i] .id,{code:「var msg = 1;」},function(){ chrome.tabs.executeScript(tabs [i] .id,{「file」:「 dialog.js「}); }); } 在dialog.js中 function callCheck(){ debugger; var DialogBox = document.createElement(「div」); var TextPara = document.createElement(「p」); TextPara.innerHTML =「ss」; DialogBox.appendChild(TextPara); $(DialogBox).dialog({ }); } alert(msg);callCheck(); –