问题
My extension is sending the tab URL's of all tabs in the window when I press a button. For some reason it's only sending one message, only the last tab's URL is received. What I wish to do is create a list of all the URL's of each tab. So for instance if one had two tabs 'google.ca' and 'bing.ca' I would be able to gather this information and ultimately list both URL's. At the moment I am only getting back one URL though, which in this example would be bing.ca.
background.js
chrome.browserAction.onClicked.addListener(buttonClicked)
function buttonClicked(){
chrome.tabs.query({currentWindow:true},function(tabs){ //currentWindow:true ensures all tabs
tabs.forEach(function(tab){
chrome.tabs.sendMessage(tab.id,tab.url);
});
});
}
sketch.js
chrome.runtime.onMessage.addListener(gotMessage);
var messagesReceived = 0; //even tracking how many messages are received manually and this increments
//only once
function gotMessage(message,sender,sendResponse){
console.log("\n");
console.log(message);
messagesReceived += 1;
console.log(messagesReceived);
}
来源:https://stackoverflow.com/questions/59558219/sending-information-regarding-all-tabs-but-listener-only-working-once-and-displa