Javascript 未选中的runtime.lastError:无法访问url的内容&引用;。扩展清单必须请求访问此主机的权限。在清单3中
请帮帮我!我在清单3中收到错误“Unchecked runtime.lastError:无法访问url的内容”。扩展清单必须请求访问此主机的权限。“Unchecked runtime.lastError:无法建立连接。接收端不存在。” 来自内容脚本的侦听器Javascript 未选中的runtime.lastError:无法访问url的内容&引用;。扩展清单必须请求访问此主机的权限。在清单3中,javascript,google-chrome-extension,manifest.json,Javascript,Google Chrome Extension,Manifest.json,请帮帮我!我在清单3中收到错误“Unchecked runtime.lastError:无法访问url的内容”。扩展清单必须请求访问此主机的权限。“Unchecked runtime.lastError:无法建立连接。接收端不存在。” 来自内容脚本的侦听器 chrome.runtime.onMessage.addListener( function(req, sender, sendResponse) { if(req.msg === "analysis background&
chrome.runtime.onMessage.addListener(
function(req, sender, sendResponse) {
if(req.msg === "analysis background") {
let obj = parse();
sendResponse(obj);
}
return true;
}
);
);
manifest.json
{
"manifest_version": 3,
"name": "extensionParser",
"version": "1.0.0",
"action": {
"default_popup": "popups/popup.html"
},
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs", "scripting",
"http://localhost/site_for_parsing"]
}
来自后台文件的代码
const siteUrl = "http://localhost/site_for_parsing";
chrome.runtime.onConnect.addListener(port => {
port.onMessage.addListener(msg => {
if(msg.message === 'analysis') {
chrome.tabs.create({active: false, url: siteUrl}, tab => {
chrome.scripting.executeScript({
target: {tabId:tab.id},
files: ['dist/parser.js']
}, (results) => {
chrome.tabs.sendMessage(tab.id, {msg: "analysis background"}, res => {
port.postMessage(res)
chrome.tabs.remove(tab.id)
})
})
});
}
});
});
(异步()=>{
const tab=wait chrome.tabs.create({url:'https://www.example.com'});
const tabId=tab.id;
如果(!tab.url)等待onTabUrlUpdated(tabId);
const results=await chrome.scripting.executeScript({
目标:{tabId},
文件:['content.js'],
});
sendMessage(tabId,{msg:'analysis background'},res=>{
港口邮递信息(res);
chrome.tabs.remove(tabId);
});
})();
函数onTabUrlUpdated(tabId){
返回新承诺((解决、拒绝)=>{
const onUpdated=(id,info)=>id==tabId&&info.url&&done(true);
const onRemoved=id=>id==tabId&&done(false);
chrome.tabs.onUpdated.addListener(onUpdated);
chrome.tabs.onRemoved.addListener(onRemoved);
功能完成(ok){
chrome.tabs.onUpdate.removeListener(onUpdate);
chrome.tabs.onRemoved.removeListener(onRemoved);
(确定?决定:拒绝)();
}
});
}
@wOxxOm我尝试使用主机权限,但无效。我也犯了同样的错误,你推荐哪种方法?这取决于你是否愿意花时间迁移到ManifestV3,因为ManifestV3仍然到处都是bug。@wOxxOm可能是一个愚蠢的问题,但我能从v3切换到v2吗?一切都应该正常?我在手机上,所以我现在还不能尝试,但我不知道为什么我没有考虑降级,所以我很好奇这是否是支持的(即任何有效的v3配置也可以在v2中工作),这取决于具体情况。如果您遇到问题,请用适当的方式发布新问题。