In a firefox extension, how do you enumerate the current window's tabs and retrieve their URLs?
Asked
Active
Viewed 4,144 times
2 Answers
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
-
downvote, because you should add the code snippet to your answer. – baris1892 Jun 11 '18 at 09:38
-
Link is dead. This is exactly why we shouldn't do link only answers. – JoL Sep 02 '18 at 11:38
-
fixed the link for now. – eMBee May 31 '19 at 11:20
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