2011-12-23 89 views
2

我試圖獲取每個POST請求的URL,讀取POST數據,然後更改它(例如替換)。我的代碼是拋出一個錯誤:stringStream is not defined,這是正確的,那麼應該定義什麼來獲取setData()方法?部分代碼最初來自SO帖子。另外我如何獲取它發佈的URL?Firefox插件攔截並修改POST數據並獲取URL

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

回答

2

請參閱Creating HTTP POSTs如何做到這一點。

  1. stringStream沒有在你的代碼中的任何地方定義,所以把var聲明放在那裏。
  2. inputStream.setData(postData, postData.length),詳情請參考nsInputStrem
+0

很好,謝謝 – MrCode 2012-03-06 17:51:03