在Google Chrome中,有一个名为“远程地址”的标题。 我正在为Firefox写一个附加组件,我需要根据远程主机决定一些内容,但看起来Firefox中没有这样的标题。 如果您知道如何从观察者对象访问远程主机,请告诉我。
observe : function(aSubject, aTopic, aData) {
//I need remote host here
}
她是Google Chrome中标题的屏幕截图
答案 0 :(得分:1)
如果标题不存在,则抛出异常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
用于制作此代码段的实用文章: