【发布时间】:2021-04-24 14:49:32
【问题描述】:
请帮帮我!我在谷歌浏览器清单 3 中收到错误 Unchecked runtime.lastError: Cannot access contents of url. Extension manifest must request permission to access this host. 和 Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.。
来自内容脚本的监听器
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)
})
})
});
}
});
});
提前感谢您!我在等你的答案。美好的一天!
【问题讨论】:
-
@wOxxOm 我尝试使用 host_permissions,但它不起作用。我也犯了同样的错误
-
你的
parse函数是做什么的?
标签: javascript google-chrome-extension manifest.json