2013-02-10 92 views
9

是否有任何方法可以让我检测按钮点击是由真实用户执行的,而不是用户加载到其浏览器开发者的一些自动方法(javascript)控制台或其他浏览器开发工具?检测按钮是否点击真实用户或由脚本触发

我尝试了以下方法建议在各种计算器的帖子,但他们都没有出现工作。 REF:How to detect if a click() is a mouse click or triggered by some code?

脚本检测方法尝试和失败:

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浏览器控制台上面这些都不是陷阱它的方法,按一下按钮作为被一个触发脚本。

var button = document.getElementById("btnSubmit"); 
button.click(); 

=========================================== ===============================

谢谢你所有的回复,并感谢提供这样一个伟大的计算器该网站有助于实现这么多的知识共享,并为我们节省了无数个小时的技术人员。

看来我还没有可靠的方法。所有3种浏览器(FF,IE & Chrome)均为用户提供开发者/控制台界面,以在我的网页上运行/注入JavaScript。看起来,每个浏览器的味道都会稍微区别一些事件属性值。例如:Chrome将脚本激活的cick和真实用户之间的差异用e.screenX捕捉,但在IE中:e.screenX对脚本单击(合成)和用户按钮单击具有相同的值点击

以下检测方法根本不起作用或在不同浏览器中不一致:e.isTrsuted e.originalEvent(event.source!= window)(e.distance!= null)

mousedown事件显示为只能由一个真正的用户按钮点击触发,但我必须假设除了点击按钮事件之外,还有一些脚本方法来模拟mousedown。 (五){alert('mouseisdown真正的按钮点击'); }

如果任何人都可以找出一种在多个浏览器上工作的可靠方法,它可以检测合成(脚本)按钮点击和用户点击按钮之间的区别,您将拥有超级英雄状态。

+0

截至目前,我看不到一个公认的答案。更糟糕的是,我不会接受。 – 2016-01-21 16:28:36

+1

对于firefox e.isTrusted的作品,不知道别人。 ** e.isTrsuted **不起作用;-) – 2016-02-07 15:34:27

回答

3

为了安全起见,当您使用javascript触发事件时,它的注册方式与用户触发事件的方式不同。控制台记录了对象,您将看到两种情况之间的显着差异。

mybutton.click(function(ev) { 
    console.log(ev); 
}); 

这里有两种情况下,一些示例输出:

jQuery.Event 
currentTarget: button 
data: null 
delegateTarget: button 
handleObj: Object 
isTrigger: true 
jQuery191011352501437067986: true 
namespace: "" 
namespace_re: null 
result: undefined 
target: button 
timeStamp: 1360472753601 
type: "mousedown" 
__proto__: Object 

jQuery.Event {originalEvent: MouseEvent, type: "mousedown", isDefaultPrevented:  function, timeStamp: 1360472840714, jQuery191011352501437067986: true…} 
altKey: false 
bubbles: true 
button: 0 
buttons: undefined 
cancelable: true 
clientX: 39 
clientY: 13 
ctrlKey: false 
currentTarget: button 
data: null 
delegateTarget: button 
eventPhase: 2 
fromElement: null 
handleObj: Object 
isDefaultPrevented: function returnFalse() { 
jQuery191011352501437067986: true 
metaKey: false 
offsetX: 37 
offsetY: 11 
originalEvent: MouseEvent 
pageX: 39 
pageY: 13 
relatedTarget: null 
screenX: 1354 
screenY: 286 
shiftKey: false 
target: button 
timeStamp: 1360472840714 
toElement: button 
type: "mousedown" 
view: Window 
which: 1 
__proto__: Object 
+0

这是一个拖动示例,该问题指出了有关点击事件的问题。 – 2013-02-10 04:28:57

+0

在button.click事件中的Chrome中:e显示(e.originalEvent.type =='mousedown')用于脚本点击和实际点击。是否还有其他属性在不同浏览器中会有所不同并保持一致? – user151402 2013-02-11 23:54:15

+0

[ev.isTrigger]怎么样(http://stackoverflow.com/questions/10704168/in-javascript-what-is-event-istrigger) – 2013-02-11 23:58:57

0
Fiddle

这里。

$("button").mousedown(function(e) { 
    if(e.which) { 
     alert(1); 
    } 
}); 
$("button").trigger("mousedown"); 

或JavaScript:

document.getElementsByTagName('button')[0].onmousedown = function(e) { 
if(e.which) { 
    alert(1); // original click 
} 
} 
document.getElementsByTagName('button')[0].onmousedown(); 

可以在拨弄看到,它不会让触发功能调用,但只有当鼠标向下按钮。如果在按钮上触发了自动点击,则e.which未定义,可以轻松捕捉。

UPDATE:Fiddle For Javascript

+0

好的小提琴,我只是添加了一个console.log,所以他可以看到像我这样的输出在我的答案中描述了他是否愿意。 [更新的小提琴](http://jsfiddle.net/332AA/1/) – 2013-02-10 03:49:12

+0

@KyleWeller您也可以将它添加到我的答案先生。 – 2013-02-10 03:51:16

+0

在这个问题中已经提到'e.which'技巧;它似乎不适用于OP。 – 2013-02-10 04:03:23

10
当按钮点击发生通过鼠标

中,事件e通常已经记录了鼠标指针的位置。尝试类似:

if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){ 
    alert("real button click"); 
    } 
+0

+1为简单起见:) – 2013-02-10 04:29:40

+0

适用于Chrome,但不适用于IE开发控制台。在IE中,e.screenX具有手动和脚本button.click的+值 – user151402 2013-02-11 21:09:51

5

不,在所有情况下都不可能。

如提到的其他答案,您可以查找鼠标坐标(clientX/Y和screenX/Y),如果它们不存在,则可以认为它是,可能是不是人为操作。

但是,如果用户在按钮上标签并使用空格键单击它,或以其他方式单击它而不使用鼠标,则坐标也将为零,并且此方法将错误地将其确定为脚本点击。

此外,如果脚本使用dispatchEvent而不是click,则可以为事件提供坐标。在这种情况下,此方法将错误地将其标识为用户生成的点击。

// This will produce what appears to be a user-generated click. 
function simulateClick(element) { 
    var evt = document.createEvent("MouseEvents"); 
    evt.initMouseEvent("click", true, true, window, 
    0, 110, 111, 10, 11, false, false, false, false, 0, null); 
    element.dispatchEvent(evt); 
} 

http://jsfiddle.net/bYq7m/

相关问题