12

Is there a way to retrieve all the tabs open and sort them in to an array in Chrome? So if Gmail and YouTube were open, there would be two entries in the array entitled "gmail.com" and "youtube.com".

Zirak
  • 38,920
  • 13
  • 81
  • 92
Ray
  • 289
  • 1
  • 2
  • 13

2 Answers2

11

Yes, here is how you can do this:

Note: this requires permission "tabs" to be specified in your manifest file.

chrome.windows.getAll({populate:true}, getAllOpenWindows);

function getAllOpenWindows(winData) {

  var tabs = [];
  for (var i in winData) {
    if (winData[i].focused === true) {
        var winTabs = winData[i].tabs;
        var totTabs = winTabs.length;
        for (var j=0; j<totTabs;j++) {
          tabs.push(winTabs[j].url);
        }
    }
  }
  console.log(tabs);
}

In this example I am just adding tab url as you asked in an array but each "tab" object contains a lot more information. Url will be the full URL you can apply some regular expression to extract the domain names from the URL.

bpatel
  • 381
  • 1
  • 4
5

Unless you are building a plugin, there isn't a way that I know of to retrieve all of the names of the open tabs, especially if the tabs contain content from separate domains. If you were able to do such a thing, it could be quite a security issue!

You can check the Chrome documentation here: http://developer.chrome.com/extensions/devguide.html

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • I am building a Chrome plugin. – Ray Aug 11 '12 at 15:31
  • It's bad news since I'm trying to migrate all tabs from Chrome to Firefox manually (the other direction is straight forward since I can make Firefox run any script at chrome:// level). – lilydjwg Nov 22 '15 at 09:18