Created
July 21, 2014 03:18
-
-
Save Noitidart/644494bdc26f996739ef to your computer and use it in GitHub Desktop.
_ff-addon-snippet-LoadContextAndGoodies - Gets the nsILoadContext from nsIRequest. Also returns some common stuff like nsIDOMWindow, contentWindow, gBrowser, tab, and browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadContextAndGoodies(request, return_goodies) { | |
var loadContext = null; | |
if (request instanceof Ci.nsIRequest) { | |
try { | |
if (request.loadGroup && request.loadGroup.notificationCallbacks) { | |
loadContext = request.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext); | |
} | |
} catch (ex) { | |
console.exception('request loadGroup with notificationCallbacks but oculd not get nsIloadContext', ex); | |
} | |
if (!loadContext) { | |
try { | |
if (request.notificationCallbacks) { | |
loadContext = request.notificationCallbacks.getInterface(Ci.nsILoadContext); | |
} | |
} catch (ex) { | |
console.exception('request has notificationCallbacks but could not get nsILoadContext', ex); | |
/* start - noit's backup try, it might be redundant (im not sure) as Wladamir Palant didn't have this way*/ | |
try { | |
var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor); | |
loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext); | |
} catch (ex) { | |
console.exception('backup method failed:' ex); | |
} | |
/* end - my backup try, it might be redundant as Wladamir Palant didn't have this way*/ | |
} | |
} | |
} else { | |
console.warn('request argument is not instance of nsIRequest') | |
} | |
if (return_goodies) { | |
if (!loadContext) { | |
return null; | |
} | |
var contentWindow = loadContext.associatedWindow; | |
var DOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIWebNavigation) | |
.QueryInterface(Ci.nsIDocShellTreeItem) | |
.rootTreeItem | |
.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIDOMWindow); | |
var gBrowser = DOMWindow.gBrowser; | |
if (gBrowser) { | |
var tab = gBrowser._getTabForContentWindow(contentWindow.top); | |
var browser = tab.linkedBrowser; | |
} else { | |
var tab, browser = null; | |
} | |
var goodies = { | |
loadContext: loadContext, | |
DOMWindow: DOMWindow, | |
gBrowser: gBrowser, | |
contentWindow: contentWindow, | |
browser: browser, | |
tab: tab | |
}; | |
return goodies; | |
} else { | |
return loadContext; | |
} | |
} |
Nice way to detect if in private mode:
isRequestFromPrivateWindow: function (subject, httpChannel) {
"use strict";
var loadContext, interfaceRequestor, isPrivate = false, contentWindow;
try {
interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
try {
loadContext = interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);
} catch (ex) {
try {
loadContext = subject.loadGroup.notificationCallbacks.getInterface(Components.interfaces.nsILoadContext);
} catch (ex1) {}
}
if (loadContext) {
isPrivate = loadContext.usePrivateBrowsing;
}
} catch (ex2) {}
return isPrivate;
}
From https://pastebin.mozilla.org/8823253 by user @Devuct off of IRC #extdev
https://gist.github.com/intika/503ab65f457dc20bb77b/revisions
Just fixed a little bug... we could have a request without tab resulting in tab being null
(DOMWindow.gBrowser exist but not tab for some requests)
Way cool @intika thanks for that catch. I had to update your catch though:
if (gBrowser && gBrowser._getTabForContentWindow(contentWindow.top)) {
cuz if gBrowser is undefined or null then doing gBrowser.anything will cuase error :)
yeah that was my first edit i thought it was not necessary to make both tests :)
Possibly better version:
function getLoadContext(request) {
var loadContext = null;
if (request instanceof Ci.nsIRequest) {
try {
if (request.loadGroup && request.loadGroup.notificationCallbacks) {
loadContext = request.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
}
} catch (ex) {
console.exception('request loadGroup with notificationCallbacks but oculd not get nsIloadContext', ex);
try {
if (request.notificationCallbacks) {
loadContext = request.notificationCallbacks.getInterface(Ci.nsILoadContext);
}
} catch (ex) {
console.exception('request has notificationCallbacks but could not get nsILoadContext', ex);
/* start - noit's backup try, it might be redundant (im not sure) as Wladamir Palant didn't have this way*/
try {
var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
} catch (ex) {
console.exception('backup method failed:', ex); // fixed on aug 14 2015
}
/* end - my backup try, it might be redundant as Wladamir Palant didn't have this way*/
}
}
} else {
console.warn('request argument is not instance of nsIRequest')
}
return loadContext;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Rev1
nsILoadContext
nsIDOMWindow
fromloadContext.associatedWindow
(which is acontentWindow
)var interfaceRequestor
, I implemented this above as a backup as it might be redundant.