检测按钮是否单击真实用户或由脚本触发 [英] Detect if button click real user or triggered by a script

查看:12
本文介绍了检测按钮是否单击真实用户或由脚本触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以让我检测按钮单击是否是由真实用户执行的,而不是用户已加载到其浏览器开发人员控制台或其他浏览器开发人员工具的某种自动化方法 (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");
   }

这篇关于检测按钮是否单击真实用户或由脚本触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆