Riccardo
Riccardo

Reputation: 41

When timer reaches 0 click automatically on the button with Greasemonkey

Here is an example of a timer http://www.prezzipazzi.com/prodotto.php?id=1809 and I need a code that when the timer reaches 0 it click automatically on the button1809.

I tried this but it does not work:

var waitForZeroInterval = setInterval (CheckForZero, 0);
var hasRun = false;

function CheckForZero () {

    if ( (unsafeWindow.seconds == 0)  &&  (unsafeWindow.milisec == 0) )
    {
        clearInterval (waitForZeroInterval);

        var targButton  = document.getElementById ('bottone1809');
        var clickEvent  = document.createEvent ('MouseEvents');

        clickEvent.initEvent ('click', true, true);
        targButton.dispatchEvent (clickEvent);
    };

    if (!hasRun) {
        hasRun = true;
        setInterval(CheckForZero, 35000);
    };
}

Upvotes: 0

Views: 1801

Answers (2)

Brock Adams
Brock Adams

Reputation: 93443

Does it click the button one time and then stop, or not click the button at all?
It looks like the hasRun logic is wrong.

I can't test at that site since (1) it annoys me greatly, (2) It seems to require a registration -- which I will not do. So, please confirm that it operates like this:

  1. You login and load an auction page -- 1809 in this example.
  2. The 30-second timer starts. When it hits 0, you click.
  3. The timer resets -- Is there a delay? -- to a new 30-second countdown but the auction remains the same.
  4. Repeat, ad nauseam.

If all that is true, and the current code clicked the button once, then this should work:

var waitForZeroInterval = null;

function RestartZeroCheck () {
    waitForZeroInterval = setInterval (CheckForZero, 100);
}

function CheckForZero () {

    if ( (unsafeWindow.seconds == 0)  &&  (unsafeWindow.milisec == 0) )
    {
        clearInterval (waitForZeroInterval);

        var targButton  = document.getElementById ('bottone1809');
        var clickEvent  = document.createEvent ('MouseEvents');

        clickEvent.initEvent ('click', true, true);
        targButton.dispatchEvent (clickEvent);

        //--- After a short pause, start checking for the next timer to zero.
        setTimeout (RestartZeroCheck, 333);
    };
}

RestartZeroCheck ();


Note that if that works, then examining the page's structure suggests that this:

var targButton  = document.getElementById ('bottone1809');

Can be changed to this:

var targButton  = document.querySelector (
    "#left-inside div.post-wrapper td center input[id^='bottone']"
);

So that the auction number does not have to be hard-coded.

Upvotes: 1

dku.rajkumar
dku.rajkumar

Reputation: 18568

some more update:

HTML

<input type="button" name="button1" id="button1" onclick="do_some();"/>

script

var waitForZeroInterval = setInterval (function(){}, 0);
var hasRun = false;

function CheckForZero () {    
   clearInterval (waitForZeroInterval);
   jQuery('#button1').click();
 }

if (!hasRun) {
    hasRun = true;
    waitForZeroInterval  = setInterval(CheckForZero(), 1000);
    }

function do_some(){
alert("thank you");
}

This is working fine.. i have tested. i am getting that alert after 1 sec. The button is clicked and then the do_some function is called to display alert. You can check here run code

Upvotes: 0

Related Questions