Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically press ENTER key/Confirm alert box with javascript/jquery

I'm writing a userscript (javascript/jquery) to automate some things in a browser game. There's an auction where products can be bought with a discount. I want a script that automatically buys goods when it has 33% discount.

The script runs on the url of the auction (greasemonkey), then checks if there are products with at least 33% discount - if that's the case then it will press the button in that row to buy the product.

The problem I'm facing now is: Once you have pressed the button, you have to confirm you want to buy the goods via an alert box. Is there a way to automate this?

I've googled and also checked stackoverflow and people say it's not possible with javascript/jquery. Is this really the case? That would mean it's basically impossible to automate buying goods in the browser game i'm playing. I was thinking of letting the script automatically press ENTER because that would be the same as clicking 'Ok' in the alert box. But that also is impossible they say. So now i'm wondering: is there a way to automate this?

This is the code behind the button:

<input id="buybutton_3204781" class="button" type="button" onclick="if(confirm('Wil je deze producten kopen?')){document.submitForm.BuyAuctionNr.value=3204781; document.submitForm.submit();}{return false;}" value="Kopen">

EDIT:

Hooray, it works by changing the attribute onClick of the button!! This is the code used:

$('element').attr('some attribute','some attributes value');

Can be closed now, thanks alot guys, appreciate your help!!

like image 836
Rpk_74 Avatar asked Mar 27 '26 05:03

Rpk_74


1 Answers

Depending on the browser, it may be possible to overwrite window.confirm such as

(function() {
   'use strict';

    // Might was well save this in case you need it later
    var oldConfirm = window.confirm;
    window.confirm = function (e) {
        // TODO: could put additional logic in here if necessary
        return true;
    };

} ());

I didn't do any extensive testing, but I was able to override window.alert and window.confirm in firebug at the very least.

Note that this won't help you if their scripts have gained a reference to alert / confirm already (such as var a = window.confirm; a('herp');)

An alternate approach would be to override the function of the button you are auto clicking, or issue the AJAX / POST manually using some xhr.

like image 58
Anthony Sottile Avatar answered Mar 29 '26 17:03

Anthony Sottile