Modify URL before loading page in firefox

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

Modify URL before loading page in firefox

Post by raid1141 »

Hi,

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

Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).addObserver({

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

            //request.cancel(Components.results.NS_BINDING_ABORTED);
            
            var standardURL = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIStandardURL);
            standardURL.spec = "chrome://mynewaddon/content/newfile.ext";
            
            var ioService = Components.classes["@mozilla.org/network/io-service;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?
thorc
Posts: 39
Joined: April 10th, 2011, 12:54 pm

Re: Modify URL before loading page in firefox

Post by thorc »

interesting.
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
     if(appcontent)
       appcontent.addEventListener("DOMContentLoaded", onPageLoad, true);
     var messagepane = document.getElementById("messagepane"); // mail
     if(messagepane)
       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)){
         return;
      }
         if( doc.location.href=="about:config" ){
               doc.location.replace("chrome://myextension/content/config.html");      
         }          
       
      // add event listener for page unload
     //  aEvent.originalTarget.defaultView.addEventListener("unload", function(event){ myExtension.onPageUnload(event); }, true);
  }
raid1141
Posts: 3
Joined: November 6th, 2011, 7:27 am

Re: Modify URL before loading page in firefox

Post by raid1141 »

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?
sunkumar
Posts: 3
Joined: March 24th, 2012, 6:53 am

Re: Modify URL before loading page in firefox

Post by sunkumar »

did you find the solution for this issue? I have a similar use case and stuck with this.
d2mw
Posts: 2
Joined: February 16th, 2013, 6:25 pm

Re: Modify URL before loading page in firefox

Post by d2mw »

See http://stackoverflow.com/questions/5205672/modify-url-before-loading-page-in-firefox/ for a solution to this
User avatar
LIMPET235
Moderator
Posts: 39933
Joined: October 19th, 2007, 1:53 am
Location: The South Coast of N.S.W. Oz.

Re: Modify URL before loading page in firefox

Post by LIMPET235 »

Please don't bump these old threads.
Thank you.
Locking.
[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