Reputation: 302
I have a weird bug in my addon, the addon itself needs to add a request header parameters for a specific domain, it's all working, but the bug is, that the observer http-on-modify-request is not called at start, only if I reload the page, then it's working.
I mean:
My code, I'm using the addon 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();
};
Please help me, I searched for hours with no results. The code is working, but not at first time of page loading, only if I reload.
The goal is that only on mysite.com, there will be a x-text=test
header request, all the time, but only on mysite.com.
Upvotes: 3
Views: 1702
Reputation: 696
currentUri
on browser is the Uri that is currently loaded in the tab. At "http-on-modify-request" notification time, the request is not sent to the server yet, so if it's a new tab, the browser doesn't have any currentUri
. When you refresh the tab in place, it uses the uri of the current page, and it seemingly works.
Try this instead:
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);
}
}
Upvotes: 2