3

I am trying to convert a Chrome Extension to Firefox using the new API WebExtension.

Everything works fine except the use of chrome.runtime.sendMessage() in a webpage. The goal is to communicate with the addon and pass some data.

For that, I am using the property "externally_connectable" as written here : can-a-site-invoke-a-browser-extension

background.js

chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {

    sendResponse({
        success: true,
        message: 'ok'
    });

    return true; // Bug chrome, close channel otherwise
});

In webpage

chrome.runtime.sendMessage(EXTENSION_ID, {type: 'show', data: 'test'}, function(response) {
if (response.success && !response.success) {
    console.log(response.message);
}

});

In chrome, the communication works fine but in Firefox, the code executed in the webpage doesn't work : "chrome is not defined".

Is there another var to use instead of "chrome" or is it not implemented ?

I've found nothing about this on the web :( Thanks

Community
  • 1
  • 1
JBGO
  • 41
  • 2
  • 5
  • `externally_connectable` is not listed as a supported key in [*manifest.json*](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json). The answer you have linked provides an event driven method of accomplishing the same capability. – Makyen Jul 20 '16 at 20:06

2 Answers2

5

Web extensions do not support externally_connectable website scripts but you can communicate between website scripts and extension scripts as shown in this example https://github.com/mdn/webextensions-examples/tree/master/page-to-extension-messaging

-1

Try use WebExtension APIs with "browser" namespace

browser.runtime.sendMessage(...)

All available APIs on mozilla development https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#WebExtension_APIs

Dima Kovalenko
  • 63
  • 1
  • 1
  • 5
  • 1
    This only works *within* the extension. @JBGO is trying to do it from within the webpage (which works on Chrome). – mlissner Mar 26 '18 at 22:23