且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firefox附加SDK:获取http响应头

更新时间:2022-10-29 11:12:41

几乎在那里,看看样本代码在这里更多的事情你可以做。

  onExamineResponse:function(oHttp)
{
try
{
var header_value = oHttp.getResponseHeader(< the_header_that_i_need>);
// URI是您正在查看的响应的nsIURI
// spec向您提供完整的URL字符串
var url = oHttp.URI.spec;
}
catch(err)
{
console.log(err);






$ b

另外人们经常需要找到相关的选项卡,这个答案找到标签,发射一个http-审查回应事件


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").

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