Firefox 附加 SDK:获取 http 响应标头 [英] Firefox add-on SDK: Get http response headers
问题描述
我是插件开发的新手,我已经为这个问题苦苦挣扎了一段时间.这里有一些问题在某种程度上是相关的,但它们还没有帮助我找到解决方案.
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.
所以,我正在开发一个 Firefox 插件,当任何网页加载到浏览器的任何选项卡中时,它都会读取一个特定的标题.
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.
我能够观察标签加载,但我认为没有办法在以下(简单)代码中读取 http 标头,只有 url.如果我错了,请纠正我.
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);
});
});
});
我还可以通过观察这样的 http 事件来读取响应标头:
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);
}
}
};
问题(也是个人困惑的主要来源)是,当我阅读响应标头时,我不知道响应是针对哪个请求的.我想以某种方式映射请求(尤其是请求 url)和响应标头(the_header_that_i_need").
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);
}
}
人们还经常需要找到相关的标签,这回答了 查找触发 http-on-examine-response 事件的选项卡
Also people often need to find the tab related, which this answers Finding the tab that fired an http-on-examine-response event
这篇关于Firefox 附加 SDK:获取 http 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!