Chrome版本57.0.2987中的chrome.runtime.sendMessage错误 [英] chrome.runtime.sendMessage error in Chrome version 57.0.2987

查看:139
本文介绍了Chrome版本57.0.2987中的chrome.runtime.sendMessage错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的谷歌浏览器从版本 56.0.2924 更新为 57.0.2987

我有一个扩展,它不能在这个新版本中工作。我debuged,并找到原因是chrome.runtime.sendMessage不起作用。我在后台页面发送消息

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

我有一个监听器(同一个后台页面):

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.name == EVENT_ENUM.JOB_INIT ){
//做某事
}
});

我的 manifiest 确实注入了这些代码。

但运行 chrome.runtime.sendMessage 后没有任何事情发生。我搜查,但没有得到任何问题。有人对我有同样的问题吗?谢谢

解决方案

我不知道为什么它在56.0.2924中适合你,因为这种行为很久以前就被故意改变了在49.0.2622.0中,请参阅crbug.com/ 479425 commit r369379


切勿将端口连接到同一帧。连接到同一个框架是没有意义的,因为onMessage不应该为同一个框架触发。


它适合您的事实在Chrome 49出现问题后,立即修复。

同样的问题已经在crbug.com/ 报告704108 ,因此您可能会将其列为明星以遵循进度。





解决方案显然,是将侦听器提取到一个单独的函数中:

pre $ function $ on $ message $ message $ $ $ $ b .........
}

chrome.runtime.onMessage.addListener(onMessage);

然后直接调用它:

 的onMessage(.......); 


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

解决方案

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

这篇关于Chrome版本57.0.2987中的chrome.runtime.sendMessage错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆