0

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

Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
directedition
  • 11,145
  • 18
  • 58
  • 79

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
  • 8,414
  • 4
  • 39
  • 69
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
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,246
  • 2
  • 43
  • 64