-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-replace-node-text.js
175 lines (167 loc) · 5.44 KB
/
trusted-replace-node-text.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
observeDocumentWithTimeout,
handleExistingNodes,
handleMutations,
replaceNodeText,
isTargetNode,
parseNodeTextParams,
logMessage,
hit,
nodeListToArray,
getAddedNodes,
toRegExp,
getTrustedTypesApi,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-replace-node-text
*
* @description
* Replaces text in text content of matched DOM nodes.
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-replace-node-text', nodeName, textMatch, pattern, replacement)
* ```
*
* - `nodeName` — required, string or RegExp, specifies DOM node name from which the text will be removed.
* Must target lowercased node names, e.g `div` instead of `DIV`.
* - `textMatch` — required, string or RegExp to match against node's text content.
* If matched, the `pattern` will be replaced by the `replacement`. Case sensitive.
* - `pattern` — required, string or regexp for matching contents of `node.textContent` that should be replaced.
* By default only first occurrence is replaced. To replace all occurrences use `g` flag in RegExp - `/pattern/g`.
* - `replacement` — required, string to replace text content matched by `pattern`.
* - `...extraArgs` — optional, string, if includes 'verbose' will log original and modified text content.
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* ### Examples
*
* 1. Replace node's text content:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-node-text', 'div', 'some', 'text', 'other text')
* ```
*
* ```html
* <!-- before -->
* <div>some text</div>
* <div>text</div>
* <span>some text</span>
*
* <!-- after -->
* <div>some other text</div>
* <div>text</div>
* <span>some text</span>
* ```
*
* 1. Replace node's text content, matching both node name, text and pattern by RegExp:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-node-text', '/[a-z]*[0-9]/', '/s\dme/', '/t\dxt/', 'other text')
* ```
*
* ```html
* <!-- before -->
* <qrce3>s0me t3xt</qrce3> // this node is going to be matched by both node name and text
* <qrce3>text</qrce3> // this node won't be matched by text content nor text content
* <span>some text</span>
*
* <!-- after -->
* <qrce3>s0me other text</qrce3> // text content has changed
* <qrce3>text</qrce3>
* <span>some text</span>
* ```
*
* 1. Replace all occurrences in node's text content, matching both node name and text:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-node-text', 'p', 'bar', '/a/g', 'x')
* ```
*
* ```html
* <!-- before -->
* <p>foa bar baz</p> // this node is going to be matched by both node name and text
*
* <!-- after -->
* <p>fox bxr bxz</p> // text content has changed
* ```
*
* 1. Replace node's text content and log original and modified text content:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-node-text', 'div', 'some', 'text', 'other text', 'verbose')
* ```
*
* @added v1.9.37.
*/
/* eslint-enable max-len */
export function trustedReplaceNodeText(source, nodeName, textMatch, pattern, replacement, ...extraArgs) {
const {
selector,
nodeNameMatch,
textContentMatch,
patternMatch,
} = parseNodeTextParams(nodeName, textMatch, pattern);
const shouldLog = extraArgs.includes('verbose');
/**
* Handles nodes by removing text content of matched nodes
*
* Note: instead of drilling down all the arguments for both replace-node-text
* and trusted-replace-node-text scriptlets, only the handler is being passed
*
* @param {Node[]} nodes nodes to handle
* @returns {void}
*/
const handleNodes = (nodes) => nodes.forEach((node) => {
const shouldReplace = isTargetNode(
node,
nodeNameMatch,
textContentMatch,
);
if (shouldReplace) {
if (shouldLog) {
const originalText = node.textContent;
if (originalText) {
logMessage(source, `Original text content: ${originalText}`);
}
}
replaceNodeText(source, node, patternMatch, replacement);
if (shouldLog) {
const modifiedText = node.textContent;
if (modifiedText) {
logMessage(source, `Modified text content: ${modifiedText}`);
}
}
}
});
// Apply dedicated handler to already rendered nodes...
if (document.documentElement) {
handleExistingNodes(selector, handleNodes);
}
// and newly added nodes
observeDocumentWithTimeout((mutations) => handleMutations(mutations, handleNodes));
}
export const trustedReplaceNodeTextNames = [
'trusted-replace-node-text',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedReplaceNodeText.primaryName = trustedReplaceNodeTextNames[0];
trustedReplaceNodeText.injections = [
observeDocumentWithTimeout,
handleExistingNodes,
handleMutations,
replaceNodeText,
isTargetNode,
parseNodeTextParams,
logMessage,
// following helpers should be imported and injected
// because they are used by helpers above
hit,
nodeListToArray,
getAddedNodes,
toRegExp,
getTrustedTypesApi,
];