-
Notifications
You must be signed in to change notification settings - Fork 30
/
debug-on-property-write.js
79 lines (72 loc) · 1.93 KB
/
debug-on-property-write.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
import {
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isEmptyObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet debug-on-property-write
*
* @description
* This scriptlet is basically the same as [abort-on-property-write](#abort-on-property-write),
* but instead of aborting it starts the debugger.
*
* > It is not allowed for prod versions of filter lists.
*
* ### Examples
*
* ```adblock
* ! Aborts script when it tries to write in property `window.test`
* example.org#%#//scriptlet('debug-on-property-write', 'test')
* ```
*
* @added v1.0.4.
*/
/* eslint-enable max-len */
export function debugOnPropertyWrite(source, property) {
if (!property) {
return;
}
const rid = randomId();
const abort = () => {
hit(source);
debugger; // eslint-disable-line no-debugger
};
const setChainPropAccess = (owner, property) => {
const chainInfo = getPropertyInChain(owner, property);
let { base } = chainInfo;
const { prop, chain } = chainInfo;
if (chain) {
const setter = (a) => {
base = a;
if (a instanceof Object) {
setChainPropAccess(a, chain);
}
};
Object.defineProperty(owner, prop, {
get: () => base,
set: setter,
});
return;
}
setPropertyAccess(base, prop, { set: abort });
};
setChainPropAccess(window, property);
window.onerror = createOnErrorHandler(rid).bind();
}
export const debugOnPropertyWriteNames = [
'debug-on-property-write',
];
// eslint-disable-next-line prefer-destructuring
debugOnPropertyWrite.primaryName = debugOnPropertyWriteNames[0];
debugOnPropertyWrite.injections = [
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isEmptyObject,
];