Andrew
Andrew

Reputation: 1575

is it possible to get amount of tabs opened in Firefox browser?

I want to iterate over opened tabs and do specific tasks. Is there a way to get the amount of opened tabs?

Upvotes: 1

Views: 1209

Answers (2)

Dexter
Dexter

Reputation: 2480

This is the way that Firefox currently uses to count opened windows and tabs, for telemetry:

function getOpenTabsAndWinsCounts() {
  let tabCount = 0;
  let winCount = 0;

  for (let win of Services.wm.getEnumerator("navigator:browser")) {
    winCount++;
    tabCount += win.gBrowser.tabs.length;
  }

  return { tabCount, winCount };
}

Please note how it iterates through the results of Services.wm.getEnumerator("navigator:browser"), to capture the numbers of all the different opened windows.

Depending on where you want to use the script, you might need to use var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator) rather than Services.wm, as suggested by @Shugar.

Upvotes: 2

Shugar
Shugar

Reputation: 5299

If you need a js-script, I hope the following code should be helpful:

var wM = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var numberOfTabs = wM.getMostRecentWindow("navigator:browser").gBrowser.browsers.length;

Upvotes: 0

Related Questions