[英]Message passing between content scripts and background page
I have injected content scripts to all frames. 我已将内容脚本注入所有框架。 I sent a request from background and would like to receive response from all the content scripts (frames that have been injected). 我从后台发送了一个请求,希望收到所有内容脚本(已注入的帧)的响应。
Currently I can only receive one response, how do I receive responses from all content scripts? 目前,我只能收到一个回复,如何接收所有内容脚本的回复?
content script: 内容脚本:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.bgReq == "windowInfo")
alert("bgreq received : "+ window.location.host);
});
background script: 后台脚本:
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"});
});
}
});
You need to explicitly send it to all tabs in all the windows : 您需要将其显式发送到所有窗口的所有选项卡中:
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"});
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.