Enumerating tabbrowser tabs: Difference between revisions
From MozillaZine Knowledge Base
Jump to navigationJump to search
m ("l" looks like "1", replace with "num") |
m (don't swallow the exception) |
||
Line 13: | Line 13: | ||
try { | try { | ||
dump(b.currentURI.spec); // dump URLs of all open tabs to console | dump(b.currentURI.spec); // dump URLs of all open tabs to console | ||
} catch(e) {} | } catch(e) { | ||
Components.utils.reportError(e); | |||
} | |||
}</pre> | }</pre> | ||
</li> | </li> |
Revision as of 00:55, 11 March 2006
This page is part of the extension development documentation project.
Ask your questions in MozillaZine Forums. Also try browsing example code.
Note: development documentation is in process of being moved to Mozilla Development Center (MDC).
- To go through all open tabs in a browser, you first need to get a reference to browser's window. If your code is executed from a
browser.xul
(in Firefox) overlay (for example it is a toolbar button or menu click handler), you can access current window viawindow
pre-defined variable. If your code is executed from its own window (for example, Settings dialog), you can usensIWindowMediator
to get it. - Then you need to get the
<tabbrowser>
element. You can get it withwin.gBrowser
, wherewin
is the browser's window from the previous step. You can use justgBrowser
instead ofwindow.gBrowser
, if running frombrowser.xul
overlay. - Now use
gBrowser.mPanelContainer.childNodes.length
to get the number of open tabs. Then usegBrowser.getBrowserAtIndex()
to get a<browser>
element. For example:var num = gBrowser.mPanelContainer.childNodes.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); } }
To learn what methods are available for <browser>
and <tabbrowser>
elements, use DOM Inspector or look in browser.xml and tabbrowser.xml for corresponding XBL bindings.