Created
March 4, 2020 14:21
-
-
Save hayleyxyz/70aefb66109fbf74f497e139f19d8328 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getEventNames = (root) => { | |
let events = [ ]; | |
const objectHasSubPrototype = (object, comp) => { | |
let proto = Object.getPrototypeOf(object); | |
while(proto !== null && proto !== EventTarget) { | |
proto = Object.getPrototypeOf(proto); | |
} | |
return (proto !== null); | |
}; | |
const addEventNames = (propNames) => { | |
propNames.filter(x => x.match(/^on\w+$/)).forEach((propName) => { | |
propName = propName.substr(2); | |
if(events.indexOf(propName) === -1) { | |
events.push(propName); | |
} | |
}); | |
}; | |
Object.getOwnPropertyNames(root).forEach((name) => { | |
let value = root[name]; | |
if(value) { | |
if(objectHasSubPrototype(value, EventTarget)) { | |
let propNames = Object.getOwnPropertyNames(Object.getPrototypeOf(value).prototype); | |
addEventNames(propNames); | |
propNames = Object.getOwnPropertyNames(window); | |
addEventNames(propNames); | |
} | |
} | |
}); | |
return events; | |
}; | |
getEventNames(window).forEach((eventName) => { | |
window.addEventListener(eventName, (event) => console.log(eventName, event)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment