我正在使用 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);
当我加载标签时结果:
//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
这里发生了什么以及如何解决它?