3

I am trying to modify the script below to click a button that looks like this on a site:

<button id="checkPrice-02070" onclick="checkPrice(02070,null); return false;" class="orangeDark">
  <span>check price</span>
</button>

I am using the code below. So far, the page seems to keep reloading; nothing else happens.
Any advice to someone new?

(function () {
    window.addEventListener("load", function (e) {
        clickConfirmButton()
    }, false);
})();

function clickConfirmButton() {
    var buttons = document.getElementsByTagName('button');
    var clicked = false;
    for (var index = 0; (index < buttons.length);  index++) {
        if (buttons[index].value == "check price") {
            buttons[index].click();
            clicked = true;
            break;
        }
    }
    if (!clicked) {
        setTimeout("window.location.reload()", 300 * 1000);
    }
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
AAA
  • 2,388
  • 9
  • 32
  • 47

1 Answers1

-1

A <button>s value is not the visible text. You'd want to search textContent.

However:

  1. If that sample HTML is correct, you'd be better off searching for ids that start with checkPrice. See the code below.

  2. Are you sure you want to reload if the button is not found? If it is added by AJAX this is not the best approach. See this answer.

  3. Don't use setTimeout with a string (to evaluate) argument like that. See the code below.

  4. You do not need to wrap the code in an anonymous function.

Anyway, this should work, given the sample HTML:

window.addEventListener ("load", clickConfirmButton, false);

function clickConfirmButton (zEvent) {
    var button = document.querySelector ("button[id^='checkPrice']");
    if (button) {
        button.click ();
    }
    else {
        setTimeout (function () { location.reload(); }, 300 * 1000);
    }
}


To check the button text anyway, use:

function clickConfirmButton (zEvent) {
    var buttons = document.querySelectorAll ("button[id^='checkPrice']");
    var clicked = false;

    for (var index = 0, numBtn = buttons.length;  index < numBtn;  ++index) {
        if (/check price/i.test (buttons[index].textContent) ) {
            buttons[index].click();
            clicked = true;
            break;
        }
    }
    if (!clicked) {
        setTimeout (function () { location.reload(); }, 300 * 1000);
    }
}
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Excellent! It works but the page continously clicks the button without waiting any amount of time (even if I adjust the setTimeout to 300000 x 1000). Any advice on how to add additional wait? – AAA Jun 20 '12 at 11:38
  • You're saying that it clicks the button but then reloads the page anyway? Or somehow auto-clicks more than once? If either of those is true, then the question doesn't match the actual page, or the GM script has something else going on besides the indicated code. ... Link to the target page and post the complete script you are using. (Edit the question, or open anew question or use http://pastebin.com/.) – Brock Adams Jun 20 '12 at 12:29
  • @BrockAdams I don't understand the syntax with the ```/``` symbols in this context ```/check price/i.test```. Could you please point me to the correct resource to learn more. – bibscy Sep 24 '20 at 16:50