Skip to content

Instantly share code, notes, and snippets.

@defianceblack
Forked from galpx/getEvents.js
Created August 25, 2022 01:28
Show Gist options
  • Save defianceblack/c4c5c20f3803d7ae2de0dd913e13cad1 to your computer and use it in GitHub Desktop.
Save defianceblack/c4c5c20f3803d7ae2de0dd913e13cad1 to your computer and use it in GitHub Desktop.

Revisions

  1. @galpx galpx created this gist Aug 5, 2019.
    56 changes: 56 additions & 0 deletions getEvents.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@

    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;
    }

    console.log(getEvents());