因此,我正在尝试执行来自外部源的脚本,比如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…&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向popup.js中的background.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'"
}发布于 2015-06-26 23:40:33
朴实无华。将*://*/*添加到权限。
发布于 2017-08-15 17:31:10
虽然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));
});
});发布于 2019-10-21 18:38:01
通过manifest.json以声明方式注入内容脚本非常容易。matches属性确定脚本可以访问的URL,其中还可以包括文件系统URL。
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}],要了解有关匹配模式的更多信息,请访问https://developer.chrome.com/extensions/match_patterns
了解有关注入脚本https://developer.chrome.com/extensions/content_scripts#pi的更多信息
https://stackoverflow.com/questions/31028259
复制相似问题