Last active
December 24, 2018 11:04
-
-
Save GHolk/91598076a71d546811f3c033486378f8 to your computer and use it in GitHub Desktop.
prevent double click being click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name double click not click | |
// @namespace http://gholk.github.io/ | |
// @description prevent double click being click | |
// @version 6 | |
// @grant none | |
// ==/UserScript== | |
/* | |
when firefox on android use click then scroll to zoom, | |
first click event would send. second touchstart event | |
will not send, but will send touchmove event. | |
this script delay 0.3 second after every click, | |
if there are no any touchmove event detect in delay, | |
click event would be send again. | |
*/ | |
class Clicker { | |
constructor() { | |
this.lastClickTime = new Date() | |
this.delay = 0.3 // s | |
this.listener = clickEvent => this.click(clickEvent) | |
this.source = document | |
} | |
sleep(second = this.delay) { | |
return new Promise(wake => setTimeout(wake, second*1000)) | |
} | |
listen(source = this.source) { | |
source.addEventListener('click', this.listener, true) | |
} | |
unlisten(source = this.source) { | |
source.removeEventListener('click', this.listener, true) | |
} | |
nextTapIsZoom() { | |
return new Promise(decide => { | |
this.source.addEventListener( | |
'touchmove', | |
touch => decide(true), | |
{once: true, capture: true} | |
) | |
this.sleep().then(() => decide(false)) | |
}) | |
} | |
forceClick(clickEvent) { | |
this.unlisten() | |
const node = clickEvent.target | |
if (node.tagName == 'IMG') node.dispatchEvent(clickEvent) | |
else node.click() | |
this.listen() | |
} | |
async click(clickEvent) { | |
this.stopEvent(clickEvent) | |
const isZoom = await this.nextTapIsZoom() | |
if (!isZoom) this.forceClick(clickEvent) | |
} | |
stopEvent(event) { | |
event.stopImmediatePropagation() | |
event.preventDefault() | |
} | |
} | |
const clicker = new Clicker() | |
clicker.listen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment