我正在构建一个扩展程序以在Firefox中获取POST请求。我阅读了文档中的拦截页面加载和HTTP观察者,但仍无法在页面加载时获取特定的POST数据(例如:data1 = 50& sdata2 = 0& data3 = 50)。
我查看了TamperData的代码,发现他们使用了stream.available()和stream.read(1)。但是,我无法使用这些命令来处理我的代码。
目前我的代码如下:
var ObserverTest = {
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
}
if (topic == "http-on-examine-response") {
var newListener = new TracingListener();
subject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = subject.setNewListener(newListener);
}
},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(ObserverTest, "http-on-modify-request", false);
observerService.addObserver(ObserverTest, "http-on-examine-response", false);
},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(ObserverTest, "http-on-modify-request");
observerService.removeObserver(ObserverTest,"http-on-examine-response");
}
}
window.addEventListener("load", ObserverTest.register, false);
window.addEventListener("unload", ObserverTest.unregister, false);
//Helper function for XPCOM instanciation (from Firebug)
function CCIN(cName, ifaceName) {
return Cc[cName].createInstance(Ci[ifaceName]);
}
// Copy response listener implementation.
function TracingListener() {
this.originalListener = null;
this.receivedData = []; // array for incoming data.
}
TracingListener.prototype = {
onDataAvailable: function(request, context, inputStream, offset, count) {
var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream");
var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream");
var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream");
var stream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
stream.init(binaryInputStream);
binaryInputStream.setInputStream(inputStream);
storageStream.init(8192, count, null);
binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
// Copy received data as they come.
var data = binaryInputStream.readBytes(count);
this.receivedData.push(data);
binaryOutputStream.writeBytes(data, count);
this.originalListener.onDataAvailable(request, context, storageStream.newInputStream(0), offset, count);
},
onStartRequest: function(request, context) {
this.originalListener.onStartRequest(request, context);
},
onStopRequest: function(request, context, statusCode)
{
// Get entire response
var responseSource = this.receivedData.join();
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;
}
}
答案 0 :(得分:3)
首先,根本不需要"http-on-examine-response"
和TracingListener
。如果您想对响应执行某些操作,那么这些内容就具有优势,但您需要处理请求中的数据,因此topic == 'http-on-modify-request'
就是这样。
以下功能(未经测试,但从我的一个扩展中复制并清理了一下)演示了如何获取发布数据。假定从http-on-modify-request
调用该函数。
const ScriptableInputStream = Components.Constructor(
"@mozilla.org/scriptableinputstream;1",
"nsIScriptableInputStream",
"init");
function observeRequest(channel, topic, data) {
let post = null;
if (!(channel instanceof Ci.nsIHttpChannel) ||
!(channel instanceof Ci.nsIUploadChannel)) {
return post;
}
if (channel.requestMethod !== 'POST') {
return post;
}
try {
let us = channel.uploadStream;
if (!us) {
return post;
}
if (us instanceof Ci.nsIMultiplexInputStream) {
// Seeking in a nsIMultiplexInputStream effectively breaks the stream.
return post;
}
if (!(us instanceof Ci.nsISeekableStream)) {
// Cannot seek within the stream :(
return post;
}
let oldpos = us.tell();
us.seek(0, 0);
try {
let is = new ScriptableInputStream(us);
// we'll read max 64k
let available = Math.min(is.available(), 1 << 16);
if (available) {
post = is.read(available);
}
}
finally {
// Always restore the stream position!
us.seek(0, oldpos);
}
}
catch (ex) {
Cu.reportError(ex);
}
return post;
}
根据您的使用情况,您可能需要检查us instanceof
是否为nsIMIMEInputStream
或nsIStringInputStream
用于特殊处理或快速路径...
你可以从你的观察者那里给它打电话:
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
observeRequest(subject, topic, data);
}
},