Mozilla Firefox Extension Code Snippets

The Browser and Tabbed Browser Concepts


See the Mozilla page for the concepts: The browser (

Iterate Through the Open Pages


Using a window mediator, all the pages in their own windows or in a tab in any window can be accessed. There is the Mozilla concept page, and the Mozilla reference page. Below is the code example.

var enumtr = windowMediator.getEnumerator(null); /* null type for all windows */
while (enumtr.hasMoreElements()) {
  var win = enumtr.getNext();
  var loc = win.document.location.toString();

  /* in chrome://browser/content/browser.xul, access user document */
  if (\/\/browser\/content\/browser\.xul/) == 0) {
    var tabs = win.gBrowser.tabContainer.tabbrowser.browsers;

    // Check each tab of this browser instance
    var ntabs = tabs.length;
    for (var i = 0; i < ntabs; i++) {
      var currentBrowser = tabs[i];
      log("tab " + i + " : " + currentBrowser.currentURI.spec);

      var doc = currentBrowser.contentDocument;
      log(" tab " + i + " : " + doc.location.toString());
    } //for
  } //if
} //while

If an extension defines a global variable, e.g. “myextension”, it will be one instance per window. With the code above, once you get the window, you can access each of the extension variables by “win.myextension” in the enumeration loop. All the tabs under a window will access the same instance using just the “myextension” name. But the instances in other windows are also accessible to any tab. An idea is at the document loading time (browser window loading time, not the user content loading time), use the window mediator enumeration loop to hook cross-references to existing windows and build a mesh of them, in the way much like the Ethernet switches vote a root node and forward traffic through a spanning tree. Of course, once any window is closed, they need to re-vote a new root window. With this mesh (or tree or any other structure) of user extension variables, any user page can be accessed.

The above code shows to access tabs following the exact window-to-tab structure. The tabs can also be accessed by searching for “tabbrowser” elements under a window.

  // this search will find the current tab content:
  var list = win.document.getElementsByTagName('tabbrowser');
  log("win tabs: " + list.length);
  if (list.length > 0) {
    var doc = list[0].contentDocument;
    log(s + " win tab0 " + doc.location.toString());

2 Responses to Mozilla Firefox Extension Code Snippets

  1. Pingback: Firefox Extension Javascript Access Local File | minghuasweblog

  2. Pingback: Technical Topics | minghuasweblog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s