我看到了很多关于这个错误的问题,但是我已经尝试了所有建议的解决方案,包括禁用我的所有其他的chrome扩展,但是没有任何东西修复了这个错误。每次之后,我都会重新加载扩展,但是同样的错误不断出现。
我要提到的是,我的代码没有包含"background.js“文件,因为我没有在其中找到任何需要,但是我可能认为我不需要它是错误的,我对此非常陌生。
我的代码:
popup.js
document.addEventListener("DOMContentLoaded", async () =>
{
var activate_switch = document.querySelector("input[id=activate]");
activate_switch.addEventListener('change', function()
{
if(this.checked)
{
console.log("activated");
chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
{
if(tabs.length == 0)
{
console.log("could not send mesage to current tab");
}
else
{
chrome.tabs.sendMessage(tabs[0].id, {message:"run"}, function(response)
{
console.log("Hello");
});
};
});
}
});
});
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "run" ) {
console.log("made it");
}
}
);
manifest.json
{
"name": "First Chrome Extension",
"description": "Build an Extension!",
"version": "0.01",
"externally_connectable": {
"ids": [
"*"
]
},
"permissions": [
"tabs",
"background",
"activeTab"
],
"action": {
"default_title": "Scraper",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"contentScript.js"
],
"run_at": "document_end"
}
],
"manifest_version": 3
}
它会引发一个错误:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
发布于 2022-11-30 21:57:48
尝试删除chrome.tabs.sendMessage的回调函数。
所以,不是这样的:
chrome.tabs.sendMessage(tabs[0].id, {message:"run"}, function(response)
{
console.log("Hello");
});
就这么做吧:
chrome.tabs.sendMessage(tabs[0].id, {message:"run"});
https://stackoverflow.com/questions/73719041
复制相似问题