Firefox插件-在提交之前捕获post变量[no-enctype]

Firefox addon - Catch post variables before submit [no enctype]

本文关键字:post 变量 no-enctype 插件 提交 Firefox      更新时间:2023-11-10

我有以下表格:

<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中之前捕获变量值。请注意,表单没有属性:enctype="multipart/form-data"。这是一个重要的细节,因为我有一个代码可以让我获得post数据,但它只适用于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
}

谢谢!

如果您有一个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实例(这些实例会中断),并且您必须检查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替换流,那么这些问题就不太重要了,可以忽略。

正在分析

如何准确解析GET风格的参数取决于您,其他答案中已经讨论过了。唯一需要记住的是,通常GET解析器期望有一个前导?(例如?some=value),而POST数据没有(例如只有some=value)。