Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Noitidart/644494bdc26f996739ef to your computer and use it in GitHub Desktop.
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.
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;
}
}
@Noitidart
Copy link
Author

README

Rev1

@Noitidart
Copy link
Author

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

@intika
Copy link

intika commented Jun 30, 2015

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)

@Noitidart
Copy link
Author

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 :)

@intika
Copy link

intika commented Jul 1, 2015

yeah that was my first edit i thought it was not necessary to make both tests :)

@Noitidart
Copy link
Author

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