Sending information regarding all tabs but listener only working once and displays final tab's URL

扶醉桌前 提交于 2021-02-11 13:50:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!