5
votes

I am trying to associate an XMLHttpRequest with a tab on the browser using the following code:

function getBrowserFromChannel(aChannel) {
    var notificationCallbacks = 
        aChannel.notificationCallbacks ? 
                    aChannel.notificationCallbacks :
                    aChannel.loadGroup.notificationCallbacks;

    if (!notificationCallbacks) {
        console.log("no callbacks");
        return (0);
    }
    var loadContext = notificationCallbacks.getInterface(Ci.nsILoadContext);

getInterface(Ci.nsILoadContext) fails with: "Component does not have requested interface"

Any idea how else I can get the browser?

Thanks

1

1 Answers

1
votes

Try this code (from Lightbeam):

function getLoadContext(aRequest) {
    try {
        // first try the notification callbacks
        var loadContext = aRequest.QueryInterface(Ci.nsIChannel)
            .notificationCallbacks.getInterface(Ci.nsILoadContext);
        return loadContext;
    } catch (ex) {
        // fail over to trying the load group
        try {
            if (!aRequest.loadGroup) return null;

            var loadContext = aRequest.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
            return loadContext;
        } catch (ex) {
            return null;
        }
    }
}

Note the license is MPL 1.1/GPL 2.0/LGPL 2.1