I want to write a script that works on Tampermonkey, the script manage on Chrome. And what

it does is that it create two bottoms on a page. When I click the first bottom, it would open a new window.

and that window would direct to page A, due to some reasons the page A won't show up (due to excess user for university course selection) instead it would direct to page B.

So my script would automatically keep redirecting to page A until the users are not full.


my code is the following, I assume yahoo.com is the page A, the page I eventually want. and google as the page B


// ==UserScript==
// @name name1
// @require http://ajax.googleapis.com/ajax/libs.../jquery.min.js
// @require https://gist.github.com/raw/2625891/...KeyElements.js
// @version 0.1
// ==/UserScript==

//bottom 1
var input=document.createElement("input");
input.type="button";
input.value= "method 1: click here";
input.onclick = fun1;
input.setAttribute("style", "font-size:15px;position:absolute;top:80px;right:690px;");
document.body.appendChild(input);

//bottom 2
var input2 = document.createElement("input");
input2.type="button";
input2.value="method 2: click here";
input2.onclick = fun2;
input2.setAttribute("style", "font-size:15px;position:absolute;top:110px;right:690px;");
document.body.appendChild(input2);


function fun1(){
window.open("https://www.yahoo.com/","test", "width=300, height=300");
method1();
}

function fun2(){
window.open("https://www.yahoo.com/","test2", "width=300, height=300");
method2();
}

function method1(){
if(window.location.href=="https://www.google.com/")
location.replace("https://www.yahoo.com/");
}

function method2(){
setTimeout(function() {location.href = "https://www.yahoo.com/"}, 1000);
}


So my goal is that when I click the first bottom, the script would use

if(window.location.href=="https://www.google.com/")
location.replace("https://www.yahoo.com/");

this to keep refreshing the new window page until it successfully loads the yahoo page , page A.
When I click the second bottom, it would use

setTimeout(function() {location.href = "https://www.yahoo.com/"}, 1000);

to keep refreshing the new window page until it successfully loads the yahoo page , page A.

When I only use only 1 method instead of 2 and without making those bottoms, it works absolutely fine. When I make two bottoms and my code would not work.

Can someone please help? Thanks so much