Search code examples
google-chromegoogle-chrome-extensionbrowser-tab

Reload Chrome extension tabs


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


Solution

  • 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()
    }