-
Notifications
You must be signed in to change notification settings - Fork 30
/
abort-on-property-write.js
100 lines (92 loc) · 2.51 KB
/
abort-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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isEmptyObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet abort-on-property-write
*
* @description
* Aborts a script when it attempts to **write** the specified property.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#abort-on-property-writejs-
*
* Related ABP source:
* https://gitlab.com/eyeo/snippets/-/blob/main/source/behavioral/abort-on-property-write.js
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('abort-on-property-write', property)
* ```
*
* - `property` — required, path to a property (joined with `.` if needed).
* The property must be attached to `window`
*
* ### Examples
*
* ```adblock
* ! Aborts script when it tries to set `window.adblock` value
* example.org#%#//scriptlet('abort-on-property-write', 'adblock')
* ```
*
* @added v1.0.4.
*/
/* eslint-enable max-len */
export function abortOnPropertyWrite(source, property) {
if (!property) {
return;
}
const rid = randomId();
const abort = () => {
hit(source);
throw new ReferenceError(rid);
};
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 abortOnPropertyWriteNames = [
'abort-on-property-write',
// aliases are needed for matching the related scriptlet converted into our syntax
'abort-on-property-write.js',
'ubo-abort-on-property-write.js',
'aopw.js',
'ubo-aopw.js',
'ubo-abort-on-property-write',
'ubo-aopw',
'abp-abort-on-property-write',
];
// eslint-disable-next-line prefer-destructuring
abortOnPropertyWrite.primaryName = abortOnPropertyWriteNames[0];
abortOnPropertyWrite.injections = [
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isEmptyObject,
];