1

Is it possible to send a message from Content script to Browser action directly without using background page? Following is a simplified version of what my code looks like. The content script seems to be working fine, but I get the following error in the console:

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

I am assuming it's because the Browser action is not always active. But I don't want to use a Background page because I don't want the script running constantly hogging memory. I am hoping to send message directly to Browser action and display a popup, sort of like browserAction.onClicked displaying a popup. This is my first extension that I am trying to build, so trying to figure things out. Thanks

[manifest.json]

{

  "manifest_version": 2,
  "name": "Test",
  "version": "0.1",
  "icons": {
    "48": "icons/test.png"
  },

  "permissions": [
    "activeTab"
  ],

  "browser_action": {
    "default_icon":"icons/test.png",
    "default_title": "test",
    "default_popup": "popup/popup.html",
    "browser_style": true
  },

  "content_scripts": [
    {
      "matches": ["*://testwebsite"],
      "js": ["content_scripts/content-script.js"]
    }
  ]

}

[popup.js]

function handleMessage(request, sender, sendResponse) {
  console.log("Message from the content script: " +
  request.greeting);
  sendResponse({response: "Response from background script"});
}

browser.runtime.onMessage.addListener(handleMessage);

[content-script.js]

function handleResponse(message) {
  console.log(`Message from the background script:  ${message.response}`);
}

function handleError(error) {
  console.log(`Error: ${error}`);
}


function send_2_popup() {
    var sending = browser.runtime.sendMessage({
    greeting: "Greeting from the content script"
    });
    sending.then(handleResponse, handleError);
}


var btn = document.getElementById("btn");
btn.addEventListener("click", send_2_popup);
J.Doe
  • 11
  • 2
  • "it's because the Browser action is not always active" -> You appear to have answered you own question. What are you *actually* asking. Yes, in order for the popup to receive something it has to *exist*. If the popup isn't open, it doesn't exist. – Makyen Sep 16 '17 at 23:20
  • You can't do what you want without a background page. There is no capability to programatically open the actual extension popup. However, you can fake it. See the "Open a pseudo-popup" section of my answer to [How to create a global hotkey for opening the "browserAction" popup in Firefox (WebExtensions)?](https://stackoverflow.com/a/40296092) – Makyen Sep 16 '17 at 23:27
  • Thanks for the information. It sucks that you have to have the background script running to hog resources. – J.Doe Sep 17 '17 at 19:04
  • A well-written background script won't use much wrt. resources. It should only be responding to relatively rare events. It almost never needs to load large libraries. Having a single, larger background script is *significantly* better than having content scripts that load lots of rarely needed stuff (often including large libraries) into *every single page*. When an extension does that, the code is duplicated in every tab (think hundreds of tabs). As an example, [here's an extension](https://stackoverflow.com/q/39054125) that loads 78 different scripts into **every** `https://` page. – Makyen Sep 17 '17 at 19:15

1 Answers1

-2

You can rather send a message from the popup to the background and get a response as well as a message from background.. this way the background will know that the popup exists and hence the message from background to popup will be successful.