Detect synthetic clicks on a webpage

前端 未结 6 1461
谎友^
谎友^ 2021-01-15 19:51

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

6条回答
  •  一向
    一向 (楼主)
    2021-01-15 20:14

    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)

提交回复
热议问题