我是附加开发的新手,我一直在努力解决这个问题。这里有一些问题在某种程度上是相关的,但它们还没有帮助我找到解决方案。
所以,我正在开发一个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);
},
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”)。
答案 0 :(得分:3)
你几乎就在那里,看看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