如何从loadContext获取TabID

时间:2016-08-31 07:34:09

标签: firefox firefox-addon firefox-addon-sdk nightly-build

我正在使用Firefox Nightly ver 51.0a1(2016-08-16) 并尝试该代码从http请求获取TabID

Cu.import('resource://gre/modules/Services.jsm');

var httpObs = {
    observe: function (aSubject, aTopic, aData) {
        if (aTopic == 'http-on-modify-request') {
            /*start - do not edit here*/
            var oHttp = aSubject.QueryInterface(Ci.nsIHttpChannel); //i used nsIHttpChannel but i guess you can use nsIChannel, im not sure why though
            var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
            //var DOMWindow = interfaceRequestor.getInterface(Ci.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below
            var loadContext;
            try { 
                loadContext = aSubject.QueryInterface(Ci.nsIChannel) // aSubject is equivalent to aSubject from observe
                                      .notificationCallbacks 
                                      .getInterface(Ci.nsILoadContext);
            } catch (ex) { 
                try { 
                    loadContext = aSubject.loadGroup.notificationCallbacks 
                                          .getInterface(Ci.nsILoadContext); 
                } catch (ex) {
                    loadContext = null;
                }
            }
            /*end do not edit here*/
            /*start - do all your edits below here*/
            var url = oHttp.URI.spec; //can get url without needing loadContext
            if(loadContext){
                var window = loadContext.associatedWindow;
                 var utab = utabs.getTabForContentWindow(window);
                 console.log(utabs.getTabId(utab));
            }

        }
    }
};
Services.obs.addObserver(httpObs, 'http-on-modify-request', false);

我从Code_snippets

得到它

加载标签时的结果:

    //When i load tab -5-1
    no result...
    //When i load tab -5-2
    console.log: test: -5-2
...
    console.log: test: -5-2
    //When i load tab -5-3 ... 
    no result

这里发生了什么以及如何解决?

0 个答案:

没有答案
相关问题