Is it possible to substitute "location.href" with invocation of my own function?

73 views Asked by At

I load a third-party game to my wrapper system, and this game redirects to different places with different parameters. What I need to do is not to allow it to happen, but to capture when it tries to, get the new string it sets "location.href" to and act accordingly. I can run my JS in the game's VM, after all game's code is executed.

Is it possible? I searched the Internet, of course, but didn't find any solution.


    /// here is the code I can not change, because it's a part of a 3rd party game I loaded:
    function goThere(){
       window.location.href = "google.com?some=thing";
    }

    /// here I can insert something, before the func above is invoked.
    var realLocation = window.location;
    /// I want the following function to be called every 
    ///time anyone attempts to change location.href:
    function onLocationChangeAttempt(newLocation){ 
       alert('Attempt to go to location '+newLocation+'! But we prevented it.'); 
    }
    window.location = { get:... set:...} // what goes here? It's the gist of my question.

    // and here it's invoked. 
    goThere();
1

There are 1 answers

0
gurvinder372 On

You can't change it, but you can override it

goThere = function()
{
   //your logic
}

You need to add this logic once the third party library has been loaded in your browser session.

Something like

document.addEventListener( "DOMContentLoaded", function(){
    goThere = function()
    {
       //your logic
    }   
});