I\'ve been trying at this for a long time now, and no good results.
var myObserver = {
observe: function(subject, topic, data)
{
if (topic == \"h
While testing something I created a condensed version (see this gist) of the channel replacement logic mentioned in other answers.
The general idea seems to be to transfer all critical properties over to the new channel, remove callbacks from the old channel so manipulations won't trip up the page load and then shutting the old channel down.
With some modifications one can change the page URI for document loads or leave it as-is.
Warning: That was just a quick hack to get a few pages loading, I have not tested it in depth and it will probably break for some cases. I suspect there are reasons why the HTTPS Everywhere is more complex.
I've done it this way: stop nsIHttpChannel
on "http-on-modify-request"
event, get browser object for current window, call browser.loadURI
.
var utils = require("sdk/window/utils");
function needsRedirect(url) {
// to be implemented
return true;
}
function generateNewUrl(url) {
// to be implemented
return "http://www.example.com/";
}
Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService)
.addObserver({
observe: function(subject, topic, data) {
var channel = subject.QueryInterface(Ci.nsIHttpChannel);
var url = channel.originalURI.spec;
if (needsRedirect(url)) {
//stop
channel.cancel(Cr.NS_BINDING_ABORTED);
//redirect
var gBrowser = utils.getMostRecentBrowserWindow().gBrowser;
var domWin = channel.notificationCallbacks.getInterface(Ci.nsIDOMWindow);
var browser = gBrowser.getBrowserForDocument(domWin.top.document);
browser.loadURI(generateNewUrl(url));
}
}
}, "http-on-modify-request", false);
We can do this by overriding the nsiHttpChannel
with a new one, doing this is slightly complicated but luckily the add-on https-everywhere
implements this to force a https connection.
https-everywhere
's source code is available here
Most of the code needed for this is in the files
[IO Util.js
]
[ChannelReplacement.js
]
We can work with the above files alone provided we have the basic variables like Cc,Ci set up and the function xpcom_generateQI defined.
var httpRequestObserver =
{
observe: function(subject, topic, data) {
if (topic == "http-on-modify-request") {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
var requestURL = subject.URI.spec;
if(isToBeReplaced(requestURL)) {
var newURL = getURL(requestURL);
ChannelReplacement.runWhenPending(subject, function() {
var cr = new ChannelReplacement(subject, ch);
cr.replace(true,null);
cr.open();
});
}
}
},
get observerService() {
return Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
},
register: function() {
this.observerService.addObserver(this, "http-on-modify-request", false);
},
unregister: function() {
this.observerService.removeObserver(this, "http-on-modify-request");
}
};
httpRequestObserver.register();
The code will replace the request not redirect.
While I have tested the above code well enough, I am not sure about its implementation. As far I can make out, it copies all the attributes of the requested channel and sets them to the channel to be overridden. After which somehow the output requested by original request is supplied using the new channel.
P.S. I had seen a SO post in which this approach was suggested.
I am under the impression that you cannot do this at this level - I tried a variety of methods of externally "tricking" the code that calls for a nsIHttpChannel to be created (example at end of post).
What I would recommend is if you want a redirect, contact the channel's owner window (which works 99% of the time), and instruct it to redirect. I know that it's not going to behave the same, but since I don't know exactly why you're doing this, this will externally (to the user) appear to be doing the same thing as what you ask.
Here's the basics of what I was trying:
if(aTopic == "http-on-examine-response") {
var request = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
if(!request.URI.spec.match("^http://www.apple.com/")) {
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var ch = ios.newChannel("http://www.apple.com/", null, null);
var listener = {
QueryInterface : XPCOMUtils.generateQI([Ci.nsIChannelEventSink]),
onDataAvailable: function() {},
onStopRequest: function() {},
onStartRequest: function() {}
};
ch.asyncOpen(listener,null);
var eventSink = request.notificationCallbacks.getInterface(Ci.nsIChannelEventSink);
eventSink.asyncOnChannelRedirect(request,ch,Ci.nsIChannelEventSink.REDIRECT_INTERNAL,function() {});
}