使用http-on-modify-request(firefox扩展)进行POST

时间:2015-12-11 10:26:21

标签: firefox firefox-addon firefox-addon-sdk

我需要实现POST数据记录并更改为FF扩展。我设法生成了一些工作代码,但它工作错误,看起来像是触发任何请求方法,而不仅仅是根据需要发布。似乎所有步骤都是根据mozilla教程完成的,但由于某些原因它不能按预期工作。这是片段。

observe: function(subject, topic, data) 
      {
        if (topic == "http-on-modify-request") {
          var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
          //httpChannel.setRequestHeader("X-Hello", "World", false);
          //console.log("http-on");
          if(httpChannel.requestMethod == "POST")
            {
                console.log("in");
                var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
                var uploadStream = uploadChannel.uploadStream;
                uploadStream.QueryInterface(Ci.nsISeekableStream).
                             seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
                var binStream = Cc["@mozilla.org/binaryinputstream;1"].
                                createInstance(Ci.nsIBinaryInputStream);
                binStream.setInputStream(uploadStream);
                var postBytes = binStream.readByteArray(binStream.available());
                var postString = String.fromCharCode.apply(null, postBytes);
                console.log("read");
                console.log(postString);
            }

        }
      },      

感谢您的时间!

UPD:

有一个想法可以过滤POST请求

var postBytes = binStream.readByteArray(binStream.available());
uploadStream.QueryInterface(Ci.nsISeekableStream).
             seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);

var postString = String.fromCharCode.apply(null, postBytes);
var tmp = postString.split("\r\n\r\n");

if(tmp[1] && tmp[1].length <= 1024 )
{               
    //log & process
}

它有效,但我不知道它将如何处理不同的情况。

0 个答案:

没有答案