Chrome-Extension: iterate through all tabs?

谁说胖子不能爱 提交于 2019-12-04 08:54:27

问题


How would I iterate through all tabs a user has open and then check if they have a particular HTML item with id = 'item'?


回答1:


You can make it like this :

chrome.tabs.getAllInWindow(null, function(tabs){
    for (var i = 0; i < tabs.length; i++) {
    chrome.tabs.sendRequest(tabs[i].id, { action: "xxx" });                         
    }
});

After that to look after your item, if you can make it like this :

document.getElementById('item')

Don't forget that you can't manipulate the HTML by using the "background page" So the first code snip is for the background page, and the second have to be on a content script ;)




回答2:


It appears this method has been deprecated in favor of chrome.tabs.query:

http://developer.chrome.com/extensions/tabs.html#method-query

So now you'd want to do:

chrome.tabs.query({}, function(tabs) { /* blah */ } );

Passing an empty queryInfo parameter would return all of the tabs.




回答3:


This is a not deprecated vanilla way (may 2019):

chrome.tabs.query({}, function(tabs){
        tabs.forEach(tb => {
            chrome.tabs.sendMessage(tb.id, { action: "xxx" });
        });
    });



回答4:


I use this one

chrome.tabs.getAllInWindow(null, function(tabs) {
      $.each(tabs, function() {
        // u can use 'this.id' to work with evey tab 
      });
});


来源:https://stackoverflow.com/questions/5409242/chrome-extension-iterate-through-all-tabs

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