火狐扩展中的职位头

问题描述 投票:-1回答:2

我正在构建一个扩展,在那里我可以捕获所有的帖子请求,但在httpChannel.originalURI.spec中没有任何来自帖子的属性。但是在httpChannel.originalURI.spec中没有任何来自帖子的属性。我怎样才能得到帖子的属性?

myObserver.prototype = {

 observe: function(subject, topic, data) {

  if("http-on-modify-request"){

    var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);

    if(httpChannel.requestMethod=="POST")
      alert(httpChannel.originalURI.spec);

       }
  }

},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);

},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                        .getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
}
}

有什么好办法吗?

javascript firefox firefox-addon add-on
2个回答
0
投票

nsIHttpChannel只提供对HTTP头的访问。POST数据是作为请求体的一部分发送的,所以你需要将你的对象接口改为nsIUploadChannel,并将二进制上传数据读成一个字符串。

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);

-1
投票

Luckyrat的代码对我来说不能正常工作。我不得不处理一些请求超时的问题。注意到nmaiers评论这段代码是正确的工作(据我所知)。

function getPostString(httpChannel) {
    var postStr = "";
    try {
        var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
        var uploadChannelStream = uploadChannel.uploadStream;
        if (!(uploadChannelStream instanceof Ci.nsIMultiplexInputStream)) {
            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
            var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
            stream.setInputStream(uploadChannelStream);
            var postBytes = stream.readByteArray(stream.available());

            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(0, 0);

            postStr = String.fromCharCode.apply(null, postBytes);
        }
    }
    catch (e) {
        console.error("Error while reading post string from channel: ", e);
    }
    finally {
        return postStr;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.