0

我正在尝试实现一个修改 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; 

在这里修改数据。在这里进行测试,我将“测试 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]);
    }
4

1 回答 1

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")
            }
于 2013-02-08T00:42:08.407 回答