检测网页上的合成点击 [英] Detect synthetic clicks on a webpage
问题描述
通过Javascript可以检测合成点击(点击不是由人类生成,而是使用JS或其他自动化工具生成的)。
Through Javascript is it possible to detect synthetic clicks (the clicks that were not generated by human but instead was generated using JS or some other automation tool) ?
推荐答案
您可以像其他浏览器一样使用vanilla Javascript,或者考虑使用jquery,以便轻松检测非人为点击。
You can use vanilla Javascript as in the other anwsers or consider using jquery so you can easily detect non-human clicks.
1。使用jquery定义全局点击事件处理程序。您可以捕获页面元素的所有点击。
1.Define a global click event handler using jquery.So you can catch all the clicks on page elements.
$(document).click(function(event) {
});
2.如果存在click事件,则检查event.originalEvent是否存在。
更多信息
2.Check if the event.originalEvent is exists if it exists click was performed by a human. More info
鼠标点击
$(document).click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
按键
$(document).keypress(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
以下是示例代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
$(document).keypress(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
$(document).click();
$(document).keypress();
});
</script>
</head>
<body>
<p>Click me !</p>
</body>
</html>
这篇关于检测网页上的合成点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!