我已将内容脚本注入所有帧。我从后台发送了一个请求,并希望收到所有内容脚本(已注入的帧)的响应。
目前我只能收到一个回复,如何收到所有内容脚本的回复?
内容脚本:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.bgReq == "windowInfo")
alert("bgreq received : "+ window.location.host);
});
后台脚本:
chrome.runtime.onMessage.addListener(function(sentWords) {
if (sentWords.words == "injection") {
//send request to content scritps
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {bgReq:"windowInfo"});
});
}
});
答案 0 :(得分:1)
您需要将其显式发送到所有窗口中的所有选项卡:
chrome.windows.getAll({},function(windows){
for( var win in windows ){
chrome.tabs.getAllInWindow(win.id, function(tabs) {
for (var i in tabs) {
chrome.tabs.sendMessage(tabs[0].id, {bgReq:"windowInfo"});
}
});
}
});