0

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"});
        });
    }
});

1 Answer 1

1

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"});
      }
    });
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.