chrome.runtime.sendMessage error in Chrome version 57.0.2987

≡放荡痞女 提交于 2019-12-31 03:17:26

问题


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:


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(.......);


来源:https://stackoverflow.com/questions/43066596/chrome-runtime-sendmessage-error-in-chrome-version-57-0-2987

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!