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 に答える