Này người xa lạ!

Còn chờ gì nữa mà không mau đăng nhập hoặc đăng ký để cùng tham gia thảo luận với cộng đồng!

How to fix continuous reloading of a webpage by tamper monkey script?

I'm trying to build a script that will click 3 buttons in succession but after clicking the first button, the script goes into infinite reloading.

The site I'm trying to click buttons on: JDoodle
You need to sign-in to see the buttons. As you can see in the screenshots:

BUTTON 1:

BUTTON 2:

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        *.jdoodle.com/*
// @grant       none
// ==/UserScript==

'use strict';

  $ = jQuery;
  $(document).ready(function foo(){

    var node = document.querySelectorAll('a');
    node[8].click(); //clicking button 1

    node = document.querySelector('[title="User\'s Saved Files"]');
    node.click();    //clicking button 2
 }
);

Tried approaches:

1.

     unsafeWindow.r=0

     if(unsafeWindow.r==0){
        foo = function(){};
        unsafeWindow.r=1;
    }

Result : unsafeWindow.r not defined

2.

f = 1;
while(f){
   if(document.querySelector('[title="User\'s Saved Files"]'))
     f=0;
}

Result: Infinite reloading.

  1. Simply executing without approaches 1 & 2, no button is being clicked on by the script.

Bình luận

  • Yandex Browser
    After the button is clicked, the page is immediately navigated to the new URL, the old page environment is terminated, and your script runs again, and everything repeats. You need to check `location.pathname` string to detect where the code runs and make decisions what to click based on that.
  • đã sửa September 2018 Safari

    @wOxxOm said:
    After the button is clicked, the page is immediately navigated to the new URL, the old page environment is terminated, and your script runs again, and everything repeats. You need to check location.pathname string to detect where the code runs and make decisions what to click based on that.

    Actually, there are no redirections in this case as the URL remains same, "/online-compiler-c++14" in this case and the environment is changed with JS. I'm new at this and I don't understand how to fix it. Can you take a look at the site.

  • đã sửa September 2018 Chrome

    Use DevTools (network panel) and you'll see the page is navigated and loaded entirely each time and your script starts anew.

  • @wOxxOm said:
    Use DevTools (network panel) and you'll see the page is navigated and loaded entirely each time and your script starts anew.

    @wOxxOm said:
    Use DevTools (network panel) and you'll see the page is navigated and loaded entirely each time and your script starts anew.

    I've put a condition so that the 1st button is only clicked at initial page and not the next page by doing: if(location.pathname == "/online-compiler-c++14") for 1st click. But still it's reloading infinitely.

  • @wOxxOm said:
    Use DevTools (network panel) and you'll see the page is navigated and loaded entirely each time and your script starts anew.

    Can you tell me a solution, I already know the problem.
    If I try to click on 2nd button which is present in the 2nd page and not the 1st page, console gives an error:

    Failed to load resource: the server responded with a status of 404 ()

Đăng nhập hoặc Đăng ký để gửi bình luận.