36

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

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Skizit
  • 43,506
  • 91
  • 209
  • 269

4 Answers4

76

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.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 3
    but how does this iterate over the tabs? Is there a length method for `tabs`? Most API methods take a single tab or tab property, so you'd need to index them with a variable like `tabs[0]...tabs[n]`. How to get n? – jiggunjer Feb 07 '16 at 09:05
  • 2
    @jiggunjer, n = tabs.length, tabs - is an array – Vlas Bashynskyi Jul 02 '16 at 08:54
  • @Golden, What happens if you put the first arg as `null` instead of `{}`? – Pacerier Jul 26 '17 at 12:00
  • the new API reference URL is https://developer.chrome.com/docs/extensions/reference/tabs/#method-query – mark Nov 16 '22 at 21:57
23

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

Sindar
  • 10,389
  • 6
  • 32
  • 44
8

Edit July 2022:

This answer was originally written for MV3 users. If you're looking for a MV2 answer, see above. Otherwise, this answer will work perfectly fine with MV3.

Original answer

chrome.tabs.query is the function you're looking for. You can pass in parameters to a object to filter the tabs. In your case, you want to iterate over all the open tabs. Here's two versions of the code you're looking for:

Synchronous

chrome.tabs.query({}, function(tabs) {
  tabs.forEach(function (tab) {
    // do whatever you want with the tab
  });
});

Asynchronous

var tabs = await chrome.tabs.query({});
tabs.forEach(function (tab) {
  // do whatever you want with the tab
});

In both cases, the parameter tab is a Tab.

Nicolas
  • 135
  • 1
  • 6
  • var tabs = await chrome.tabs.query({}); does not work. It says, "Uncaught TypeError: Error in invocation of tabs.query(object queryInfo, function callback): No matching signature." – Jeff Baker Jul 11 '22 at 22:28
  • 1
    My comment above is for Manifest V2. It may work fine in Manifest V3. – Jeff Baker Jul 11 '22 at 23:32
  • 1
    I've updated the answer with relevant information for MV2 users! – Nicolas Jul 14 '22 at 01:29
2

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

chrome.tabs.query({}, function(tabs){
        tabs.forEach(tb => {
            chrome.tabs.sendMessage(tb.id, { action: "xxx" });
        });
    });
Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • Maybe this worked back in May, but as of now, in October, it seems like you have to iterate through both the `{ highlighted: true }`, as in, the focused tab, and `{ hightlighted: false }` - all the other ones - to really go through them all. – Tiramonium Oct 18 '19 at 20:11