Repository navigation
Expand file tree
/
Copy pathtrusted-set-attr.js
More file actions
145 lines (138 loc) · 3.67 KB
/
Copy pathtrusted-set-attr.js
File metadata and controls
145 lines (138 loc) · 3.67 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
setAttributeBySelector,
observeDOMChanges,
nativeIsNaN,
defaultAttributeSetter,
logMessage,
isValidSelector,
isValidAttributeName,
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 > 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;
}
// Selector and attribute name are validated once, otherwise the error would be logged on each DOM change
if (!isValidSelector(selector)) {
logMessage(source, `Invalid selector arg: '${selector}'`);
return;
}
if (!isValidAttributeName(attr)) {
logMessage(source, `Invalid attribute name: '${attr}'`);
return;
}
/**
* Elements whose error has been logged, so it is not logged on each DOM change.
*/
const loggedFailedElems = new WeakSet();
const applyAttr = () => {
setAttributeBySelector(source, selector, attr, value, defaultAttributeSetter, loggedFailedElems);
};
applyAttr();
observeDOMChanges(applyAttr, 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,
isValidSelector,
isValidAttributeName,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
];