Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving which tabs are open in Chrome?

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".

like image 950
Ray Avatar asked Aug 11 '12 14:08

Ray


People also ask

How do I see previously opened tabs in Chrome?

Click on the Chrome menu. Select History. You will find the number of tabs under recently closed. Click on them for restoring all tabs.

How do I restore recent tabs in Chrome?

Open the Chrome menu (click the 3-dot menu in the upper-right corner of Chrome) Click History. Click # Tabs results to restore all the closed tabs from your session at once.


2 Answers

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.

like image 57
bpatel Avatar answered Oct 21 '22 20:10

bpatel


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

like image 5
Oliver Spryn Avatar answered Oct 21 '22 20:10

Oliver Spryn