无法在Firefox扩展中修改POST数据

时间:2013-02-07 22:41:09

标签: firefox firefox-addon xpcom xulrunner

我正在尝试实现一个修改POST请求数据的Firefox扩展。

代码如下,它在标记为“失败!!!

的地方失败

任何见解都会有所帮助。

    Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

    var newData = "test 123";
    function LOG(msg) {
        var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                     .getService(Components.interfaces.nsIConsoleService);
        consoleService.logStringMessage(msg);
    }

    function CMP() {
        this.registered = false;
        this.register();
    }

    CMP.prototype = {

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

        observe: function(subject, topic, data) 
        {
            LOG("Inside observe");
            if (topic == "http-on-modify-request") 
            {
                LOG("TOPIC is http-on-modify-request");
                var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);

                if(httpChannel.requestMethod == "POST"){
                    LOG("Inside POST")
                    var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
                    //var uploadChannelStream = uploadChannel.uploadStream; 

在此处修改数据。在这里进行测试我将“test 123”作为新数据传递

                    var newStringInputStream = Components.classes['@mozilla.org/io/string-input-stream;1'].createInstance(Components.interfaces.nsIStringInputStream);
                    newStringInputStream.setData(newData,newData.length);
                    LOG("set data in newStringInputStream!!");

                    uploadChannel.setUploadStream(newStringInputStream, "text/plain", -1 );// Fails here!!!
                    httpChannel.requestMethod = "POST";
                    LOG("upload DONE!!")  
                }   
            }
        },

        QueryInterface : function(aIID) {
        if (aIID.equals(Components.interfaces.nsISupports) ||
            aIID.equals(Components.interfaces.nsIObserver))
          return this;
        throw Components.results.NS_NOINTERFACE;
        },

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

        classID: Components.ID('{F799F47E-ABA5-4AF1-B8F2-BD74E3E5BCC0}'),
        QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver])
    };

    if (XPCOMUtils.generateNSGetFactory)
    {
      var NSGetFactory = XPCOMUtils.generateNSGetFactory([CMP]);
    }

1 个答案:

答案 0 :(得分:1)

通过在上面的代码中进行更改来修复它。主要的变化是在httpChannel.uploadStream中设置修改后的数据。

希望这有助于某人!

var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
            if(httpChannel.requestMethod == "POST")
            {
                LOG("Inside POST")
                var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel); 
                var newStringInputStream = Components.classes['@mozilla.org/io/string-input-stream;1'].createInstance(Components.interfaces.nsIStringInputStream);
                newStringInputStream.setData(newData,newData.length);
                var uploadChannelStream = uploadChannel.uploadStream;
                uploadChannelStream = uploadChannelStream.QueryInterface(Components.interfaces.nsISeekableStream).seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
                httpChannel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);
                httpChannel.uploadStream.setData(newStringInputStream);
                LOG("Done POST")
            }