Javascript 尝试在Firefox扩展中获取HTTP POST请求
我正在构建一个扩展来获取Firefox中的POST请求。我阅读了有关拦截页面加载和HTTP观察者的文档,但仍然无法获取页面加载上的特定POST数据(例如:data1=50&sdata2=0&data3=50) 我查看了TamperData的代码,发现它们使用stream.available()和stream.read(1)。但是,我无法让这些命令与我的代码一起使用 当前我的代码如下所示: 首先,根本不需要Javascript 尝试在Firefox扩展中获取HTTP POST请求,javascript,http-headers,firefox-addon,Javascript,Http Headers,Firefox Addon,我正在构建一个扩展来获取Firefox中的POST请求。我阅读了有关拦截页面加载和HTTP观察者的文档,但仍然无法获取页面加载上的特定POST数据(例如:data1=50&sdata2=0&data3=50) 我查看了TamperData的代码,发现它们使用stream.available()和stream.read(1)。但是,我无法让这些命令与我的代码一起使用 当前我的代码如下所示: 首先,根本不需要“http on-examine-response”和跟踪侦听器。如果您想对响应执行某些操
“http on-examine-response”
和跟踪侦听器。如果您想对响应执行某些操作,但您正在查找请求中的数据,那么这种方法是有好处的,因此topic==“http on modify request”
就是这样
下面的函数(未经测试,但复制自我的一个扩展,并进行了一些清理)演示了如何获取post数据。假设函数是在修改请求时从http调用的
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;
}
我还是有点困惑。当您说函数observeRequest是在修改请求时从http调用的,您的意思是在ObserverTest中的observe:function(subject、topic、data){…}内吗?另外,我也不完全确定通道、主题和数据这三个参数应该传递什么。对不起,这里应该更精确一些。更新了我的答案。没关系,我成功了。非常感谢!如果其他人需要,我会在问题中添加工作代码作为编辑。编辑:没关系,你已经这么做了。如果我需要在发送POST数据之前对其进行编辑,有多少代码会被更改?在将常量的定义更改为以下内容后,你的代码为我工作:const{Cc,Cc,Ci,Cr,Components}=require(“chrome”);const ScriptableInputStream=CC(“@mozilla.org/ScriptableInputStream;1”,“nsscriptableinputstream”,“init”)代码>可以连接到错误报告吗?
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;
}
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
observeRequest(subject, topic, data);
}
},