I’m trying to use browser.runtime.sendMessage from background.js to a content script, but I keep having the error:
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist. <anonymous> moz-extension://e188c1d0-70e8-4b82-8e62-287a34ac151c/background.js:1
Here’s background.js
:
browser.runtime.sendMessage({hi:'hi'})
Here is content_scripts/sometest.js
:
browser.runtime.onMessage(message => {
console.log(message)
});
I even have a button in the pop up that should be sending a message whenever I click it. But it stills produces the same error, even if I click outside about:debugging
or a mozilla page (which theoretically you can’t inject content scripts). Here is choose_beast.js:
const storeone = document.querySelector("#storeone");
console.log(storeone) //this gets logged
storeone.addEventListener("click", () => {
console.log("clicked"); // also this gets logged in the console!
browser.runtime.sendMessage({action:"send_oi"}) //this produces the same error
});
And finally, here is my manifest.json
{
"description": "Adds a browser action icon to the toolbar. Click the button to choose a beast. The active tab's body content is then replaced with a picture of the chosen beast. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#beastify",
"manifest_version": 2,
"name": "Beastify",
"version": "1.0",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/beastify",
"icons": {
"48": "icons/beasts-48.png"
},
"permissions": [
"activeTab",
"webRequest",
"webRequestBlocking"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_scripts/sometest.js"]
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icons/beasts-32.png",
"theme_icons": [{
"light": "icons/beasts-32-light.png",
"dark": "icons/beasts-32.png",
"size": 32
}],
"default_title": "Beastify",
"default_popup": "popup/choose_beast.html"
},
"web_accessible_resources": [
"beasts/*.jpg",
"redirect.html"
]
}