views:

18

answers:

1

In a firefox extension I'm temporarily saving data in javascript to show in a sidebar. When a tab is selected, I want to change the text in the sidebar to the right data. What is the best object to use as the "Identifier" for each tab? I'm trying to use the tab object. However, I'm stuck then on how to associate the data to the tab. Currently, I get a load event on the document in a tab. Then I process the data in the document. AFter showing the new data in the tab, I want to save it into a map with the tab as the key so that I can retrieve it on a SelectTab event. However, I'm not sure how to find the tab from the documentin the load event.

So please either tell me how to get the tab object from the document object, or a better thing to use as the key.

The following is one way I've found, but I'm sure there is something more elegant.

getTabForDocument : function (document) {
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                     .getService(Components.interfaces.nsIWindowMediator);
  for (var found = false, index = 0, tabbrowser = wm.getEnumerator('navigator:browser').getNext().gBrowser;
       index < tabbrowser.tabContainer.childNodes.length && !found;
       index++) {

    // Get the next tab
    var currentTab = tabbrowser.tabContainer.childNodes[index];

        if (currentTab.linkedBrowser.contentWindow.document == document)
    {
        return currentTab;
    }
  }
  return null;
},

Thanks...

A: 

Take a look at: https://developer.mozilla.org/en/Code_snippets/Tabbed_browser, especially the gBrowser.selectedBrowser section and https://developer.mozilla.org/En/Listening_to_events_on_all_tabs

To associate data with a tab you can use the setUserData method on the browser.

To get the browser for a document use the getBrowserForDocument method on the gBrowser global object.

Fábio
This helps on the storage question. Do you also have a way to easily get the browser from the document?
Andrew
I edited my answer with information on how to do that.
Fábio