Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created November 24, 2019 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/0e18b8d7b5f6eb11554b5aa1fc4b5a4a to your computer and use it in GitHub Desktop.
Save nolanlawson/0e18b8d7b5f6eb11554b5aa1fc4b5a4a to your computer and use it in GitHub Desktop.
Attempt to spy on addEventListener
window.__numDomListeners = 0
function spyAddListener (proto) {
const addEventListener = proto.addEventListener
proto.addEventListener = function () {
window.__numDomListeners++
console.log('add', this, arguments)
return addEventListener.apply(this, arguments)
}
}
function spyRemoveListener (proto) {
const removeEventListener = proto.removeEventListener
proto.removeEventListener = function () {
window.__numDomListeners--
console.log('remove', this, arguments)
return removeEventListener.apply(this, arguments)
}
}
function spy (proto) {
spyAddListener(proto)
spyRemoveListener(proto)
}
spy(Element.prototype)
spy(document)
spy(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment