0

In a firefox extension, how do you enumerate the current window's tabs and retrieve their URLs?

Cheekysoft
  • 34,284
  • 20
  • 71
  • 85
directedition
  • 10,495
  • 17
  • 57
  • 78

2 Answers2

4

There's a code snippet at MDC that does exactly that:

var num = gBrowser.browsers.length;
for (var i = 0; i < num; i++) {
  var b = gBrowser.getBrowserAtIndex(i);
  try {
    dump(b.currentURI.spec); // dump URLs of all open tabs to console
  } catch(e) {
    Components.utils.reportError(e);
  }
}
Richard Neish
  • 7,824
  • 4
  • 34
  • 68
Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
1

When using Firefox SDK, see this:
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/List_Open_Tabs

var tabs = require("sdk/tabs");
for (let tab of tabs)
    console.log(tab.url);

Additionally, the tabs object seems to have array interface, so you can use also the .length property:

var tabs = require("sdk/tabs");
for (var i = 0; i < tabs.length; i++)
    console.log(tabs[i].url);
Roland Pihlakas
  • 4,008
  • 2
  • 38
  • 62