-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-set-attr.js
123 lines (118 loc) · 3.04 KB
/
trusted-set-attr.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {
setAttributeBySelector,
observeDOMChanges,
nativeIsNaN,
defaultAttributeSetter,
logMessage,
throttle,
hit,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-set-attr
*
* @description
* Sets attribute with arbitrary value on the specified elements. This scriptlet runs once when the page loads
* and after that on DOM tree changes.
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-set-attr', selector, attr[, value])
* ```
*
* - `selector` — required, CSS selector, specifies DOM nodes to set attributes on
* - `attr` — required, attribute to be set
* - `value` — optional, the value to assign to the attribute, defaults to ''.
*
* ### Examples
*
* 1. Set attribute by selector
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-attr', 'div.class > a.class', 'test-attribute', '[true, true]')
* ```
*
* ```html
* <!-- before -->
* <div>
* <a>Another text</a>
* <a class="class">Some text</a>
* </div>
*
* <!-- after -->
* <div>
* <a>Another text</a>
* <a class="class" test-attribute="[true, true]">Some text</a>
* </div>
* ```
*
* 1. Set attribute without value
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-attr', 'a.class', 'test-attribute')
* ```
*
* ```html
* <!-- before -->
* <a class="class">Some text</div>
*
* <!-- after -->
* <a class="class" test-attribute>Some text</div>
* ```
*
* 1. Set attribute value to `MTIzNTY=`
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-attr', 'a.class', 'test-attribute', 'MTIzNTY=')
* ```
*
* ```html
* <!-- before -->
* <a class="class">Some text</div>
*
* <!-- after -->
* <a class="class" test-attribute="MTIzNTY=">Some text</div>
* ```
*
* 1. Set attribute value to `{ playback: false }`
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-attr', 'a.class', 'test-attribute', '{ playback: false }')
* ```
*
* ```html
* <!-- before -->
* <a class="class">Some text</div>
*
* <!-- after -->
* <a class="class" test-attribute="{ playback: false }">Some text</div>
* ```
*
* @added v1.10.1.
*/
/* eslint-enable max-len */
export function trustedSetAttr(source, selector, attr, value = '') {
if (!selector || !attr) {
return;
}
setAttributeBySelector(source, selector, attr, value);
observeDOMChanges(() => setAttributeBySelector(source, selector, attr, value), true);
}
export const trustedSetAttrNames = [
'trusted-set-attr',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedSetAttr.primaryName = trustedSetAttrNames[0];
trustedSetAttr.injections = [
setAttributeBySelector,
observeDOMChanges,
nativeIsNaN,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
];