简体   繁体   English

需要鼠标点击的输入字段

[英]Input fields needing mouse click

I'm building a simple HTML page, and I have an input field followed by a search button;我正在构建一个简单的 HTML 页面,我有一个输入字段,后跟一个搜索按钮; this is the code:这是代码:

<input type="text" id="sfield" placeholder="Write something">
<button id="search">Search!</button>

I'm currently writing the javascript to assign some actions to the button and to the input field, when I thought that it would be a good idea to add a feature that needs the cursor to be on the field for the search to start.我目前正在编写 javascript 来为按钮和输入字段分配一些操作,当时我认为添加一个需要光标位于字段上以便开始搜索的功能是个好主意。 I'll explain it better: if someone wants to search something, it will appear just like a normal input field and work like that.我会更好地解释它:如果有人想要搜索某些东西,它会像普通的输入字段一样出现并像这样工作。 However, if someone tries to launch a script for auto-submitting the form, it'll act like no input was inserted.但是,如果有人尝试启动用于自动提交表单的脚本,它会表现得好像没有插入任何输入。

For example, if someone tries to inject this script:例如,如果有人试图注入这个脚本:

document.getElementById('sfield').value="Some stuff";
document.getElementById('search').click();

the search would start but the "Some stuff" string wouldn't be saved, as if the user clicked the search button without writing in the search field.搜索将开始,但不会保存“Some stuff”字符串,就好像用户单击了搜索按钮而没有在搜索字段中写入内容。 Furthermore, adding the line此外,添加行

    document.getElementById('sfield').focus();

should also do nothing, so that the only way to put the cursor in the field would be a manual action by the user.也不应该做任何事情,以便将光标放在字段中的唯一方法是用户的手动操作。

I'm wondering if it's possible to make such thing;我想知道是否有可能做出这样的事情; I already managed to get the search field blank with EventListener, but I don't have a clue about making the script discern whether the user put the cursor on the field or not.我已经设法使用 EventListener 将搜索字段设为空白,但我不知道如何让脚本辨别用户是否将光标放在该字段上。

I'd prefer not using JQuery but it's ok also with it.我宁愿不使用 JQuery,但也可以使用它。 Any idea would be greatly accepted.任何想法都会被极大地接受。 Thanks.谢谢。

In that case, the program needs to retain state.在这种情况下,程序需要保留状态。 I'd do it like this...我会这样做...

http://jsbin.com/hopicucuyi/edit?js,console,output http://jsbin.com/hopicucuyi/edit?js,console,output

<input type="text" id="sfield" placeholder="Write something" data-validated = "false">
<button id="search">Search!</button>

<script>
   const inputField = document.getElementById('sfield');
const searchButton = document.getElementById('search');

inputField.onfocus = () => {
    inputField.setAttribute("data-validated", "true") 
}
searchButton.onclick = () => {
    const isValidated = inputField.getAttribute("data-validated");
    console.log('Is Validated: ' + isValidated);
}
</script>

Generally neither of these solutions will stop a bot that uses a browser (which is a bot that runs javascript).通常,这些解决方案都不会停止使用浏览器的机器人(即运行 javascript 的机器人)。 There is no 100% solution, but there are stronger solutions than checking if something has focus or not.没有 100% 的解决方案,但有比检查某件事是否有焦点更强大的解决方案。 Remember the javascript environment is completely controllable from the browser side of things.请记住,javascript 环境是完全可以从浏览器方面进行控制的。 For instance, all I would have to do to get past your security is change the state of the input in question to data-validated="true" and your security falls apart.例如,我需要做的就是通过您的安全性,将有问题的输入状态更改为data-validated="true"并且您的安全性崩溃。

However, the browser vendors have taken this possibility into account and provide you with a little-known solution.但是,浏览器供应商已经考虑到了这种可能性,并为您提供了一个鲜为人知的解决方案。 Look at the event data coming from a mouse click or a keystroke.查看来自鼠标单击或按键的事件数据。 This event data can be generated.可以生成该事件数据。 It used to be easier in the older browsers to spoof an event just by using new Event() , but now modern browsers use specific events for keyboard and mouse.过去在旧浏览器中仅使用new Event()更容易欺骗事件,但现在现代浏览器使用键盘和鼠标的特定事件。 The part that makes spoofing these very hard is that in the event there are properties that are read-only .使这些欺骗变得非常困难的部分是,如果存在只读属性。 More specifically there is an event property called "trusted" that cannot be set by javascript.更具体地说,有一个名为“trusted”的事件属性,它不能由 javascript 设置。 If you spoof a MouseEvent or KeyboardEvent , the "trusted" property is set to false, but if you use the actual mouse or keyboard "trusted" is set to true.如果您欺骗MouseEventKeyboardEvent ,则“trusted”属性设置为 false,但如果您使用实际的鼠标或键盘,则“trusted”设置为 true。

Info on the isTrusted property here: https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted有关 isTrusted 属性的信息,请访问: https : //developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted

Note: this is also not 100% as the bot can run a keyboard/mouse macro that creates genuine "trusted" events.注意:这也不是 100%,因为机器人可以运行键盘/鼠标宏来创建真正的“可信”事件。 However, it does make the system much harder to crack as many people dont know about this security trick, and most bots will not have mouse/keyboard control built in. If you encounter a bot that uses mouse/keyboard, you've reduced the security playing-field and further analysis of the events and interface usage can take place.然而,它确实使系统更难破解,因为很多人不知道这个安全技巧,而且大多数机器人不会内置鼠标/键盘控制。如果你遇到使用鼠标/键盘的机器人,你已经减少了可以进行安全竞争以及对事件和界面使用情况的进一步分析。

If you combine this method with other methods like browser fingerprinting etc. it makes your overall anti-bot security that much stronger and discourages many more possible bots.如果您将此方法与浏览器指纹识别等其他方法结合使用,它会使您的整体反机器人安全性更加强大,并阻止更多可能的机器人。

Edit: Don't just use this method and others, obfuscate your code so that any attacker will have to take the time to deobfuscate and wade through a bunch of poorly labeled functions and encrypted strings if they want to see how your security works.编辑:不要只使用这种方法和其他方法,混淆你的代码,这样任何攻击者都必须花时间去混淆并浏览一堆标记不佳的函数和加密字符串,如果他们想看看你的安全是如何工作的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM