如何在Firefox扩展中修改http响应
嘿,我已经能够编写一个nsIStreamListener侦听器来侦听响应,并在上的教程后获取响应文本。但我无法修改发送到浏览器的响应。实际上,如果我返回响应并发送回链,它将反映在firebug中,但不会反映在浏览器中 我猜我们将不得不替换默认侦听器,而不是在链中侦听。我无法在任何地方获得任何解释如何执行此操作的文档 谁能告诉我一些关于这方面的见解?这主要是为了教育目的 提前谢谢 编辑:到目前为止,我已经找到了一些解决方案,我能够做到这一点如何在Firefox扩展中修改http响应,firefox,firefox-addon,Firefox,Firefox Addon,嘿,我已经能够编写一个nsIStreamListener侦听器来侦听响应,并在上的教程后获取响应文本。但我无法修改发送到浏览器的响应。实际上,如果我返回响应并发送回链,它将反映在firebug中,但不会反映在浏览器中 我猜我们将不得不替换默认侦听器,而不是在链中侦听。我无法在任何地方获得任何解释如何执行此操作的文档 谁能告诉我一些关于这方面的见解?这主要是为了教育目的 提前谢谢 编辑:到目前为止,我已经找到了一些解决方案,我能够做到这一点 var old; function TracingLi
var old;
function TracingListener() {}
TracingListener.prototype = {
originalListener: null,
receivedData: null, //will be an array for incoming data.
//For the listener this is step 1.
onStartRequest: function (request, context) {
this.receivedData = []; //initialize the array
//Pass on the onStartRequest call to the next listener in the chain -- VERY IMPORTANT
//old.onStartRequest(request, context);
},
//This is step 2. This gets called every time additional data is available
onDataAvailable: function (request, context, inputStream, offset, count) {
var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream");
binaryInputStream.setInputStream(inputStream);
var storageStream = CCIN("@mozilla.org/storagestream;1",
"nsIStorageStream");
//8192 is the segment size in bytes, count is the maximum size of the stream in bytes
storageStream.init(8192, count, null);
var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream");
binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
// Copy received data as they come.
var data = binaryInputStream.readBytes(count);
this.receivedData.push(data);
binaryOutputStream.writeBytes(data, count);
//Pass it on down the chain
//old.onDataAvailable(request, context,storageStream.newInputStream(0), offset, count);
},
onStopRequest: function (request, context, statusCode) {
try {
//QueryInterface into HttpChannel to access originalURI and requestMethod properties
request.QueryInterface(Ci.nsIHttpChannel);
//Combine the response into a single string
var responseSource = this.receivedData.join('');
//edit data as needed
responseSource = "test";
console.log(responseSource);
} catch (e) {
//standard function to dump a formatted version of the error to console
dumpError(e);
}
var stream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stream.setData(responseSource, -1);
//Pass it to the original listener
//old.originalListener=null;
old.onStartRequest(channel, context);
old.onDataAvailable(channel, context, stream, 0, stream.available());
old.onStopRequest(channel, context, statusCode);
},
QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsISupports)) {
return this;
}
throw components.results.NS_NOINTERFACE;
},
readPostTextFromRequest: function (request, context) {
try {
var is = request.QueryInterface(Ci.nsIUploadChannel).uploadStream;
if (is) {
var ss = is.QueryInterface(Ci.nsISeekableStream);
var prevOffset;
if (ss) {
prevOffset = ss.tell();
ss.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
}
// Read data from the stream..
var charset = "UTF-8";
var text = this.readFromStream(is, charset, true);
if (ss && prevOffset == 0)
ss.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
return text;
} else {
dump("Failed to Query Interface for upload stream.\n");
}
} catch (exc) {
dumpError(exc);
}
return null;
},
readFromStream: function (stream, charset, noClose) {
var sis = CCSV("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream");
sis.setInputStream(stream);
var segments = [];
for (var count = stream.available(); count; count = stream.available())
segments.push(sis.readBytes(count));
if (!noClose)
sis.close();
var text = segments.join("");
return text;
}
}
httpRequestObserver = {
observe: function (request, aTopic, aData) {
if (typeof Cc == "undefined") {
var Cc = components.classes;
}
if (typeof Ci == "undefined") {
var Ci = components.interfaces;
}
if (aTopic == "http-on-examine-response") {
request.QueryInterface(Ci.nsIHttpChannel);
console.log(request.statusCode);
var newListener = new TracingListener();
request.QueryInterface(Ci.nsITraceableChannel);
channel = request;
//newListener.originalListener
//add new listener as default and save old one
old = request.setNewListener(newListener);
old.originalListener = null;
var threadManager = Cc["@mozilla.org/thread-manager;1"]
.getService(Ci.nsIThreadManager);
threadManager.currentThread.dispatch(newListener, Ci.nsIEventTarget.DISPATCH_NORMAL);
}
},
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;
},
};
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
observerService.addObserver(httpRequestObserver,
"http-on-examine-response", false);
这个例子适用于我在Firefox 34上的应用(当前为夜间): 我下载了xpi,编辑了bootstrap.js来修改流:
132 // Copy received data as they come.
133 var data = binaryInputStream.readBytes(count);
134 data = data.replace(/GitHub/g, "TEST");
135 this.receivedData.push(data);
安装XPI,然后重新加载github页面。它的页脚上写着“TEST”
您发布的代码版本实际上不会将结果传递回旧的侦听器,因此这是应该更改的第一件事
它还可能与Firebug或其他扩展严重交互。尝试在干净的配置文件中重现问题是个好主意(只安装了扩展)。Woo我的演示/实验工作正在帮助他人!谢谢你引用我的话!:)顺便说一句,你是怎么发现这个的?还有一个,因为我从来不知道这个可以用来修改显示/渲染的源代码。我以为这只是为了得到http请求的副本。超级酷,我学到了一些关于我复制的垃圾,粘贴和捣碎在一起@Noitidart:通过谷歌;很高兴知道我们互相帮助。去开源吧!哈哈,这太讽刺了,因为我太反谷歌了,我完全支持bing:开源万岁!现在这些API已经过时了。