问题描述
我是附加开发的新手,我一直在努力解决这个问题。这里有一些问题是相关的,但他们还没有帮助我找到一个解决方案。
因此,我正在开发一个Firefox加载项,读取一个特定的标题,当任何网页加载在浏览器中的任何标签。
我能够观察标签加载,但我不认为有一种方式读取以下(简单)代码中的http头,只有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);
},
观察:函数(主题,主题,数据)
{
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>中); //正常工作
console.log(header_value);
}
catch(err)
{
console.log(err);
}
}
};
问题(以及个人困惑的一个主要来源)是当我读取响应头我不知道是哪个请求的回应。我想以某种方式映射请求(尤其是请求url)和响应头(the_header_that_i_need)。
几乎在那里,看看更多的事情你可以做。
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 另外人们经常需要找到相关的选项卡,这个答案
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
这篇关于Firefox附加SDK:获取http响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!