简体   繁体   中英

Firefox addon: Getting tab from XMLHttpRequest

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM