I'm working on a Chrome Extension but lately I've noticed I've been getting the following error (pointing to the first line of popup.html
):
Unchecked runtime.lastError: Could not establish connection. Receiving
end does not exist.
I've found a similar question here. But the error there is caused by the background
property which I haven't declared on my manifest.
I'm using chrome.extension.onMessage.addListener
on the contents.js
script to listen for events and chrome.tabs.sendMessage
on the popup.js
script to send the events. Most of the time everything works fine, but sometimes I get the above error and none of the requests do anything.
The manifest.json
is of the following format:
{
"manifest_version": 2,
"name": "APP_NAME",
"description": "APP_DESCRIPTION",
"version": "APP_VERSION",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage",
"clipboardRead",
"clipboardWrite"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
],
"css": [
"content.css"
]
}
]
}
Message Listener Example:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "this") console.log({
dom: doThis()
});
if (request.action === "that") sendResponse({
dom: doThat()
});
else if (request.action === "other") doOther();
else sendResponse({});
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…