Detect synthetic clicks on a webpage

2019-02-20 06:19发布

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) ?

5条回答
不美不萌又怎样
2楼-- · 2019-02-20 06:47

may be we can do something like this:

document.onmousedown = function(e) {
     if (typeof(e.pageX) !== 'undefined' && e.pageX != 0)
      {
        alert ('human');
      }else{
          alert ('else');
      }
    }

or a similar approach:

 document.onmousedown = function(e) {
             if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){
                 alert("real button click");
               }else{
               alert("something else");
               }
         }

see here:Detect if button click real user or triggered by a script

Note: if you want a jquery solution visit this: Check if event is triggered by a human

查看更多
Root(大扎)
3楼-- · 2019-02-20 06:54

You'd probably want to watch for mousemove events and click events. If you're catching click events, but there are no mousemove events, I'd think it's safe to say that robots are clicking on your site.

查看更多
地球回转人心会变
4楼-- · 2019-02-20 06:59

There is no reliable way to do this just by examining the page events.

You can certainly tell if an event was triggered by code or a "mouse click", however there is no way to know for certain if an actual human clicked the mouse, which I believe is what you're asking.

You could probably watch for mouse movements and analyze them to see if they contain enough artifacts of human muscle movement (non-linear movement), but if this works at all, I wouldn't expect it to last very long until the ad-fraud people catch on and add some randomization.

This is why Google and others have resorted to CAPTCHAs and other Turing-type tests.

查看更多
闹够了就滚
5楼-- · 2019-02-20 07:02

You can use vanilla Javascript as in the other anwsers or consider using jquery so you can easily detect non-human clicks.

1.Define a global click event handler using jquery.So you can catch all the clicks on page elements.

 $(document).click(function(event) {
  });

2.Check if the event.originalEvent is exists if it exists click was performed by a human. More info

For mouse clicks

 $(document).click(function(event) {
    if (event.originalEvent === undefined) {
        alert('not human')
    } else {
        alert(' human');
    }
 });

For keypress

$(document).keypress(function(event) {

    if (event.originalEvent === undefined) {
        alert('not human')
    } else {
        alert(' human');
    }
});

Here is a sample code.

<!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>
查看更多
Juvenile、少年°
6楼-- · 2019-02-20 07:10

This is an old question but the accepted answer uses jQuery and I'm sure a lot of visitors here are looking for a vanilla JS solution. If you don't need to support IE, you can use Event.isTrusted:

document.querySelector('a').onclick = function(event) {
    if('isTrusted' in event) {
        if(event.isTrusted === true) {
            console.log('clicked on by a human');
        } else if(event.isTrusted === false) {
            console.log('click was triggered by a script');
        }
    }
};

https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted

In IE, all events are trusted except those created by createEvent:

https://technet.microsoft.com/en-us/windows/ff974948(v=vs.60)

查看更多
登录 后发表回答