Firefox插件:从XMLHttpRequest获取选项卡

时间:2014-01-13 01:52:50

标签: javascript firefox firefox-addon firefox-addon-sdk

我正在尝试使用以下代码将XMLHttpRequest与浏览器上的标签相关联:

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)失败,并显示:“组件没有请求的接口”

知道我还能拿到浏览器吗?

由于

1 个答案:

答案 0 :(得分:1)

尝试此代码(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;
        }
    }
}

注意许可证是MPL 1.1 / GPL 2.0 / LGPL 2.1