Error: Could not establish connection. Receiving end does not exist


(Casey Smith) #1

I am new to Firefox extensions. I am trying to send a message from the popup script to the content script when a button is clicked, but I receive this error:

Could not establish connection. Receiving end does not exist.

The popup script:

function listenForClicks(){
	document.getElementById("rec").addEventListener("click", (e) => {
		browser.tabs.query({currentWindow: true, active : true})
		.then(send)
		.catch(onError);
	});
}
function onError(error) {
    console.log(`Error: ${error}`);
}
function send(tabs){
    browser.tabs.sendMessage(tabs[0].id, {command: "start",});
}
browser.tabs.executeScript({file: "/content_scripts/recorder.js"})
.then(listenForClicks);

The content script:

(function() {
	if (window.hasRun) {
		return;
	}
	window.hasRun = true;
	browser.runTime.onMessage.addListener((message) => {
		alert(message.command);
	});
})();

manifest.json:

{

  "manifest_version": 2,
  "name": "TW Recorder",
  "version": "1.0",

  "description": "Recorder.",

  "icons": {
    "48": "icons/border-48.png"
  },
  "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest"
  ],
  "browser_action": {
    "default_icon": "icons/border-48.png",
    "default_title": "Recorder",
    "default_popup": "popup/main.html"
      },

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content_scripts/jquery-3.3.1.min.js","content_scripts/recorder.js"]
    }
  ]

}

(Niklas Gollenstede) #2

Variable and property names in JavaScript are case sensitive. It’s browser.runtime, not browser.runTime!


(Casey Smith) #3

Wow, what a silly mistake! Thank you so much!