我一直在研究chrome扩展,它需要在某些时候传递消息来触发当前网页上的一些事件。发送一个时间消息从背景页面到devtool页面
我一直在使用chrome.runtime.sendMessage将消息从devtool页面发送到背景页面。并在背景页面上,我使用chrome.runtime.onMessage.addListener获取这些消息,它正在工作。
但我不能从背景页面发送消息到devtools页面使用此! 我怎样才能从背景页面发送一次性消息到devtool页面?
devtool.js
chrome.runtime.onMessage.addListener(function(request){
if(request.name=="xyz")
{
alert("HI");
}
});
background.js
chrome.runtime.sendMessage({
name : "xyz"
});
有人能帮助?
根据此[论坛](https://groups.google.com/forum/#!msg/google-chrome-developer-tools/Q3DbtWRlgLo/kEYPaW8h0sgJ),您不能使用chrome.extension.sendMessage ()'将消息发送到在DevTools环境中运行的扩展页面。从背景页面到DevTools的最佳沟通方式是使用'chrome.extension.connect'在DevTools页面中打开一个端口,稍后在后台页面中使用port.postMessage()。 – abielita
@abielita _请将该评论写入一个答案(但请注意'extension'应该由'runtime'替换) – Xan