Javascript NSiTracableChannel,拦截HTTP流量代码

Javascript NSiTracableChannel,拦截HTTP流量代码,javascript,firefox-addon,Javascript,Firefox Addon,我一直在努力让这段代码在firefox上运行,这段代码是我一次又一次从firefox上获得的,我一直在谷歌上搜索答案,但运气不好。 我想做的是截取某些链接,并更改它们。 欢迎任何帮助 我得到的错误是函数语句需要一个名称开关指向观察:函数(AsObject、aTopic、aData)代码的一部分 const Cc=Components.class; const Ci=组件。接口; var observerService=Cc[“@mozilla.org/observer service;1”]

我一直在努力让这段代码在firefox上运行,这段代码是我一次又一次从firefox上获得的,我一直在谷歌上搜索答案,但运气不好。 我想做的是截取某些链接,并更改它们。 欢迎任何帮助

我得到的错误是函数语句需要一个名称开关指向观察:函数(AsObject、aTopic、aData)代码的一部分

const Cc=Components.class;
const Ci=组件。接口;
var observerService=Cc[“@mozilla.org/observer service;1”]
.getService(Ci.nsIObserverService);
addObserver(httpRequestObserver,
“http检查响应”,错误);
removeObserver(httpRequestObserver,
“http检查响应”);
//--------------------------------------------------------
var httpRequestObserver=
{
观察:功能(对象、特应性、适应性)
{
if(aTopic==“http检查响应”)
{
}
},
QueryInterface:函数(aIID)
{
if(aIID.equals(Ci.nsi)||
aIID.equals(Ci.nsi))
{
归还这个;
}
抛出组件.results.NS\n接口;
}
}
//------------------------------------------------------------
函数TracingListener(){
this.originalListener=null;
}
TracingListener.prototype=
{
onDataAvailable:函数(请求、上下文、输入流、偏移量、计数){
this.originalListener.onDataAvailable(请求、上下文、输入流、偏移量、计数);
},
onStartRequest:函数(请求、上下文){
this.originalListener.onStartRequest(请求,上下文);
},
onStopRequest:函数(请求、上下文、状态代码){
this.originalListener.onStopRequest(请求、上下文、状态代码);
},
QueryInterface:函数(aIID){
if(aIID.equals(Ci.nsirstreamlistener)||
aIID.equals(Ci.nsi)){
归还这个;
}
抛出组件.results.NS\n接口;
}
}
观察:功能(对象、特应性、适应性)
{
if(aTopic==“http检查响应”){
var newListener=new tracingstener();
a对象查询接口(Ci.nsTraceableChannel);
newListener.originalListener=asobject.setNewListener(newListener);
}
}
AsObject=AsObject.QueryInterface(Ci.nsIChannel);
var uri=asobject.uri;
//在任何其他冲突解决程序步骤之前请求的原始URI
//和/或重定向。
var ouri=原始对象;
ch=Services.io.newChannel(“https://google.com/“,空,空);
log(ch.toString());
//“[xpconnect频道]”
ch.QueryInterface(Ci.NSTraceableChannel);
log(ch.toString());
//“[xpconnect-wrapped(nsISupports,nsIChannel,nsITraceableChannel)]”
log(Ci.nsIUploadChannel的ch实例);
//真的
log(ch.toString());
//“[xpconnect-wrapped(nsISupports,nsIChannel,nsITraceableChannel,nsIUploadChannel)]”

//此时已知变量“ch”实现了四个给定接口。
复制粘贴此变量,它可以工作:

// const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
// Cu.import('resource://gre/modules/Services.jsm');

Services.obs.addObserver(httpRequestObserver, "http-on-examine-response", false);

// Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response");
//--------------------------------------------------------

var httpRequestObserver = {
    observe: function(aSubject, aTopic, aData) {
        // if (aTopic == "http-on-examine-response") {} // no need for this, we added observer for `http-on-examine-response` so we know for sure this only triggers for `http-on-examine-response`
        var newListener = new TracingListener();
        aSubject.QueryInterface(Ci.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
    /* // what was the point of this block of code?
    ,
    QueryInterface: function(aIID) {
        if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports)) {
            return this;
        }
        throw Cr.NS_NOINTERFACE;
    }
    */
}

//------------------------------------------------------------

function TracingListener() {}
TracingListener.prototype = {
    onDataAvailable: function(request, context, inputStream, offset, count) {
        console.log('data available');
        this.originalListener.onDataAvailable(request, context, inputStream, offset, count);
    },
    onStartRequest: function(request, context) {
        this.originalListener.onStartRequest(request, context);
    },
    onStopRequest: function(request, context, statusCode) {
        this.originalListener.onStopRequest(request, context, statusCode);
    },
    QueryInterface: function(aIID) {
        if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) {
            return this;
        }
        throw Cr.NS_NOINTERFACE;
    }
}

// what the hell? this is not part of an object? why observe colon?
/*
observe: function(aSubject, aTopic, aData) {
    if (aTopic == "http-on-examine-response") {
        var newListener = new TracingListener();
        aSubject.QueryInterface(Ci.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
}
*/

// whats with all this junk? is it supposed to be in an observer??
/*
aSubject = aSubject.QueryInterface(Ci.nsIChannel);
var uri = aSubject.URI;
// original URI that was requested before any other resolver steps
// and/or redirects.
var ouri = aSubject.originalURI;

ch = Services.io.newChannel("https://google.com/", null, null);
console.log(ch.toString());
// "[xpconnect wrapped nsIChannel]"

ch.QueryInterface(Ci.nsITraceableChannel);
console.log(ch.toString());
// "[xpconnect wrapped (nsISupports, nsIChannel, nsITraceableChannel)]"

console.log(ch instanceof Ci.nsIUploadChannel);
// true
console.log(ch.toString());
// "[xpconnect wrapped (nsISupports, nsIChannel, nsITraceableChannel, nsIUploadChannel)]"
// the variable "ch" is known to implement the four given interfaces at this point.
*/
更新
有人问我如何用这个来获取响应源,所以我从中总结出一个要点:

复制粘贴这个它可以工作:

// const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
// Cu.import('resource://gre/modules/Services.jsm');

Services.obs.addObserver(httpRequestObserver, "http-on-examine-response", false);

// Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response");
//--------------------------------------------------------

var httpRequestObserver = {
    observe: function(aSubject, aTopic, aData) {
        // if (aTopic == "http-on-examine-response") {} // no need for this, we added observer for `http-on-examine-response` so we know for sure this only triggers for `http-on-examine-response`
        var newListener = new TracingListener();
        aSubject.QueryInterface(Ci.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
    /* // what was the point of this block of code?
    ,
    QueryInterface: function(aIID) {
        if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports)) {
            return this;
        }
        throw Cr.NS_NOINTERFACE;
    }
    */
}

//------------------------------------------------------------

function TracingListener() {}
TracingListener.prototype = {
    onDataAvailable: function(request, context, inputStream, offset, count) {
        console.log('data available');
        this.originalListener.onDataAvailable(request, context, inputStream, offset, count);
    },
    onStartRequest: function(request, context) {
        this.originalListener.onStartRequest(request, context);
    },
    onStopRequest: function(request, context, statusCode) {
        this.originalListener.onStopRequest(request, context, statusCode);
    },
    QueryInterface: function(aIID) {
        if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) {
            return this;
        }
        throw Cr.NS_NOINTERFACE;
    }
}

// what the hell? this is not part of an object? why observe colon?
/*
observe: function(aSubject, aTopic, aData) {
    if (aTopic == "http-on-examine-response") {
        var newListener = new TracingListener();
        aSubject.QueryInterface(Ci.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
}
*/

// whats with all this junk? is it supposed to be in an observer??
/*
aSubject = aSubject.QueryInterface(Ci.nsIChannel);
var uri = aSubject.URI;
// original URI that was requested before any other resolver steps
// and/or redirects.
var ouri = aSubject.originalURI;

ch = Services.io.newChannel("https://google.com/", null, null);
console.log(ch.toString());
// "[xpconnect wrapped nsIChannel]"

ch.QueryInterface(Ci.nsITraceableChannel);
console.log(ch.toString());
// "[xpconnect wrapped (nsISupports, nsIChannel, nsITraceableChannel)]"

console.log(ch instanceof Ci.nsIUploadChannel);
// true
console.log(ch.toString());
// "[xpconnect wrapped (nsISupports, nsIChannel, nsITraceableChannel, nsIUploadChannel)]"
// the variable "ch" is known to implement the four given interfaces at this point.
*/
更新
有人问我如何用它获取响应源,所以我从中得出了一个要点:

这是一个令人恶心但仍在工作的演示:这是一个令人恶心但仍在工作的演示: