简体   繁体   English

如何绕过 javascript 文本框的按键要求?

[英]How can I get around a keypress requirement on a javascript textbox?

Hello and thanks for reading.您好,感谢您的阅读。

So I am trying to automatically log into this form using Chrome.因此,我正在尝试使用 Chrome 自动登录此表单。

https://app.patientaccess.com/login https://app.patientaccess.com/login

I use my own Chrome extension to do this.我使用自己的 Chrome 扩展程序来执行此操作。 It's basically just a javascript file that fills in the fields automatically.它基本上只是一个自动填写字段的 javascript 文件。 I use it for a range of websites.我将它用于一系列网站。

For example, this is my function for filling in a textbox:例如,这是我的 function 用于填写文本框:

function fld(param, val){try{document.querySelectorAll(param)[0].value = val} catch(e){}}

And I call it like this:我这样称呼它:

fld("input[id='loginForm-email']", "mail@myemail.com")

For most websites it works.对于大多数网站,它都有效。 But for the Patient Access website above, it doesn't and I think the reason is because the password textbox requires a physical keypress (or listening for a change event or something).但是对于上面的 Patient Access 网站,它没有,我认为原因是密码文本框需要物理按键(或侦听更改事件或其他内容)。

When I run my script, it fills in the textboxes just fine but it doesn't let me click next because, even though the textboxes appear to be filled in, the webpage knows that I haven't actually pressed any keys.当我运行我的脚本时,它会很好地填充文本框,但它不允许我单击下一步,因为即使文本框似乎已填充,网页也知道我实际上没有按下任何键。

To get around it, I have to have my script fill in the form, then click in the password box, press space, delete the space I just entered and then it lets me sign in because it knows I have physically pressed a key in the password box.为了解决这个问题,我必须让我的脚本填写表格,然后单击密码框,按空格,删除我刚刚输入的空格,然后它让我登录,因为它知道我已经物理按下了密码框。

So my question is how can I make my automatic sign in javascript work on this website?所以我的问题是如何让我的自动登录 javascript 在这个网站上工作?

I don't think a script can use keypress events to type into a textbox but that's ok because I can fill in the textbox programmatically (using the fld function above).我认为脚本不能使用按键事件在文本框中键入内容,但这没关系,因为我可以以编程方式填写文本框(使用上面的 fld function)。 But how can I convince the page that I have typed into the password textbox so it lets me submit the form?但是我怎样才能说服我在密码文本框中输入的页面,以便它让我提交表单?

I am using JavaScript as I said and I can include jQuery if needed.正如我所说,我正在使用 JavaScript,如果需要,我可以包括 jQuery。

Thanks.谢谢。

After much trial and error, I was able to solve it with JavaScript using this:经过多次试验和错误,我能够使用 JavaScript 解决它:

var fireOnThis = document.querySelectorAll("[type='text']")[0]
const changeEvent = new Event('input', { bubbles: true, cancelable: true });
fireOnThis.dispatchEvent(changeEvent);

It works absolutely beautifully.它工作得非常漂亮。 I turned this into a function and all I do is run this function after automatically filling in the text field (it doesn't work before filling in the field, has to be after).我把它变成了 function ,我所做的就是在自动填写文本字段后运行这个 function (在填写字段之前它不起作用,必须在之后)。

I tried many other solutions on other websites and none worked but this one does.我在其他网站上尝试了许多其他解决方案,但没有一个有效,但这个有效。 Whee!呸!

I got the concept from a Chrome Extension called Form Filler .我从一个名为Form Filler的 Chrome 扩展中得到了这个概念。 I tried the extension on the form I was trying to automate and to my surprise, it filled in the form without issue.我在我试图自动化的表单上尝试了扩展,令我惊讶的是,它毫无问题地填写了表单。 So I looked at the source code to find out how it did it and I found this:所以我查看了源代码以了解它是如何做到的,我发现了这个:

['input', 'click', 'change', 'blur'].forEach(event => {
  const changeEvent = new Event(event, { bubbles: true, cancelable: true });
  element.dispatchEvent(changeEvent);
});

So I just converted that into the code in my answer and it worked a treat.因此,我只是将其转换为答案中的代码,并且效果很好。 So credit should really go to Hussein Shabbir, the developer of Form Filler.因此,真正应该归功于 Form Filler 的开发者 Hussein Shabbir。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中的按键事件上获取文本框的值 - How to get the value of a textbox on keypress event in javascript Javascript - 在每个按键时从文本框中获取值 - Javascript - get value from textbox at every keypress 如何获取 javascript 中给出的 KeyPress 方法中当前 td 列值的值 - How can I get the value of current td column value on KeyPress method given in javascript 如何使用Javascript在Safari中生成按键事件? - How can I generate a keypress event in Safari with Javascript? 如何在Javascript中模拟“ CTRL + S”按键 - How can I simulate “CTRL +S” keypress in Javascript 如何在每次按键时让typeAhead正常工作 - How can I get typeAhead to work everytime on first keypress 如何让Eclipse在一次按键中启动Ant? - How can I get Eclipse to launch Ant in one keypress? 如何获得jquery .val()AFTER按键事件? - How can I get jquery .val() AFTER keypress event? 如何使用 prompt-sync 获取 1 keypress 的值? - How can I use prompt-sync to get the value of 1 keypress? 我如何验证按键上的文本框以仅允许 blazor 服务器中的字母数字字符 - How can i validate textbox on keypress to allow only alphanumeric characters in blazor server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM