0

I have followed this thread and it's excellent: How do I click on this button with Greasemonkey?

But I need a script on Greasemonkey to:

  1. Start script
  2. Each 1 minute script reloads page (so, *setTimeout(function(){ location.reload(); }, 60*1000);* )
    • IF, after reload, on the page appears the button with text "Eureka", click on button and end script
    • IF NOT appears the button with text "Eureka", again reload page, check for button with that specific text, if still not appears reload page etc

Code script to click on button when text appears, and it works without problems:

// ==UserScript==
// @name     _Click on a specific link
// @include  http://www.mypage.com*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change
introduced in GM 1.0.
It restores the sandbox.
*/

//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements ("span:contains('Eureka')", clickOnFollowButton);

function clickOnFollowButton (jNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

I think I need use conditionals; but I don't know where and how. :(

I tried:

// ==UserScript==
// @name     _Click on a specific link
// @include  http://www.mypage.com*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change
introduced in GM 1.0.
It restores the sandbox.
*/
//--- Note that contains() is CASE-SENSITIVE.

var FREQUENCY = 60*1000;

function clickOnFollowButton (jNode) {
    if ("span:contains('Eureka')") {
        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        jNode[0].dispatchEvent (clickEvent);
    } 
    else {
        window.setTimeout(window.location.reload, FREQUENCY);
    }
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
noire
  • 9
  • 2

1 Answers1

0

I can boil the question's stated logic down to:

  1. Wait for the button for 1 minute.
  2. If it's found, click it.
  3. Otherwise reload the page.

If that's true, then simplified logic can be used.
BUT, If clicking the button also causes the page itself to reload, or if something else is going on, then you will need to use GM_setValue to transmit state information from one script instance to the next. (Each reload starts the script over, same as a new page load.)

Next, the second code-block doesn't work for a few reasons, and it checks for the element right away, not allowing it to AJAX-in. It needs to check at the end of the timer run.

The simplest way to do that is with a global state variable.

Code like this should work, based on the question, and provided you haven't omitted "But" criteria as listed above:

// ==UserScript==
// @name     _Click on a specific link, refresh if it doesn't appear
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.

const reloadFrequency   = 60*1000;  //  60 seconds
let buttonWasFound      = false;

//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements ("span:contains('Eureka')", clickOnFollowButton);

setTimeout (reloadOnlyIfButtonMissing, reloadFrequency);

function clickOnFollowButton (jNode) {
    buttonWasFound  = true;

    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

function reloadOnlyIfButtonMissing () {
    if ( ! buttonWasFound) {
        location.reload;  //  This kills script instance.
    }
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thank you very much Brock, script works but I don't understand if there's refresh of page. It seems not refresh the page if button isn't found. If button is found, instead, it clicks and works all well. When button is clicked, the page itself not reload but it goes to another page (button is a link), so I don't need GM_setValue I think. But maybe I need it for refresh page when button isn't found? – noire Nov 14 '17 at 11:08
  • Something you are not showing us is the problem. On a page that didn't refresh, type `jQuery("span:contains('Eureka')").length` in the console. What is the result? – Brock Adams Nov 14 '17 at 20:15