3

Is there some way to get the tab id's of only the tabs that are part of my extension?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • 2
    What do you mean "part of my extension"? You could just keep track of tabs your extension creates as you create them. – Jason Hall Feb 24 '12 at 03:03

3 Answers3

4
myTabs[i].location.reload()

makes error:

Uncaught TypeError: Cannot call method 'reload' of undefined

but this code:

chrome.tabs.executeScript(myTabs[i].id, {code:"document.location.reload(true);"});

works. But better to use correct method:

chrome.tabs.reload(myTabs[i].id)
Veronika
  • 75
  • 6
  • 1
    In your answer, you're referring to `myTabs` from the accepted answer. Please note that the answerer (confusingly) uses the same name ( `myTabs` ) for different collections. `myTabs` from `chrome.tabs.query` returns a [Tab type](http://developer.chrome.com/extensions/tabs.html#type-Tab), which does not have a direct reference to the `window` object. Your answer is correct for this case. At the bottom of the other answer, `myTabs` is an array of `window` objects (returned from `chrome.extension.getViews`). The last code snippet is only applicable to the last `myTabs` object. – Rob W Oct 17 '12 at 19:36
1

This really depends on what you mean by "part of my extension".

If you mean tabs that are displaying a page that is contained within your extension you can do the following;

chrome.tabs.query({}, function (tabs) {
  var myTabs = [];
  for (var i = 0; i < tabs.length; i++) {
    if (tabs[i].url.indexOf(chrome.extension.getURL('')) === 0) {
      myTabs.push(tabs[i].id);
    }
  }
  console.log(myTabs);
});

If you want to access the DOM of your tabs instead, it gets even easier;

var myTabs = chrome.extension.getViews({type: 'tab'});

With access to the DOM you can simply iterate of each view (DOMWindow) and refresh each page;

for (var i = 0; i < myTabs.length; i++) {
  myTabs[i].location.reload()
}
neocotic
  • 2,131
  • 18
  • 18
0

I've tried to do the same thing and using chrome.tabs.query and chrome.tabs.get didn't work for me. This worked perfectly:

var data = chrome.extension.getViews({'type':'tab'});
$.each(data, function(k,v) {
    v.window.location.reload();
});
yutictodiyo8fi
  • 101
  • 1
  • 4