sam2426679
sam2426679

Reputation: 3857

Firefox SDK monitoring http response

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!

Upvotes: 1

Views: 291

Answers (1)

lexasss
lexasss

Reputation: 302

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' ) { ... }

Upvotes: 2

Related Questions