2016-04-21 60 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(); –