http-headers - Firefox 附加 SDK : Get http response headers

标签 http-headers firefox-addon firefox-addon-sdk

我是附加开发的新手,我一直在努力解决这个问题。这里有一些问题在某种程度上相关,但它们还没有帮助我找到解决方案。

所以,我正在开发一个 Firefox 插件,当浏览器的任何选项卡中加载任何网页时,它会读取一个特定的标题。

我能够观察标签加载,但我认为没有办法读取以下(简单)代码中的 http header ,只有 url。如果我错了,请纠正我。

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

我还可以通过观察这样的 http 事件来读取响应头:
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);
       }
   }
};

问题(也是个人困惑的一个主要来源)是,当我阅读响应头时,我不知道响应是针对哪个请求的。我想以某种方式映射请求(尤其是请求 url)和响应头(“the_header_that_i_need”)。

最佳答案

你已经差不多了,看看 sample code here了解更多您可以做的事情。

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

人们也经常需要找到相关的标签,这回答了 Finding the tab that fired an http-on-examine-response event

关于http-headers - Firefox 附加 SDK : Get http response headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16583340/

相关文章:

javascript - Components.utils.unload() 是否也卸载辅助导入?

javascript - 从 Firefox 附加内容脚本中的 main.js 接收消息

firefox - 延长请求模块的超时时间

javascript - 使用 nsIXMLHttpRequest 发布到服务器时,POST 有效负载是什么样的?

firefox - 开发一个适当的Firefox扩展(不是首先通过编码到xpi)?

jquery - FireFox插件与服务器的连接

python - 请求在 Accept header 中添加 python 库

html - 为什么 Firefox 忽略基于范围的查询的缓存控制?

javascript - 使用 Angular 1.6 拦截器保护 header 或 cookie

configuration - Nginx 中 $host 和 $http_host 有什么区别