Created
July 24, 2019 03:12
-
-
Save AngusFu/bd9fd160bc8c2e9925004bfcd010b5f7 to your computer and use it in GitHub Desktop.
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
const addListener = HTMLElement.prototype.addEventListener | |
const removeListener = HTMLElement.prototype.removeEventListener | |
// click:stop:prevent | |
HTMLElement.prototype.addEventListener = function (name, handler, option) { | |
const re = /:(stop|prevent)$/ | |
const opt = {} | |
while (true) { | |
let n = name.replace(re, '') | |
if (n === name) { | |
break | |
} | |
name = n | |
opt[RegExp.$1] = true | |
} | |
addListener.call(this, name, function (e) { | |
if (opt.stop) e.stopPropagation() | |
if (opt.prevent) e.preventDefault() | |
handler.call(this, e) | |
}, option) | |
} | |
HTMLElement.prototype.removeEventListener = function (name, ...rest) { | |
const re = /:(stop|prevent)$/ | |
while (true) { | |
let n = name.replace(re, '') | |
if (n === name) { | |
break | |
} | |
name = n | |
} | |
removeListener.call(this, name, ...rest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment