nsITraceableChannel,拦截HTTP通信代码 [英] nsITraceableChannel, Intercept HTTP Traffic code

查看:150
本文介绍了nsITraceableChannel,拦截HTTP通信代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这个代码工作的Firefox,我从
nsITraceableChannel,拦截HTTP流量
并从如何从nsITraceableChannel获得一个url?,我一直在寻找答案,没有运气。
我想要做的是截取某些链接,并改变它们。
欢迎任何帮助

我得到的错误是函数声明需要一个名字 witch指向 observe:function aSubject,aTopic,aData)部分代码

const Cc = Components.classes; const Ci = Components.interfaces; var observerService = Cc [@ mozilla.org/observer-service;1] .getService(Ci.nsIObserverService); observerService.addObserver(httpRequestObserver,http-on- (););} ----------------------- --------------------------------- var httpRequestObserver = {observe:function(aSubject,aTopic,aData){if( aTopic ==http-on-examine-response){}},QueryInterface:function(aIID){如果(aIID.equals(Ci.nsIObserver)|| aIID.equals(Ci.nsISupports)){return this; }抛出Components.results.NS_NOINTERFACE; }} // ----------------------------------------------函数TracingListener(){this.originalListener = null;} TracingListener.prototype = {onDataAvailable:function(request,context,inputStream,offset,count){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; }抛出Components.results.NS_NOINTERFACE; }}观察:function(aSubject,aTopic,aData){if(aTopic ==http-on-examine-response){var newListener = new TracingListener(); aSubject.QueryInterface(Ci.nsITraceableChannel); newListener.originalListener = aSubject.setNewListener(newListener); }} aSubject = aSubject.QueryInterface(Ci.nsIChannel); var uri = aSubject.URI; //在任何其他解析器步骤之前请求的原始URI //和/或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.ofCi.nsIUploadChannel); // trueconsole.log(ch.toString()); //[xpconnect wrapped(nsISupports,nsIChannel,nsITraceableChannel,nsIUploadChannel)]//变量ch被称为在这一点上实现了四个给定的接口

复制粘贴这个工作:
$ b $

b

  // 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 = {
观察:函数(aSubject,aTopic,aData){
// if(aTopic == http-on-examine-response){} //不需要这个,我们为http-on-examine-response添加了观察者,所以我们确实知道这只会触发`http-on-examine-response`
var newListener = new TracingListener();
Subject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = aSubject.setNewListener(newListener);
}
/ * //这段代码的重点是什么?

QueryInterface:function(aIID){
if(aIID.equals(Ci.nsIObserver)|| aIID.equals(Ci.nsISupports)){
return this;
}
throw Cr.NS_NOINTERFACE;
}
* /
}

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

函数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;
}
}

//到底是什么?这不是一个对象的一部分?为什么观察结肠?
/ *
观察:function(aSubject,aTopic,aData){
if(aTopic ==http-on-examine-response){
var newListener = new TracingListener ();
Subject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = aSubject.setNewListener(newListener);
}
}
* /

//这是什么垃圾?是应该在观察员?
/ *
Subject = aSubject.QueryInterface(Ci.nsIChannel);
var uri = aSubject.URI;
//在任何其他解析器步骤之前请求的原始URI
//和/或重定向。
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)]
//变量ch已知可以实现这四个给定的接口。
* /



更新



有人问我如何得到响应源,所以我创建了一个要点: https://gist.github.com/Noitidart/d0b08629ee2804538ad9#file-_ff-addon-snippet-copyofrequestsource-js-L16


Ive been trying to get this code working for firefox, that I got from nsITraceableChannel, Intercept HTTP Traffic and from How to get an url from nsITraceableChannel?, Ive been googling for the answer, with no luck. What I would like to do is intercept certain links, and change them. Any help is welcomed

The error I get is function statement requires a name witch points to observe: function(aSubject, aTopic, aData) part of the code

 const Cc = Components.classes;
const Ci = Components.interfaces;

var observerService = Cc["@mozilla.org/observer-service;1"]
    .getService(Ci.nsIObserverService);

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

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

 var httpRequestObserver =
{
    observe: function(aSubject, aTopic, aData)
    {
        if (aTopic == "http-on-examine-response")
        {
        }
    },

    QueryInterface : function (aIID)
    {
        if (aIID.equals(Ci.nsIObserver) ||
            aIID.equals(Ci.nsISupports))
        {
            return this;
        }

        throw Components.results.NS_NOINTERFACE;

    }
}

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

 function TracingListener() {
    this.originalListener = null;
}

TracingListener.prototype =
{
    onDataAvailable: function(request, context, inputStream, offset, count) {
        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 Components.results.NS_NOINTERFACE;
    }
}

observe: function(aSubject, aTopic, aData)
{
    if (aTopic == "http-on-examine-response") {
        var newListener = new TracingListener();
        aSubject.QueryInterface(Ci.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
}

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.

解决方案

Copy paste this it works:

// 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.
*/

Update

Someone asked me how to get the response source with this, so I created a gist out of it: https://gist.github.com/Noitidart/d0b08629ee2804538ad9#file-_ff-addon-snippet-copyofrequestsource-js-L16

这篇关于nsITraceableChannel,拦截HTTP通信代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆