5

I'm new to add-on development and I've been struggling with this issue for a while now. There are some questions here that are somehow related but they haven't helped me to find a solution yet.

So, I'm developing a Firefox add-on that reads one particular header when any web page that is loaded in any tab in the browser.

I'm able to observer tab loads but I don't think there is a way to read http headers inside the following (simple) code, only url. Please correct me if I'm wrong.

var tabs = require("sdk/tabs");
tabs.on('open', function(tab){
  tab.on('ready', function(tab){
    console.log(tab.url);
  });
});
});

I'm also able to read response headers by observing http events like this:

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);
   },

  observe: function(subject, topic, data) 
  {
    if (topic == "http-on-examine-response") {
         subject.QueryInterface(Ci.nsIHttpChannel);
            this.onExamineResponse(subject);
    }
  },
  onExamineResponse: function (oHttp)
  {  
        try
       {
         var header_value = oHttp.getResponseHeader("<the_header_that_i_need>"); // Works fine
         console.log(header_value);        
       }
       catch(err)
       {
         console.log(err);
       }
   }
};

The problem (and a major source of personal confusion) is that when I'm reading the response headers I don't know to which request the response is for. I want to somehow map the request (request url especially) and the response header ("the_header_that_i_need").

mikko76
  • 115
  • 1
  • 5

1 Answers1

3

You're pretty much there, take a look at the sample code here for more things you can do.

  onExamineResponse: function (oHttp)
  {  
        try
       {
         var header_value = oHttp.getResponseHeader("<the_header_that_i_need>"); 
         // URI is the nsIURI of the response you're looking at 
         // and spec gives you the full URL string
         var url = oHttp.URI.spec;
       }
       catch(err)
       {
         console.log(err);
       }
   }

Also people often need to find the tab related, which this answers Finding the tab that fired an http-on-examine-response event

Community
  • 1
  • 1
Bryan Clark
  • 2,542
  • 1
  • 15
  • 19
  • 1
    Thanks for the pointers Bryan! I also found out that, in onExamineResponse, I can call oHttp.name which returns the url of the request.I'm surprised that the headers are not available in tab's 'ready' event. The page is loaded so I would think the headers are there handy. – mikko76 May 17 '13 at 06:24