0

모든 프레임에 콘텐츠 스크립트를 삽입했습니다. 백그라운드에서 요청을 보내고 모든 콘텐츠 스크립트 (삽입 된 프레임)의 응답을 수신하고 싶습니다.콘텐츠 스크립트와 배경 페이지 간 메시지 전달

현재 하나의 응답 만받을 수 있습니다. 모든 콘텐츠 스크립트의 응답을받는 방법은 무엇입니까?

콘텐츠 스크립트

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

답변

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"}); 
     } 
    }); 
    } 
}); 
관련 문제