Redirect HTTP Request (Before it has been loaded)

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

Redirect HTTP Request (Before it has been loaded)

Post by acestuff »

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)) {
            aSubject.cancel(Components.results.NS_ERROR_NO_CONTENT);      
         }
      }
      catch(e) {
      }
   }
}
var observerService = Components.classes["@mozilla.org/observer-service;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:
https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads

I'd be most grateful if you can offer any piece of help!
Thanks in advance,
Colin
raid1141
Posts: 3
Joined: November 6th, 2011, 7:27 am

Re: Redirect HTTP Request (Before it has been loaded)

Post by raid1141 »

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

Re: Redirect HTTP Request (Before it has been loaded)

Post by d2mw »

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

A solution can be found here: http://stackoverflow.com/questions/5205672/modify-url-before-loading-page-in-firefox/
User avatar
LIMPET235
Moderator
Posts: 39952
Joined: October 19th, 2007, 1:53 am
Location: The South Coast of N.S.W. Oz.

Re: Redirect HTTP Request (Before it has been loaded)

Post by LIMPET235 »

Thanks but still locking this oldie.
[Ancient Amateur Astronomer.]
Win-10-H/64 bit/500G SSD/16 Gig Ram/450Watt PSU/350WattUPS/Firefox-115.0.2/T-bird-115.3.2./SnagIt-v10.0.1/MWP-7.12.125.

(Always choose the "Custom" Install.)
Locked