2013-02-10 91 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/

相關問題