Google chrome extension chrome.tabs.executeScript thows错误“;运行tabs时未选中runtime.lastError.executeScript:无法访问url的内容…”;
所以我尝试从外部源执行一个脚本,比如background.js中的www.script.google.com。 但是我得到了这个错误-Google chrome extension chrome.tabs.executeScript thows错误“;运行tabs时未选中runtime.lastError.executeScript:无法访问url的内容…”;,google-chrome-extension,Google Chrome Extension,所以我尝试从外部源执行一个脚本,比如background.js中的www.script.google.com。 但是我得到了这个错误- Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/devtools.html?&remoteBase=https://chrome…&dock
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/devtools.html?&remoteBase=https://chrome…&dockSide=undocked&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)". Extension manifest must request permission to access this host.
我正在做的是将消息从popup.js发送到background.js
在popup.js中-
chrome.runtime.sendMessage({type:"addtask"});
在background.js中-
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if(request.type == "addtask")
{
chrome.tabs.executeScript(null,
{file:"https://script.google.com/url of script....."});
}
});
我的manifest.json-
{
"name": "Extension",
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [{
"js": ["jquery.min.js","contentscript.js"],
"matches": ["http://*/*","https://*/*"],
"css" : ["feedback.css"]
}],
"permissions": [
"storage","tabs","https://script.google.com"
],
"web_accessible_resources": ["feedback.js","html2canvas.js","event.js"],
"content_security_policy": "script-src 'self' https://script.google.com/*; object-src 'self'"
}
平直的。将
*://*/*/*
添加到权限中。在这种情况下,虽然ArtPip建议有效,但通常您希望在某个选项卡或所有选项卡上执行脚本,并在您的权限不允许在该选项卡或某些选项卡上注入时正确处理错误
以下是在所有选项卡上执行脚本并正确处理错误的示例:
tabs.forEach(tab => {
chrome.tabs.executeScript(tab.id, { file: filename }, result => {
const lastErr = chrome.runtime.lastError;
if (lastErr) console.log('tab: ' + tab.id + ' lastError: ' + JSON.stringify(lastErr));
});
});
通过manifest.json以声明方式注入内容脚本非常容易。matches属性确定脚本可以访问哪些URL,这些URL还可以包括文件系统URL
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}],
“内容脚本”:[{
“匹配项”:[“
要了解有关注入脚本的更多信息您的意图不是100%清楚。您是尝试将外部脚本作为内容脚本还是作为背景脚本执行?@Sid,如果您想捕获最后一个错误,请看这是难以置信的。经过数小时的研究,我发现我的权限URL错误:缺少尾随的星号符号o它只匹配主页。Chrome上次的错误有点误导,因为它审查了我试图通过“数据:text/html,chromewbdata”访问的URL"有人能解释一下,这和吗?我只是想在当前检查的窗口上获取数据,这个权限使我能够这样做,但我不想要求用户提供看起来吓人的权限。为什么这不在教程的文档中?*://*
很好,但是要小心chrome://extensions/
允许捕获内部错误,例如错误\重试次数过多\错误\代理\连接\失败错误\空\响应错误\连接\重置等。