chrome.runtime.onMessage.addListener sendResponse

发布时间 2023-06-27 23:39:40作者: hrdom

If multiple pages are listening for onMessage events, only the first to call sendResponse() for a particular event will succeed in sending the response. All other responses to that event will be ignored.

如果多个页面正在监听onMessage事件,则只有第一个为特定事件调用sendResponse()的页面才能成功发送响应。对该事件的所有其他响应将被忽略。

 

上面的意思我猜测大概是:

chrome.tabs.sendMessage(tab.id, { from: "popup", }, callback);
这个tab里有多个frame(iframe),这里的“multiple pages”,应该是指的这个tab里的“multiple frames”,我不知道为什么要用pages这个词,很令人困惑。devtool中source页确实也是有page这项
如果有多个frames,background.js中的callback只接收第一个frame调用的sendResponse(xxx)中的xxx,后边其它frames即使调用sendResponse了,background.js中的callback也不会处理了