-3

Using Tampermonkey to change the behavior of a website. Have some problems with a website with the following code:

<script language="JavaScript">
        if (typeof jQuery != 'undefined') {
            jQuery(window).load(function() {
                 window.setTimeout(function() {
                     window.location.replace(window.location.href);
                 }, 180E3);
             });
         }
</script>

How does one remove/prevent it reloading the page?

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
Steamz
  • 13
  • 1

2 Answers2

0

Without messing that much with jQuery, if your script runs before that piece of code you can do the following:

jQuery.fn.load = function() {
  console.log("Tried to reload the page!")
}

// Then the following, outputs 'Tried to reload the page!' and does nothing else
jQuery(window).load(function() {
  // code
})

If you still need the load function afterwards you could do the following:

var oldLoad = jQuery.fn.load
var undesiredCallback = "function() {
             window.setTimeout(function() {
                 window.location.replace(window.location.href);
             }, 180E3);
         }"
jQuery.fn.load = function(callback) {
  if(callback.toString() !== undesiredCallback) {
    oldLoad(callback);
  }
}

But it's browser dependent and very unreliable

Another way would be adding a onbeforeunload event, but that would pop a prompt on your tab:

window.onbeforeunload = function() {
    return "Stay here please";
}
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
0

You can also override setTimeout functionallity.

var oldSetTimeout = window.setTimeout;
window.setTimeout = function(func,interval){ 
    if(typeof func === "function" && func.toString().indexOf('window.location.replace(window.location.href)') > -1){
        /*do nothing*/
    }else{ 
        oldSetTimeout(func,interval) 
    } 
}
yosiweinreb
  • 475
  • 4
  • 12