Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - Finding the tab associated with a DOM window

I'm adding some new functionality in a Firefox extension, TryAgain, that traps HTTP error codes (e.g. 500) and automatically retries loading the page after some interval.

Trapping the codes works excellently, and I'm trying to tally the total number of retries and store this in the tab using Session Store. Unfortunately, right now I'm getting a reference to a DOM window (through interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow)), but I need a reference to a tab, which is a nsIDOMNode as per nsISessionStore docs on setTabValue().

What I have so far (I've snipped the actual retrying out of 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 fails on setTabValue with an invalid parameter. How can I get the tab associated with the DOM window?

As an alternative solution, can I store variables associated with a DOM window in some way such that I don't have to clean up the memory myself?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

domWindow is a content window, first you need to get the chrome window containing it. That's done by 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 should pass in domWindow.top because domWindow might not be the top frame):

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

Note that this is the <browser> element, not the associated <tab> (for the latter you would need getBrowserIndexForDocument and then look at gBrowser.tabs[index]). But I think you simply want to store a property for this tab, not have this property persist across sessions? Then you can use an expando property:

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

Here _myExtensionErrorCount should be a name that is unique enough to avoid conflicts with other extensions that might want to use expando properties as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...