Modify URL before loading page in firefox

Talk about add-ons and extension development.
Posts: 3
Joined: November 6th, 2011, 7:27 am

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


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

   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) {

            var standardURL = Components.classes[";1"].createInstance(Components.interfaces.nsIStandardURL);
            standardURL.spec = "chrome://mynewaddon/content/newfile.ext";
            var ioService = Components.classes[";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?

Posts: 39
Joined: April 10th, 2011, 12:54 pm

Post Posted November 6th, 2011, 1:16 pm

I use onPageLoad to redirect urls. however the user will see a millisecond of the page before redirections
It dosen't bother me that much.but it would be a better approach if you get your code working, saving network traffic and all that.

Code: Select all
     var appcontent = document.getElementById("appcontent");   // browser
       appcontent.addEventListener("DOMContentLoaded", onPageLoad, true);
     var messagepane = document.getElementById("messagepane"); // mail
       messagepane.addEventListener("load", function(event) { onPageLoad(event); }, true);

Code: Select all
function onPageLoad(aEvent) {
      var i;
      var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
      var href = getDocHref();

      // only want to reload the topmost frame
      if (isFramed(aEvent)){
         if( doc.location.href=="about:config" ){
      // add event listener for page unload
     //  aEvent.originalTarget.defaultView.addEventListener("unload", function(event){ myExtension.onPageUnload(event); }, true);

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

Post Posted November 6th, 2011, 2:03 pm

Thanks thorc, but I believe I cant use the OnPageLoad event if the request is made by an in-page javascript after the content was already loaded, which in fact is my case :-(, that's why I am trying to use nsIObserverService in order to trap the request and modify it before it is sent to the server. The problem again is in replacing the request's HTTP channel with a nsIStandardURL.

Any ideas?

Posts: 3
Joined: March 24th, 2012, 6:53 am

Post Posted March 24th, 2012, 7:20 am

did you find the solution for this issue? I have a similar use case and stuck with this.

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

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

See for a solution to this


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

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

Please don't bump these old threads.
Thank you.
Ancient Amateur Astronomer
Win-7-HP/IntelĀ® DualCore-2.0GHz/500G HDD/4 Gig Ram/550Watt PSU/350WattUPS/Firefox-20.0-30.0-38.0.1/T-bird-
RadioYachting. Conficker Test.

Return to Extension Development

Who is online

Users browsing this forum: No registered users and 1 guest