在firefox扩展中获得响应(标题和正文)

时间:2013-03-20 16:54:12

标签: javascript firefox firefox-addon

我正在尝试实现一个功能,它会在浏览器中为每个http请求提供响应文本和(所有)响应标头, 我在这里读了一下:http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/

我不知道如何从TracingListener对象中提取响应。以前有人处理过吗?谢谢!

我的代码:

    const Cc = Components.classes;
const Ci = Components.interfaces;

// Helper function for XPCOM instanciation (from Firebug)
function CCIN(cName, ifaceName) {
    return Cc[cName].createInstance(Ci[ifaceName]);
}

// get the observer service and register for the two coookie topics.
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                .getService(Components.interfaces.nsIObserverService);

// Copy response listener implementation.
function TracingListener() {
    this.originalListener = null;
    this.receivedData = [];   // array for incoming data.
}

TracingListener.prototype =
{

    originalListener: null,
    receivedData: null,   // array for incoming data.

    onDataAvailable: function(request, context, inputStream, offset, count)
    {
       var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1",
                "nsIBinaryInputStream");
        var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream");
        binaryInputStream.setInputStream(inputStream);
        storageStream.init(8192, count, null);

        var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1",
                "nsIBinaryOutputStream");

        binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));

        // Copy received data as they come.
        var data = binaryInputStream.readBytes(count);

        this.receivedData.push(data);

        binaryOutputStream.writeBytes(data, count);
        this.originalListener.onDataAvailable(request, context,storageStream.newInputStream(0), offset, count);
    },

    onStartRequest: function(request, context) {
        this.originalListener.onStartRequest(request, context);
    },

    onStopRequest: function(request, context, statusCode)
    {
        try {
            if (request.originalURI && piratequesting.baseURL == request.originalURI.prePath && request.originalURI.path.indexOf("/index.php?ajax=") == 0) {

                dump("\nProcessing: " + request.originalURI.spec + "\n");
                var date = request.getResponseHeader("Date");

                var responseSource = this.receivedData.join();
                dump("\nResponse: " + responseSource + "\n");

                piratequesting.ProcessRawResponse(request.originalURI.spec, responseSource, date);
            }
        } catch(e) { dumpError(e);}

        this.originalListener.onStopRequest(request, context, statusCode);
    },

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

// create an nsIObserver implementor
var listener = {
  observe : function(aSubject, aTopic, aData) {
    // Make sure it is our connection first.
    //if (aSubject == gChannel) {
      var httpChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
      if (aTopic == "http-on-modify-request") {
         // ...
            httpChannel.setRequestHeader("X-Hello", "World", false);
      } else if (aTopic == "http-on-examine-response") {
         // ...
         //alert(JSON.stringify(aData));
          var newListener = new TracingListener();
          aSubject.QueryInterface(Ci.nsITraceableChannel);
          newListener.originalListener = aSubject.setNewListener(newListener);
         alert(httpChannel.getResponseHeader("Location"));
         //alert(httpChannel.getAllResponseHeaders());
      }
    //}
  },

  QueryInterface : function(aIID) {
    if (aIID.equals(Components.interfaces.nsISupports) ||
        aIID.equals(Components.interfaces.nsIObserver))
      return this;
    throw Components.results.NS_NOINTERFACE;
  }
};
observerService.addObserver(listener, "http-on-modify-request", false);
observerService.addObserver(listener, "http-on-examine-response", false);

0 个答案:

没有答案