我有以下表格:
<form method="post" action="includes/do.php">
<input type="text" name="action" value="login" />
<input type="text" name="email" value="myemail@hotmail.com" />
<input type="text" name="pass" value="helloworld" />
<input type="submit" value="Send" />
</form>
然后,我想在firefox addon observer
内提交之前捕获变量值。请注意,表单DOESN&T; T具有属性:enctype="multipart/form-data"
。这是一个重要的细节,因为我有一个代码可以让我获取帖子数据但它只适用于enctype="multipart/form-data"
。
我确定我必须使用:
var scrStream = Cc["@mozilla.org/scriptableinputstream;1"]
.createInstance(Ci.nsIScriptableInputStream);
但到目前为止我还没有得到正常工作的代码(我是初学者)。
我想得到类似的东西:
{
"action": "login",
"email": "myemail@hotmail.com",
"pass": "helloworld"
}
如果您通过以下界面了解某些功能,那么更好:
function get_post_data(channel) {
var data;
// ...
return data
}
谢谢!
答案 0 :(得分:2)
如果您拥有nsIUploadChannel
频道,则可以阅读信息流的内容并将其解析为GET
参数(例如some=value&someother=thing
)。
var instream = Cc["@mozilla.org/scriptableinputstream;1"].
createInstance(Ci.nsIScriptableInputStream);
instream.init(channel.uploadStream);
var data = instream.read(instream.available());
但是,有些事情需要考虑,如果你想让原始的.uploadStream
仍然有用,即你根本不能访问nsIMultiplexInputStream
个实例(这些将会中断),你必须检查{ {1}}并回放流。
nsISeekableStream
如果你用var ustream = channel.uploadStream;
if (ustream instanceof Ci.nsIMultiplexInputStream) {
throw new Error("multiplexed input streams are not supported!");
}
if (!(ustream instanceof Ci.nsISeekableStream)) {
throw new Error("cannot rewind upload stream; not touching");
}
var pos = ustream.tell();
var instream = Cc["@mozilla.org/scriptableinputstream;1"].
createInstance(Ci.nsIScriptableInputStream);
instream.init(ustream);
var data = instream.read(instream.available());
ustream.seek(0, pos); // restore position
替换了流,那么这些并不是很重要,可以忽略不计。
您究竟如何解析.setUploadStream
样式参数取决于您,并已在other answers中进行了讨论。
唯一要记住的是,通常GET
解析器期望前导GET
(例如?
),?some=value
数据没有(例如POST
)