Detect if button click real user or triggered by a script(检测按钮是否单击真实用户或由脚本触发)
问题描述
是否有任何方法可以让我检测按钮单击是否是由真实用户执行的,而不是用户已加载到其浏览器开发人员控制台或其他浏览器开发人员工具的某种自动化方法 (javascript)?
Is there any method that enables me to detect whether a button click was performed by a real user and not some automated method (javascript) that a user has loaded onto their browser developer console or other browser developer tool?
我尝试了各种 stackoverflow 帖子中建议的以下方法,但它们似乎都不起作用.参考:如何检测 click() 是鼠标点击还是由某些代码触发?
I tried the following methods suggested in various stackoverflow posts but none of them appear to work. REF: How to detect if a click() is a mouse click or triggered by some code?
脚本检测方法尝试并失败:
Script Detection methods tried and failed:
mybutton.click(function (e) {
if (!e.which) {
//Triggered by code NOT Actually clicked
alert(' e.which - not a real button click')
} else if ('isTrusted' in e && !e.isTrsuted) {
//Triggered by code NOT Actually clicked
alert(' e.isTrusted - not a real button click')
} else if (e.originalEvent === undefined) {
//Triggered by code NOT Actually clicked
alert(' e.originalEvent - not a realbutton click')
}
// else if (!e.focus) {
// //triggered // does not detect e.focus on a real btn click
// alert(' e.focus - not a realbutton click')
// }
else {
// Button hopefully clicked by a real user and not a script
}
})
如果我运行以下脚本以从 Chrome 浏览器控制台触发按钮单击,则上述方法都不会将其捕获为由脚本触发.
If I run the following script to trigger the button click from the Chrome browser console none of the methods above traps it as being triggered by a script.
var button = document.getElementById("btnSubmit");
button.click();
=============================================================================
==========================================================================
感谢您的所有回复,并感谢 stackoverflow 提供了如此出色的网站,促进了如此多的知识共享,并为我们的技术人员节省了无数小时.
Thank you for all your responses and thanks to stackoverflow for providing such a great site that facilitates so much knowledge sharing and for saving us techies an untold number of hours.
看来我还是没有可靠的方法.所有 3 种浏览器(FF、IE 和 Chrome)都为用户提供了开发人员/控制台界面,以便在我的网页上运行/注入 javascript.似乎每种浏览器风格捕获的一些事件属性值略有不同.例如:Chrome 使用 e.screenX 捕获脚本激活 cick 和真实用户之间的差异,但在 IE 中:e.screenX 对于脚本单击(合成)和用户按钮单击具有相同的值
It appears that I still do not have reliable method. All 3 browsers (FF, IE & Chrome) provide a developer/console interfaces for a user to run/inject a javascript on my webpage. It appears that each browser flavor traps some event property values a little differently. For example: Chrome traps the difference between a script activated cick and a real user with e.screenX but in IE: e.screenX has the same value for both a script click (synthetic) and a user button click
以下检测方法要么根本不起作用,要么在不同浏览器中不一致:e.which e.isTrsuted e.originalEvent (event.source != window) (e.distance != null)
The following detection methods either did not work at all or are inconsistent across the different browsers: e.which e.isTrsuted e.originalEvent (event.source != window) (e.distance != null)
mousedown 事件似乎只由真正的用户按钮单击触发,但我必须假设除了按钮单击事件之外还有一些脚本方法可以模拟 mousedown
The mousedown event appears to be only triggered by a real user button click, but I have to assume there is some script method to emulate a mousedown in addition to a button click event
$(me.container + ' .mybutton').mousedown(function (e) {
alert('mouseisdown real button click');
}
如果有人能找到一种可靠的方法,可以跨多个浏览器工作,检测合成(脚本)按钮点击和用户点击按钮之间的区别,那么你将获得超级英雄的地位.
If anyone can figure out a reliable method that works across multiple browsers, that detects the difference between a synthetic (script) button click and a button click by a user, you will deserve superhero status.
推荐答案
当通过鼠标点击按钮时,事件e通常会记录鼠标指针位置.尝试类似:
when a button click happens through the mouse, the event e usually has the mouse pointer location recorded. Try something like :
if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){
alert("real button click");
}
这篇关于检测按钮是否单击真实用户或由脚本触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测按钮是否单击真实用户或由脚本触发


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01