-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
82 lines (58 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function _isEvent(prop) {
if (0 !== prop.indexOf('on')) {
return false;
}
return true;
}
function _getEvents(obj, hasOwnProperty = true) {
const result = [];
for (const prop in obj) {
if (!obj.hasOwnProperty(prop) && hasOwnProperty) {
continue;
}
if (_isEvent(prop)) {
// remove "on" at the beginning
result.push(prop.substr(2));
}
}
return result;
}
function getEvents(filter = '*', hasOwnProperty = true, noEmptyArrays = false, debug = false) {
const result = {};
const arr = Object.getOwnPropertyNames(window);
if ('*' === filter) {
filter = ''; // would always exist in any string possible
}
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (-1 === element.indexOf(filter)) {
continue;
}
let resultArray = [];
try {
const obj = window[element];
if (!obj || !obj['prototype']) {
continue;
}
proto = obj['prototype'];
resultArray = _getEvents(proto, hasOwnProperty);
} catch (err) {
if (debug) {
console.error(`failed to get events of %o`, element);
}
}
if (resultArray.length === 0 && noEmptyArrays) {
continue;
}
result[element] = resultArray;
}
if (-1 !== 'window'.indexOf(filter)) {
const resultArray = _getEvents(window, hasOwnProperty);
if (resultArray.length === 0 && noEmptyArrays) {
return result;
}
result['window'] = resultArray;
}
return result;
}
module.exports = getEvents;