1

This is mostly in reference to Firefox add-on SDK: Get http response headers. I am trying to log responses to http requests, but so far I am having no luck debugging this. Would really appreciate some advice... been banging my head on this for hours and gotten nowhere.

This is the main.js for my little extension:

var {Cc, Ci} = require("chrome");
var httpRequestObserver =
{
    init: function() {
        var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
        observerService.addObserver( this, 'http-on-examine-response', false );
        observerService.addObserver( this, 'http-on-examine-cached-response', false );
        observerService.addObserver( this, 'http-on-examine-merged-response', false );
    },

    observe: function(subject, topic, data) {
        if (topic === 'http-on-examine-response' || topic === 'http-on-examine-cached-response' || topic === 'http-on-examine-merged-response' ) {
            subject.QueryInterface(Ci.nsIHttpChannel);
            this.onExamineResponse(subject);
        }
    },

    onExamineResponse: function(oHttp) {  
        try
        {
            console.log( oHttp.URI.spec );       
        }
        catch(err)
        {
            console.log(err);
        }
    }
};

When I execute the firefox add-on sdk, the test browser boots up fine, and if I open Tools > Add-ons > Extensions, I see mine listed there. But when I point the test browser to google.com (or any url) nothing gets logged to the console.

I've compared my main.js to the techniques suggested @ https://developer.mozilla.org/en-US/docs/Setting_HTTP_request_headers and everything seems to be congruent. In the end I plan to do something more complicated than just logging things, but I can't even get this basic functionality to work properly!

Community
  • 1
  • 1
sam-6174
  • 3,104
  • 1
  • 33
  • 34

1 Answers1

2

It seems that you set the observer for requests, not responses (http-on-examine-request).

Use this in the init function:

observerService.addObserver( this, 'http-on-examine-response', false );
observerService.addObserver( this, 'http-on-examine-cached-response', false );
observerService.addObserver( this, 'http-on-examine-merged-response', false );

Similarly, use this in the observe function:

if (topic === 'http-on-examine-response' || topic === 'http-on-examine-cached-response' || topic === 'http-on-examine-merged-response' ) { ... }
lexasss
  • 302
  • 1
  • 2
  • 9
  • Thanks for your reply, you are right about my mixup w/ the response... but why didn't my code at least log the requests? – sam-6174 Apr 03 '15 at 16:22
  • For requests you have to set an observer for "http-on-modify-request" - notice the word "modify" – lexasss Apr 03 '15 at 16:30