Created
November 24, 2019 01:18
-
-
Save nolanlawson/0e18b8d7b5f6eb11554b5aa1fc4b5a4a to your computer and use it in GitHub Desktop.
Attempt to spy on addEventListener
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
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