MrCode
MrCode

Reputation: 64536

Firefox addon intercept and modify POST data and get the URL

I'm trying to get the URL of each POST request, read the POST data and then change it (with a replace for example). The code I have is throwing an error: stringStream is not defined, which is correct, so what should it be defined as to get the setData() method? Part of the code is originally from an SO post. Also how do I get the URL it's posting to?

//create an nsIObserver implementor
var listener = {
   observe : function(aSubject, aTopic, aData) {
      var httpChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);


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

            var channel= aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);

            if(channel.requestMethod == "POST")
            {

                channel=channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
                channel = channel.uploadStream;  
                channel.QueryInterface(Components.interfaces.nsISeekableStream)
                                .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
                var stream = Components.classes["@mozilla.org/binaryinputstream;1"]
                                .createInstance(Components.interfaces.nsIBinaryInputStream);  
                stream.setInputStream(channel);  
                var postBytes = stream.readByteArray(stream.available());  
                poststr = String.fromCharCode.apply(null, postBytes);  

                //change the poststr

                // poststr=poststr.replace(....);  

                // ERROR HERE - stringStream is not defined
                stringStream.setData(poststr, poststr.length);  
                //changing the postdata  
                channel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
                channel = channel.uploadStream;  
                channel = channel.QueryInterface(Components.interfaces.nsISeekableStream)
                          .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
                channel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);  
                channel.uploadStream.setData(stringStream);  
                channel.requestMethod = "POST";

            }

          }

   },

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

var observerService = null;

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

   removeObserver : function(){
      observerService.removeObserver(listener, "http-on-modify-request");
   }
};

Init.addObserver();

Upvotes: 2

Views: 6003

Answers (1)

schemacs
schemacs

Reputation: 2891

see Creating HTTP POSTs for how to do this right.

  1. stringStream is not defined anywhere in your code, so put var declaration or so.
  2. inputStream.setData(postData, postData.length), please refer to nsInputStrem for more.

Upvotes: 2

Related Questions