1

I'm trying to make Greasemonkey script that clicks on a button at intervals and also clicks on it at the first load without wait.

I made this code which clicks every 120 seconds but the first time the page loads I have to wait 120 seconds for the code to click the GO button or I have to manually do it. :

window.setTimeout(function(){document.getElementById("masterPage_cphPageBody_btnGo").click()},120000)

and here is the source code related to it from the website

<input id="masterPage_cphPageBody_btnGo" name="masterPage$cphPageBody$btnGo" value="Go" onclick="SetSearchDate();showWaitpage();" class="btn2" type="submit">

How can I add a line to click on the GO the first time the page loads and 120 seconds there after?

the webpage before and after pressing the GO button has the same exact URL so adding the click on GO button without wait time will send hte page into a loop of URL loaded GO clicked
URL Loaded GO clicked URL Loaded GO clicked ...

checked the differences of the page before and after the first time GO button is clicked...

so I need the script to click Go button immidiately only if the following page does NOT show on the page

<span id="masterPage_cphPageBody_lblSelectAvailableTime" class="bodyBold">Select an available time</span>

but if "Select an available time" text show on page the script should wait 120 secon

I'm running: Greasemonkey, Windows 8 64, Firefox

doctorman
  • 13
  • 5

2 Answers2

1

Use a named function instead on an anonymous one (Often a good idea anyway).

Then use document.querySelector to check if that element is present. (That function has much more flexibility than getElementById.)

So your code becomes:

if (null === document.querySelector ("#masterPage_cphPageBody_lblSelectAvailableTime") ) {
    clickGoButton ();  //-- Initial, immediate click.
}

window.setTimeout (clickGoButton, 120000);

function clickGoButton () {
    document.getElementById ("masterPage_cphPageBody_btnGo").click ();
}
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • the webpage before and after pressing the GO button has the same exact URL so adding the click on GO button without wait time will send the page into a loop of URL loaded GO clicked URL Loaded GO clicked URL Loaded GO clicked ... – doctorman Jun 30 '16 at 22:38
  • Okay, see the updated answer to the revised question. – Brock Adams Jun 30 '16 at 23:14
  • I think I almost came up with the same solution look at my answer below – doctorman Jun 30 '16 at 23:21
  • Just beware that that kind of approach carries a big performance penalty. It shouldn't matter in this case, but it's a bad habit to get into. – Brock Adams Jun 30 '16 at 23:25
  • Why? you mean choosing the actual text vs. the span ID?.. coding is fun.. really enjoyed doing this little thing.. appreciate your help! – doctorman Jun 30 '16 at 23:28
  • if you have a minute I would really appreciate your help with my other question as well here [link](http://stackoverflow.com/questions/38124924/greasemonkey-submit-search) – doctorman Jun 30 '16 at 23:32
  • Yes, [processing raw HTML, with regex, has risks and complications](http://stackoverflow.com/a/1732454/331508) (not a big factor in your one case) and performs slower due to needing to search the whole raw text -- which browsers are not optimized for, versus a targeted DOM search -- which browsers optimize AMAP. – Brock Adams Jun 30 '16 at 23:32
  • As for your other question. That is not how SO works. We answer just what tickles our fancy. But at first glance, it looks like I should close that question as a duplicate of either [this](http://stackoverflow.com/questions/24025165/simulating-a-mousedown-click-mouseup-sequence-in-tampermonkey) or [this](http://stackoverflow.com/questions/15048223/choosing-and-activating-the-right-controls-on-an-ajax-driven-site). Try applying techniques from those 2 questions. – Brock Adams Jun 30 '16 at 23:35
  • Thank you. I understand. look at the 2 links and I could not figure it out. – doctorman Jun 30 '16 at 23:53
0

Considering the post and pre Go button URL are the same to avoid the boot loop I found a textual difference between post and pre click and used it to avoid the loop

here is the code that worked for me at last

if (/Select an available time/i.test (document.body.innerHTML) )
{
    window.setTimeout (clickGoButton, 120000);
}
else {
  clickGoButton ();
}


function clickGoButton () {
    document.getElementById ("masterPage_cphPageBody_btnGo").click ();
}
doctorman
  • 13
  • 5