admin管理员组

文章数量:1294759

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!!

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!!

Share Improve this question edited Dec 16, 2012 at 22:05 Rpk_74 asked Dec 16, 2012 at 20:08 Rpk_74Rpk_74 412 gold badges2 silver badges5 bronze badges 4
  • When an alert box or prompt box is present in the browser window, it requires user interaction and cannot be simulated with javascript or jquery. You will need to find a new way around this. – Ohgodwhy Commented Dec 16, 2012 at 20:10
  • Look at a better tool: seleniumhq – epascarello Commented Dec 16, 2012 at 20:13
  • Have you identified the original source code that opens the confirmation dialog? Can you override the behavior with your own function? This is for a greasemonkey script to change some behavior for a specific browser game, right? Are you sure you want to "click OK automatically" or would you rather just not have the confirmation appear in the first place? Not sure "browser automation" is what you're looking for here... – No Results Found Commented Dec 16, 2012 at 20:13
  • Hey 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"> Maybe I can just change the onclick attribute so it doesn't pop up an confirmation box first and just executes the function? – Rpk_74 Commented Dec 16, 2012 at 20:15
Add a ment  | 

4 Answers 4

Reset to default 5

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.

With JavaScript, you have the ability to alter the HTML and JavaScript code in any way you like.

I would remend altering the OnClick function so that

<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">

simply bees

<input id="buybutton_3204781" class="button" type="button" 
    onclick=
    "document.submitForm.BuyAuctionNr.value=3204781; 
     document.submitForm.submit();" 
value="Kopen">

Without changing much you can try this

$(".button").each(function() {
  if (this.id.indexOf("buybutton")!=-1) this.onclick=function() {
    document.submitForm.BuyAuctionNr.value=this.id.replace("buybutton_","");
    document.submitForm.BuyAuctionNr.submit();
  } 
});

I use this and onclick because I want to replace the existing onclick handler, not add one

If you just want to buy, grab the IDs and submit the form with your user script

since i do not know how you know the discount, an example could be

$(".button").each(function() {
  if (this.id.indexOf("buybutton")!=-1) {
    var ID = this.id.replace("buybutton_","");
    if ($("#discount_"+ID).val()<30) {
      document.submitForm.BuyAuctionNr.value=ID;
      document.submitForm.BuyAuctionNr.submit();
    }
  } 
});

Which will submit the first it finds. Replace the submit with $.get or post to submit all the discounted stuff

You have to replace the original system alert by the jquery modal to achieve such requirement.

The following is a tutorial to introduce jquery modal:

http://www.jacklmoore./notes/jquery-modal-tutorial

本文标签: How to automatically press ENTER keyConfirm alert box with javascriptjqueryStack Overflow