Send a message from my website to my extension background page

My website and extension are cooperating in such a way that I need to send a message from my website (not content-script) to my background script. In Chrome, this could be achieved via

window.chrome.runtime.sendMessage(
  EXTENSION_ID,
  {
    // some message
  }
);

and then in manifest.json I declare the externally_connectable attribute, which is not supported by Firefox.

There doesn’t seem to be a way to do this with WebExtensions.

Any advice from anyone?

Looks like in Firefox it will (unfortunately) need a content-script intermediary. https://stackoverflow.com/questions/38132246/firefox-addon-send-message-from-webpage-to-background-script

Until https://bugzilla.mozilla.org/show_bug.cgi?id=1319168 is resolved you will indeed need a content script to interact with pages.

1 Like

Is there a way to pass message back to web page from extension.?

You can use postMessage to communicate with webpage (from your content script):

window.postMessage({type: "do_something"}, "https://example.com");
1 Like