在谷歌浏览器中有一个名为远程地址的标题。我正在为 Firefox 编写一个附加组件,我需要根据远程主机来决定一些东西,但看起来 Firefox 中没有这样的标头。如果您知道如何从观察者对象访问远程主机,请告诉我。
observe : function(aSubject, aTopic, aData) {
//I need remote host here
}
她是 Google chrome 中标题的屏幕截图
在谷歌浏览器中有一个名为远程地址的标题。我正在为 Firefox 编写一个附加组件,我需要根据远程主机来决定一些东西,但看起来 Firefox 中没有这样的标头。如果您知道如何从观察者对象访问远程主机,请告诉我。
observe : function(aSubject, aTopic, aData) {
//I need remote host here
}
她是 Google chrome 中标题的屏幕截图
如果标题不存在,则会抛出异常NS_ERROR_NOT_AVAILABLE
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var httpRequestObserver =
{
observe: function(subject, topic, data)
{
var httpChannel, requestURL;
httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
requestURL = httpChannel.URI.spec;
if (topic == "http-on-modify-request") {
//if (requestURL.indexOf('google.com') > -1) {
//httpChannel.setRequestHeader('MyCustomRequestHeader', 'hiiii', false);
try {
var Host = httpChannel.getRequestHeader('Host');
} catch (ex) {
var Host = 'NULL';
}
console.log('REQUEST Header "Host" = ' + Host);
//}
} else if (topic == "http-on-examine-response") {
try {
var Host = httpChannel.getResponseHeader('Host');
} catch (ex) {
var Host = 'NULL';
}
console.log('RESPONSE Header "Host" = ' + Host);
}
}
};
Services.obs.addObserver(httpRequestObserver, "http-on-modify-request", false);
Services.obs.addObserver(httpRequestObserver, "http-on-examine-response", false);
//Services.obs.removeObserver(httpRequestObserver, "http-on-modify-request", false); //run this on shudown of your addon otherwise the observer stags registerd
//Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response", false); //run this on shudown of your addon otherwise the observer stags registerd
用于制作此片段的有用文章: