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

- 15,848
- 18
- 99
- 158

- 43,506
- 91
- 209
- 269
-
1Can you please update the chosen answer? The API is now deprecated. – Andrew Rondeau Dec 18 '18 at 20:23
4 Answers
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.

- 55,884
- 29
- 169
- 223

- 823
- 1
- 6
- 5
-
3but 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
-
@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
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 ;)

- 10,389
- 6
- 32
- 44
-
You might want to add to check for the element he can send document.getElementById('item') != null and save that somewhere and retrieve it to determine if the element exists on the page. – Jesus Ramos Mar 23 '11 at 17:32
-
Haha no problem, typing kinda slow today and had to refer to my extension to make sure what I was writing was correct. – Jesus Ramos Mar 23 '11 at 17:34
-
6The correct answer should be changed to http://stackoverflow.com/a/15281976/257319 – Jul 14 '15 at 17:13
-
-
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.

- 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
-
1My comment above is for Manifest V2. It may work fine in Manifest V3. – Jeff Baker Jul 11 '22 at 23:32
-
1
This is a not deprecated vanilla way (may 2019):
chrome.tabs.query({}, function(tabs){
tabs.forEach(tb => {
chrome.tabs.sendMessage(tb.id, { action: "xxx" });
});
});

- 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