有没有使我能够检测是否点击一个按钮是由一个真实的用户和执行的不是一些自动化的方法(JavaScript)的用户已加载到自己的浏览器开发者控制台或其他浏览器的开发人员工具的任何方法?
我尝试以下方法,在不同的岗位计算器建议,但没有人似乎工作。 REF: 如何检测一个点击()是一个鼠标点击或通过一些代码触发?
脚本检测方法尝试和失败:
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();
================================================== ========================
感谢您对所有的答复和感谢StackOverflow上提供有利于这么多的知识共享这样一个伟大的网站,并救了我们技术人员的小时无数。
看来,我还没有可靠的方法。 所有3个浏览器(FF,IE和铬),为用户运行/在我的网页注入一个javascript提供一个开发者/控制台接口。 看来,每个浏览器的味道陷阱一些事件属性值不同的一点。 例如:铬捕获脚本激活依序按与e.screenX但在IE实际用户之间的差:e.screenX具有相同的值两者的脚本点击(合成的)和一个用户按一下按钮
下面的检测方法或者根本不工作或在不同的浏览器不一致:(!e.distance = NULL)e.which e.isTrsuted e.originalEvent(!event.source =窗口)
MouseDown事件似乎是由一个真实的用户点击按钮来触发只,但我必须假设有一些脚本方法,除了一个按钮单击事件来模拟鼠标按下
$(me.container + ' .mybutton').mousedown(function (e) {
alert('mouseisdown real button click');
}
如果任何人都可以弄清楚,在多个浏览器上运行的可靠方法,通过检测用户的合成(脚本)按钮,点击一个按钮,点击之间的区别,你会值得超级英雄的地位。
当点击一个按钮发生通过鼠标时,事件e通常具有记录的鼠标指针的位置。 尝试是这样的:
if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){
alert("real button click");
}
不,这不可能在所有情况下。
至于其他的答案中提到,你可以看看鼠标坐标(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/
出于安全考虑,当你触发了JavaScript它会注册不同比如果用户触发该事件的事件。 控制台日志ev
对象,你会看到两种情况之间的差异显著。
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
小提琴这里。
$("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: 小提琴为Javascript