-
Notifications
You must be signed in to change notification settings - Fork 30
/
log-addEventListener.js
89 lines (82 loc) · 2.63 KB
/
log-addEventListener.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
83
84
85
86
87
88
89
import {
hit,
validateType,
validateListener,
listenerToString,
convertTypeToString,
logMessage,
objectToString,
isEmptyObject,
} from '../helpers';
/**
* @scriptlet log-addEventListener
*
* @description
* Logs all addEventListener calls to the console.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#addeventlistener-loggerjs-
*
* ### Syntax
*
* ```adblock
* example.org#%#//scriptlet('log-addEventListener')
* ```
*
* @added v1.0.4.
*/
export function logAddEventListener(source) {
const nativeAddEventListener = window.EventTarget.prototype.addEventListener;
function addEventListenerWrapper(type, listener, ...args) {
if (validateType(type) && validateListener(listener)) {
const message = `addEventListener("${type}", ${listenerToString(listener)})`;
logMessage(source, message, true);
hit(source);
} else {
// logging while debugging
const message = `Invalid event type or listener passed to addEventListener:
type: ${convertTypeToString(type)}
listener: ${convertTypeToString(listener)}`;
logMessage(source, message, true);
}
// Avoid illegal invocations due to lost context
// https://github.com/AdguardTeam/Scriptlets/issues/271
let context = this;
if (this && this.constructor?.name === 'Window' && this !== window) {
context = window;
}
return nativeAddEventListener.apply(context, [type, listener, ...args]);
}
const descriptor = {
configurable: true,
set: () => {},
get: () => addEventListenerWrapper,
};
// https://github.com/AdguardTeam/Scriptlets/issues/215
// https://github.com/AdguardTeam/Scriptlets/issues/143
Object.defineProperty(window.EventTarget.prototype, 'addEventListener', descriptor);
Object.defineProperty(window, 'addEventListener', descriptor);
Object.defineProperty(document, 'addEventListener', descriptor);
}
export const logAddEventListenerNames = [
'log-addEventListener',
// aliases are needed for matching the related scriptlet converted into our syntax
'addEventListener-logger.js',
'ubo-addEventListener-logger.js',
'aell.js',
'ubo-aell.js',
'ubo-addEventListener-logger',
'ubo-aell',
];
// eslint-disable-next-line prefer-destructuring
logAddEventListener.primaryName = logAddEventListenerNames[0];
logAddEventListener.injections = [
hit,
validateType,
validateListener,
listenerToString,
convertTypeToString,
logMessage,
objectToString,
isEmptyObject,
];