仅在200状态代码的响应中,http-on-examine-response上的Firefox扩展列表

时间:2013-04-19 19:35:10

标签: firefox firefox-addon

我有一个观察者在http-on-examine-response上定义为

httpRequestObserver = {

observe: function(request, aTopic, aData){

        if (aTopic == "http-on-examine-response") {
             request.QueryInterface(Ci.nsIHttpChannel);

             var newListener = new TracingListener();
             request.QueryInterface(Ci.nsITraceableChannel);

             newListener.originalListener = request.setNewListener(newListener);

    }
}, 
QueryInterface: function(aIID){
    if (typeof Cc == "undefined") {
        var Cc = components.classes;
    }
    if (typeof Ci == "undefined") {
        var Ci = components.interfaces;
    }
    if (aIID.equals(Ci.nsIObserver) ||
    aIID.equals(Ci.nsISupports)) {
        return this;
    }

    throw components.results.NS_NOINTERFACE;

},
};

我想只在那些响应的响应代码是200的套件中添加监听器。基本上我想要完成的是避免3xx响应。但是我无法从头部读取ResponseCode。如何获得响应代码在这里。

提前感谢您的帮助...

1 个答案:

答案 0 :(得分:1)

您可以使用httpChannel.responseStatus,此处为document

因此,请将observe功能更改为

observe: function(request, aTopic, aData){
        if (aTopic !== 'http-on-examine-response') {
            return;
        }
        let httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
        if (httpChannel.responseStatus !== 200) {
            return;
        }

        // do your stuff...
        let traceableChannel = event.subject.QueryInterface(Ci.nsITraceableChannel);
        let newListener = new TracingListener();
        newListener.originalListener = traceableChannel.setNewListener(newListener);

    }
},