Skip to content

Instantly share code, notes, and snippets.

@AngusFu
Created July 24, 2019 03:12
Show Gist options
  • Save AngusFu/bd9fd160bc8c2e9925004bfcd010b5f7 to your computer and use it in GitHub Desktop.
Save AngusFu/bd9fd160bc8c2e9925004bfcd010b5f7 to your computer and use it in GitHub Desktop.
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