Question

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

Was it helpful?

Solution

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);
  }
}

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top