Google chrome Chrome扩展注入脚本获取错误

Google chrome Chrome扩展注入脚本获取错误,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,关于使用命令注入脚本 chrome.tabs.executeScript( null, {file: "dialog.js"}); 投掷误差 运行tabs时未选中runtime.lastError.executeScript:无法访问url“chrome”的内容-devtools://devtools/bundled/inspector.html?&remoteBase=…om/serve_file/@4fc366553993dd1524b47a280fed49d8ec28421e/&do

关于使用命令注入脚本

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

运行tabs时未选中runtime.lastError.executeScript:无法访问url“chrome”的内容-devtools://devtools/bundled/inspector.html?&remoteBase=…om/serve_file/@4fc366553993dd1524b47a280fed49d8ec28421e/&dockSide=undocked”。扩展清单必须请求访问此主机的权限。 在onNativeMessage(浏览器)-extension://knldjmfmopnpolahpmmgbagdohdnhkik/background.js:31:5)


任何有解决方案的人请提出建议。

请明确设置
executeScript
tabId
参数,该参数将是当前窗口的活动选项卡

如果无法直接获取
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"});
    }
});
chrome.tabs.query({active:true,currentWindow:true},函数(tabs){

对于(var i=0;iThanks,它正在工作。但需要将消息传递给dialog.js。如何将消息传递给dialog.js方法?@VishwajeetBose,如果您的意思是在调用
executeScript
时传递参数,请查看甚至不执行dialog.js code.chrome.tabs.executeScript(tabs[i].id,{code:“var msg=1;”},函数(){chrome.tabs.executeScript(tabs[i].id,{“file”:“dialog.js”});}在dialog.js函数callCheck(){debugger;var DialogBox=document.createElement(“div”);var TextPara=document.createElement(“p”);TextPara.innerHTML=“ss”;DialogBox.appendChild(TextPara)$(DialogBox).dialog({});}alert(msg);callCheck();@VishwajeetBose,请注意当执行内部
executeScript
时,
i
tabs相等。长度
显然
tabs[i]
未定义。您可以定义一个局部变量,如
var tabId=tabs[i].id
,然后在
executeScript
中使用它。顺便说一句,您可以捕获最后一个错误:
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"});
    }
});