RoBo
RoBo

Reputation: 91

Intercept a link request in javascript

I have a script that parse the web and change the urls to my site to make anonymous surfing. So for example from a source site www.externalsite.com I replace attribute like

href="images/logo.png"

with

href="http://www.myanonymsite.com/getpage=www.externalsite.com/images/logo.png"

Everything goes well with href, src, etc... attributes or any other full path url. What is hard is to intercept the url that are construct by cryptic customs javascript functions like:

var u = "images";
var i = document.createElement("img");
var ir = Xd(i, u, "logo.png");
Xd = function(i, u , a) {
    i.src = "/" + u + "/" + a;
    return i;
};

It is not possible to convert that url before it is built, but is there a way to intercept this request event to modify the url before it is sent to the browser?

Upvotes: 3

Views: 4972

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

you can also intercept the link click after they are rendered by overriding their default behavior.

Use this method when you want to alter the link on click

function alterLinks()
{
   $( document ).on( "click", "a", function(e){

      e.preventDefault();

      var href = $( this ).attr( "href" );
      //put the logic to update the href

      //finally relocate to that URL as you would have intended to

      location.href = href;

   } );

}

Upvotes: 4

Related Questions