I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modified and then loading the page should start.
Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?
Here is what I managed to do, but still don't know how to replace the old HTTP channel with the new one....
Code: Select all
Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).addObserver({
observe: function(request, aTopic, aData){
if (aTopic == "http-on-modify-request") {
var httpChannel = request.QueryInterface(Components.interfaces.nsIHttpChannel);
if (request instanceof Components.interfaces.nsIHttpChannel &&
request.originalURI.spec.indexOf("/file.ext") > 0) {
//request.cancel(Components.results.NS_BINDING_ABORTED);
var standardURL = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIStandardURL);
standardURL.spec = "chrome://mynewaddon/content/newfile.ext";
var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var channel = ioService.newChannelFromURI(standardURL);
// Here I have to replace the old channel with the new one
}
}
},
QueryInterface: function(aIID) {
if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports)) {
return this;
}
throw Components.results.NS_NOINTERFACE;
},
}, "http-on-modify-request", false);
Anyone can help me with this one?