javascript - Message passing between content scripts and background page -
i have injected content scripts frames. sent request background , receive response content scripts (frames have been injected).
currently can receive 1 response, how receive responses 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 content scritps chrome.tabs.query({active: true, currentwindow: true}, function(tabs) { chrome.tabs.sendmessage(tabs[0].id, {bgreq:"windowinfo"}); }); } });
you need explicitly send tabs in windows :
chrome.windows.getall({},function(windows){ for( var win in windows ){ chrome.tabs.getallinwindow(win.id, function(tabs) { (var in tabs) { chrome.tabs.sendmessage(tabs[0].id, {bgreq:"windowinfo"}); } }); } });