1
votes

Trying to convert an extension from chrome to edge and I'm having an issue with browser.runtime.sendmessage() with a listener. To simplify things, I created a simple hello world extension. This new extension works in chrome but not edge. Anybody know what I need to change?

manifest.json

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "author" : "Sandbox",
  "version": "0.1",
  "content_scripts": [ {
    "all_frames": true,
    "js": [ "jquery-3.1.1.min.js", "content_script.js" ],
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ]
  } ],
  "permissions": [ "http://*/*", "https://*/*", "storage" ],
  "background": {
    "scripts": [
      "jquery-3.1.1.min.js",
      "background.js"
    ], 
    "persistent": true 
  }
}

background.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(request.greeting);
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });

content_scripts.js

browser.runtime.sendMessage({greeting: "hello"}, function(response) {
    if (response != undefined) {
        console.log(response.farewell);
    }
});
1
I am not sure but I ran into this issue and looked like Edge requires to refresh the page in order to send a message. - Hung Cao
When in chrome, you should use chrome.* api while in Edge, you should use browser.* api. - Haibara Ai
As far as I can tell it doesn't matter if I use browser or chrome in edge or chrome, but I didn't realize I was mix and matching. I will make them all the same when I get back to work Monday - Brian
When I made them both "Browser" it works. - Brian

1 Answers

1
votes

The correct syntax for Edge is found here: Edge Supported APIs

In the note section:

For Microsoft Edge, all extension APIs are under the browser namespace, 
e.g. browser.browserAction.disable().
Microsoft Edge extension APIs use callbacks, not promises.

Use browser.runtime.sendMessage() instead of chrome.runtime.sendMessage().