MozillaZine

Redirect HTTP Request (Before it has been loaded)

Talk about add-ons and extension development.
acestuff
 
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)) {
            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

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


d2mw
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: http://stackoverflow.com/questions/5205672/modify-url-before-loading-page-in-firefox/

LIMPET235
Moderator

User avatar
 
Posts: 34380
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-30.0-36.0.1/T-bird-2.0.0.24/SnagIt-v10.0.1/MWP-7.5.0.
RadioYachting. Conficker Test.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 1 guest