Redirect HTTP Request (Before it has been loaded)

Talk about add-ons and extension development.
Posts: 3
Joined: July 18th, 2010, 10:11 am

Post Posted June 20th, 2011, 7:53 am

As part of a filter addon, I need to stop certain resources, embedded into webpages (eg. javascripts) from being loaded but intead load another script in their place.

I figured that, because Firefox doesn't have a "onbeforeload" event listener (this is what I've used in Google Chrome), I need to redirect the HTTP requests to certain urls before they load.

This has been driving me crazy for 3 days now. The best I have come up with is this:
Code: Select all
var observe = function(aSubject, aTopic, aData)
   if(aTopic == "http-on-modify-request") {
      try {
         var httpChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
         if(isBadUrl(aSubject.URI.asciiSpec)) {
      catch(e) {
var observerService = Components.classes[";1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(observe, "http-on-modify-request", false);

The problem is, it can only stop the request: it can't redirect it as the target has already been set.

I have looked at similar addons but I am yet to find a solution. I'm pretty sure HTTPS Everywhere must use some kind of redirect of requests before they load but it is too complicated for me to understand how it does it!

This page looks like a useful resource but I don't know how to use any of the code to redirect requests:

I'd be most grateful if you can offer any piece of help!
Thanks in advance,

Posts: 3
Joined: November 6th, 2011, 7:27 am

Post Posted November 7th, 2011, 6:46 am

New Member
Posts: 2
Joined: February 16th, 2013, 6:25 pm

Post Posted February 16th, 2013, 6:27 pm

Replying to this ancient thread for the sake of the search engines.

A solution can be found here:


User avatar
Posts: 35730
Joined: October 19th, 2007, 1:53 am
Location: The South Coast of N.S.W. Oz.

Post Posted February 16th, 2013, 10:51 pm

Thanks but still locking this oldie.
Ancient Amateur Astronomer
Win-7-HP/IntelĀ® DualCore-2.0GHz/500G HDD/4 Gig Ram/550Watt PSU/350WattUPS/Firefox-20.0-43.0.2/T-bird-
RadioYachting. Conficker Test. (Always choose the "Custom" Install.)

Return to Extension Development

Who is online

Users browsing this forum: No registered users and 2 guests