MozillaZine

Modify URL before loading page in firefox

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

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

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

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

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

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?

sunkumar
 
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.

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

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

See http://stackoverflow.com/questions/5205672/modify-url-before-loading-page-in-firefox/ for a solution to this

LIMPET235
Moderator

User avatar
 
Posts: 34391
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.
Locking.
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: KilliK and 2 guests