我的插件中有一个奇怪的错误,
插件本身需要为特定域添加请求标头参数,
一切正常,但错误是,只有在我重新加载页面之后,观察者的http-on-modify-request才在开始时调用,然后它才起作用.
我的意思是:
>我进入mysite.com/-未修改标题,
>我重新加载页面-已标头
>再次重新加载-已标头
> mysite.com/上的新标签-无需修改标题
>重新加载标签-标题已修改
我的代码,我正在使用插件SDK:
exports.main = function(options,callbacks) {
// Create observer
httpRequestObserver =
{
observe: function(subject, topic, data)
{
if (topic == "http-on-modify-request") {
//only identify to specific preference domain
var windowsService = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
var uri = windowsService.getMostRecentWindow('navigator:browser').getBrowser().currentURI;
var domainloc = uri.host;
if (domainloc=="mysite.com"){
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
httpChannel.setRequestHeader("x-test", "test", false);
}
}
},
register: function()
{
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);
},
unregister: function()
{
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
}
};
//register observer
httpRequestObserver.register();
};
exports.onUnload = function(reason) {
httpRequestObserver.unregister();
};
请帮助我,我搜索了数小时却没有结果.
该代码有效,但在第一次页面加载时不起作用,
仅当我重新加载时.
目标是仅在mysite.com上始终存在一个x-text = test标头请求,但仅在mysite.com上.
解决方法:
浏览器上的currentUri是选项卡中当前加载的Uri.在“ http-on-modify-request”通知时间,请求尚未发送到服务器,因此,如果是新标签,则浏览器没有任何currentUri.当您在适当位置刷新选项卡时,它会使用当前页面的uri,并且似乎可以正常使用.
尝试以下方法:
if (topic == "http-on-modify-request") {
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
var uri = httpChannel.URI;
var domainloc = uri.host;
//only identify to specific preference domain
if (domainloc == "mysite.com") {
httpChannel.setRequestHeader("x-test", "test", false);
}
}