如何使用 javascript/jquery 自动按下 ENTER 键/确认警告框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13905043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:07:12  来源:igfitidea点击:

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

javascriptjqueryuserscripts

提问by Rpk_74

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.

我正在编写一个用户脚本(javascript/jquery)来自动化浏览器游戏中的一些事情。有一个拍卖,可以以折扣价购买产品。我想要一个在有 33% 折扣时自动购买商品的脚本。

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.

该脚本在拍卖 (greasemonkey) 的 url 上运行,然后检查是否有至少 33% 折扣的产品 - 如果是这种情况,它将按下该行中的按钮以购买该产品。

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?

我用谷歌搜索并检查了stackoverflow,人们说javascript/jquery不可能。真的是这样吗?这意味着在我正在玩的浏览器游戏中自动购买商品基本上是不可能的。我正在考虑让脚本自动按 ENTER 键,因为这与在警告框中单击“确定”相同。但这也是不可能的。所以现在我想知道:有没有办法自动化这个?

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:

万岁,它通过更改按钮的 onClick 属性来工作!!这是使用的代码:

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

$('元素').attr('某些属性','某些属性值');

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

现在可以关闭了,非常感谢大家,感谢您的帮助!!

回答by Anthony Sottile

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

根据浏览器的不同,可能会覆盖window.confirm诸如

(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.alertand window.confirmin firebug at the very least.

我没有做任何广泛的测试,但我至少能够覆盖window.alertwindow.confirm在 firebug 中。

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');)

请注意,如果他们的脚本已经获得了对警报/确认的引用(例如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.

另一种方法是覆盖您自动单击的按钮的功能,或者使用一些 xhr 手动发出 AJAX/POST。

回答by S?ren Lorentzen

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

使用 JavaScript,您可以以任何您喜欢的方式更改 HTML 和 JavaScript 代码。

I would recommend altering the OnClick function so that

我建议更改 OnClick 函数,以便

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

简单地变成

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

回答by mplungjan

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 thisand onclickbecause I want to replace the existing onclick handler, not add one

我使用this并且onclick因为我想替换现有的 onclick 处理程序,而不是添加一个

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

如果您只想购买,请获取 ID 并使用您的用户脚本提交表单

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

哪个将提交它找到的第一个。用 $.get 或 post 替换提交以提交所有打折的东西

回答by SaidbakR

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

您必须用 jquery 模态替换原始系统警报才能实现此类要求。

The following is a tutorial to introduce jquery modal:

下面是介绍jquery modal的教程:

http://www.Hymanlmoore.com/notes/jquery-modal-tutorial

http://www.Hymanlmoore.com/notes/jquery-modal-tutorial