Skip to content

Instantly share code, notes, and snippets.

@onmax
Last active February 8, 2022 19:42
Show Gist options
  • Save onmax/5179d9bf1e936be68ddaaf245073b86c to your computer and use it in GitHub Desktop.
Save onmax/5179d9bf1e936be68ddaaf245073b86c to your computer and use it in GitHub Desktop.
List all active events in a page
// More info at:
// https://www.perimeterx.com/tech-blog/2019/list-every-event-that-exists-in-the-browser/
// Just run it in developer console:
function _isEvent(prop) {
if (0 !== prop.indexOf("on")) {
return false;
}
return true;
}
function _getEvents(obj) {
var result = [];
for (var prop in obj) {
if (_isEvent(prop)) {
prop = prop.substr(2); // remove "on" at the beginning
result.push(prop);
}
}
return result;
}
function getEvents() {
const result = {};
result["window"] = _getEvents(window, hasOwnProperty);
const arr = Object.getOwnPropertyNames(window);
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
let resultArray = [];
try {
const obj = window[element];
if (!obj || !obj["prototype"]) {
continue;
}
proto = obj["prototype"];
resultArray = _getEvents(proto);
} catch (err) {
// console.error(`failed to get events of %o`, element);
}
result[element] = resultArray;
}
return result;
}
events = getEvents()
const nEvents = Object.values(events).map(e => e.length).reduce((partialSum, a) => partialSum + a, 0)
console.log(`There are ${nEvents} events in your page`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment