2

I've updated my google chrome browser from version: 56.0.2924 to 57.0.2987.

I have an extension, and it can not work in this new version. I debuged, and find the reason is chrome.runtime.sendMessage do not work. I send a message in background page:

chrome.runtime.sendMessage({"name": EVENT_ENUM.JOB_INIT, "data": job});

And i have a listener (the same background page):

chrome.runtime.onMessage.addListener(
   function (request, sender, sendResponse) {
     if (request.name == EVENT_ENUM.JOB_INIT) {       
       //do something
     }
   });

My manifiest has surely injected these codes.

But after run the chrome.runtime.sendMessage nothing happened. I searched but got no issue about that. Is anyone has the same issue with me? Thank you

1 Answer 1

3

I don't know why it works for you in 56.0.2924 because this behavior was changed intentionally long time ago in 49.0.2622.0, see crbug.com/479425 commit r369379:

Never connect a port to the same frame. Connecting to the same frame does not make sense because onMessage should not be triggered for the same frame.

The fact that it worked for you after Chrome 49 was a bug, fixed now.
The same issue is already reported in crbug.com/704108 so you may star it to follow the progress.


The solution, obviously, is to extract the listener to a separate function:

function onMessage(message, sender, sendResponse) {
    .........
}

chrome.runtime.onMessage.addListener(onMessage);

then call it directly:

onMessage(.......);
2
  • How does the concept of 'port' and 'frame' apply to this problem? What even is a 'frame' in this context? Commented Apr 13, 2017 at 4:45
  • A frame is basically a window: each built-in extension page like popup/background/options or a web page has one, iframes also are pages so they have own window. As for port, it's created automatically by runtime.sendMessage which is simply a wrapper of runtime.connect and port.postMessage.
    – woxxom
    Commented Apr 13, 2017 at 4:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.