Finding a tab related to the DOM window

I am adding some new features to the Firefox extension, TryAgain , which intercepts HTTP error codes (e.g. 500) and automatically retries loading after some interval.

The code trap works fine, and I am trying to calculate the total number of attempts and store this in a tab using the session store . Unfortunately, right now I get a link to the DOM window (via interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow) ), but I need a link to the tab, which is nsIDOMNode by nsISessionStore docs to setTabValue () .

What I had so far (I disabled the actual retry from this example):

 // This function implements the nsIObserverService interface and observes // the status of all HTTP channels observe : function(aSubject, aTopic, aData) { var httpChannel = aSubject .QueryInterface(Components.interfaces.nsIHttpChannel); if (httpChannel.responseStatus == 500) { var domWindow; try { var notificationCallbacks; if (httpChannel.notificationCallbacks) { notificationCallbacks = httpChannel.notificationCallbacks; } else { notificationCallbacks = aSubject.loadGroup .notificationCallbacks; } var interfaceRequestor = notificationCallbacks .QueryInterface(Components.interfaces .nsIInterfaceRequestor); domWindow = interfaceRequestor .getInterface(Components.interfaces.nsIDOMWindow); } catch (e) { // No window associated with this channel return; } var ss = Components.classes["@mozilla.org/browser/sessionstore;1"] .getService(Components.interfaces.nsISessionStore); ss.setTabValue(domWindow, "foo", "bar"); } }, 

This, of course, does not work on setTabValue with an invalid parameter. How can I get the tab associated with the DOM window?

As an alternative solution, can I somehow store the variables associated with the DOM window, so that I do not need to clear the memory myself?

+7
source share
2 answers

domWindow is a content window, first you need to get a chrome window containing it. This is done using this rather ugly code:

 var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) .getInterface(Ci.nsIWebNavigation) .QueryInterface(Ci.nsIDocShellTreeItem) .rootTreeItem .QueryInterface(Ci.nsIInterfaceRequestor) .getInterface(Ci.nsIDOMWindow); 

Then you want to ask the <tabbrowser> element about the tab (note that you have to go through domWindow.top because domWindow may not be the top frame):

 var browser = chromeWindow.gBrowser.getBrowserForDocument(domWindow.top.document); 

Note that this is a <browser> element, not a linked <tab> (for the latter, you will need to getBrowserIndexForDocument , and then look at gBrowser.tabs[index] ). But I think that you just want to save the property for this tab, do you keep this property in all sessions? Then you can use the expando property:

 if (!("_myExtensionErrorCount" in browser)) browser._myExtensionErrorCount = 0; browser._myExtensionErrorCount++; 

Here _myExtensionErrorCount must be a unique name to avoid conflicts with other extensions, which can also use the expando properties.

+6
source

The nsISessionStore APIs that reference the nsIDOMNode aTab require you to hold the node tab while saving data. If you work in the overlay of the .xul browser and want to display the current tab, it is as simple as gBrowser.selectedTab. If you just keep a link to some content document in the chrome overlay window, here's how you find the corresponding tab:

 function tabFromDoc(doc) { var no = gBrowser.getBrowserIndexForDocument(doc); return gBrowser.tabContainer.childNodes[no]; } // example use: Cc['@mozilla.org/browser/sessionstore;1'] .getService(Ci.nsISessionStore) .setTabValue(tabFromDoc(myContentDoc), 'myKey', 'myValue'); 
+4
source

All Articles