browser.runtime.onMessage.addListener in Popup broke communication between content and background scripts

Fixed, flighted Issue #8473140

Details

Author
Ilia L.
Created
Aug 12, 2016
Privacy
This issue is public.
Found in
  • Microsoft Edge
Found in build #
38.14393
Fixed in build #
15002
Reports
Reported by 3 people

Sign in to watch or report this issue.

Steps to reproduce

I’m developing edge browser extension. I have popup, persistent background page and content script. I want to send messages from content script to background. I also want to send messages between background and popup (for example to update UI when server load complete etc.) Communication is made with

browser.runtime.sendMessage,
browser.runtime.onMessage

Looks like adding onMessage listener in Popup broke communication between content and background scripts.

This is the example code to reproduce the problem:

manifest.json

{
    "name": "TEST",
    "short_name": "TEST",
    "author": "TEST",
    "version": "0.1",
    "manifest_version": 2,
    "background": {
        "page": "background.html",
        "persistent": true
    },
    "permissions": [
        "unlimitedStorage",
        "contextMenus",
        "cookies",
        "tabs",
        "notifications",
        "http://*/*",
        "https://*/*",
        "activeTab",
        "<all_urls>"
    ],
    "content_scripts": [
      {
          "all_frames": true,
          "run_at": "document_start",
          "js": [
              "js/content.js"
          ],
          "matches": [
              "http://*/*",
              "https://*/*",
              "file:///*"
          ]
        }
    ],
    "browser_action": {
        "default_title": "RUN TEST",
        "default_popup": "popup.html"
    }
}

popup.html

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript" src="js/popup.js"></script>
</head>
<body style="min-width: 300px; min-height: 415px;">
</body>
</html>

popup.js

console.log("POPUP: start");

window.addEventListener("unload", OnUnload, false);
window.addEventListener("load", OnLoad, false);

function OnLoad() {
    console.log("POPUP: OnLoad");
    browser.runtime.onMessage.addListener(OnMessage);
    //chrome.runtime.onMessage.addListener(OnMessage);
}

function OnUnload() {
    console.log("POPUP: Unload");
    browser.runtime.onMessage.removeListener(OnMessage);
    //chrome.runtime.onMessage.removeListener(OnMessage);
    window.removeEventListener("unload", OnUnload);
    window.removeEventListener("load", OnLoad);
}

function OnMessage(request, sender, sendResponse) {
    console.log("POPUP: OnMessage " + request.name);

    if (request.name === "hi") {
        sendResponse({"message": "Hello from Popup"});
    }
}

background.js

console.log("BG: start");

window.addEventListener("load", OnLoad, true);

function OnLoad() {
    console.log("BG: OnLoad");
    browser.runtime.onMessage.addListener(BGOnMessage);
    //chrome.runtime.onMessage.addListener(BGOnMessage);
}

function BGOnMessage(request, sender, sendResponse) {
    console.log("BG: BGOnMessage " + request.name);

    if (request.name === "hi") {
        sendResponse({"message": "Hello from BG"});
    }
}

content.js

console.log("CS: start");

browser.runtime.sendMessage({"name": "hi"}, function (response) {
//chrome.runtime.sendMessage({"name": "hi"}, function (response) {
    if (!response) {
        console.log("Empty response!!!");
        return;
    }
    console.log("CS: response message = " + response.message);
});

It seems to work in Chrome, but in Edge I always receive “Empty response!!!” after opening popup once. If i comment

browser.runtime.onMessage.addListener(OnMessage); 

in popup code all works fine, but adding this listener once broke messages receiving in background. Looks like a bug in extension platform.

Attachments

1 attachment

Comments and activity

  • Microsoft Edge Team

    Changed Assigned To to “Brad E.”

    Changed Assigned To to “Sermet I.”

    Changed Assigned To to “Sebastian P.”

    Changed Assigned To to “Akshay P.”

    Changed Assigned To from “Akshay P.” to “Anushree B.”

    Changed Status to “Confirmed”

    Changed Assigned To from “Anushree B.” to “Arif S.”

  • Thanks for filing this bug! We’ve confirmed the issue and plan on addressing it in a future update. In the meantime, you can use the following workaround to avoid this behavior:

    Replace the call to sendResponse() in the background script with a call to browser.tabs.sendMessage() and pass in sender.tab.id as the tabId parameter
    In the content script, add a listener to the browser.onMessage event with a callback that performs the same actions that the sendResponse() method does. If you have multiple messages that could fire this callback, you will need to uniquely identify them by adding a new property in the message object being sent back in the previous step.
    Remove the passing of the sendResponse() method in the sendMessage() call in the content script.

    Feel free to let me know if you have any questions! Thanks again!

  • Microsoft Edge Team

    Changed Status from “Confirmed” to “In code review”

    Changed Status from “In code review” to “In progress”

    Changed Status from “In progress” to “Fixed, not yet flighted”

    Changed Assigned To to “Akshay P.”

    Changed Status from “Fixed, not yet flighted” to “Fixed, flighted”

  • Running version:
    Microsoft Edge 39.14986.1000.0
    Microsoft EdgeHTML 15.4986

    And it’s still happening. Has the patch been released?

  • Microsoft Edge Team

    Changed Assigned To from “Akshay P.” to “Snehita P.”

  • Hello,

    Thank you for providing this information about the issue. We are pleased to report this feature is fixed in Edge 15063 and is available in our latest Insider Preview build.

    Best Wishes,
    The MS Edge Team

You need to sign in to your Microsoft account to add a comment.

Sign in