Firefox webextension Error: Could not establish connection. Receiving end does not exist
You get the error:
Error: Could not establish connection. Receiving end does not exist.
when you attempt to communicate (e.g. tabs.sendMessage()
, tabs.connect()
) to a tab where a content script is not listening for messages. This includes times when a content script does not exist in the tab.
You can not inject content scripts into about:*
URLs
For your issue, you are getting this error because no content script is injected in the tab. By the time you are trying to send the message, in a main_frame
webRequest.onErrorOccurred
event, the URL for the tab is already about:neterror?[much more, including the URL where the error occurred]
. You can not inject content scripts into about:*
URLs. Thus, there is no content script in the tab listening for your message.
Specifically, you have used the <all_urls>
match pattern in your manifest.json content_scripts
entry. <all_urls>
matches:
The special value
"<all_urls>"
matches all URLs under any of the supported schemes: that is, "http", "https", "file", "ftp", "app".
It does not match about:*
URLs.
For a bit more discussion of the URL used when Firefox gets a webRequest.onErrorOccurred
event in a main_frame
, see "Injecting into navigation error page gets: Error: No window matching {“matchesHost”:[“”]}"
Another solution to this problem: If you reload your extension (as a normal part of the dev) it severs all connections to content scripts.
You must remember to reload the page with the content script as well for it to re-listen correctly.
I also had this same error.
My problem and solution was different but I'm adding it in case it helps.
In my case my content.js script initially did not have a browser.runtime.onMessage.addListener() function (FireFox).
When I later added this listener to the content.js script I did not reload the temporary extension in the "about:debugging" page in FireFox. I got the above error.
After clicking "reload" in the "about:debugging" tab the content script received the message.