-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathremove-node-text.js
160 lines (153 loc) · 4.17 KB
/
remove-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
import {
observeDocumentWithTimeout,
handleExistingNodes,
handleMutations,
replaceNodeText,
isTargetNode,
parseNodeTextParams,
hit,
nodeListToArray,
getAddedNodes,
toRegExp,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet remove-node-text
*
* @description
* Removes text from DOM nodes.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/commit/2bb446797a12086f2eebc0c8635b671b8b90c477
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('remove-node-text', nodeName, textMatch[, parentSelector])
* ```
*
* - `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 whole text will be removed. Case sensitive.
* - `parentSelector` — optional, string, CSS selector to match parent node.
*
* ### Examples
*
* 1. Remove node's text content:
*
* ```adblock
* example.org#%#//scriptlet('remove-node-text', 'div', 'some text')
* ```
*
* ```html
* <!-- before -->
* <div>some text</div>
* <span>some text</span>
*
* <!-- after -->
* <div></div >
* <span>some text</span>
* ```
*
* 2. Remove node's text content, matching both node name and condition by RegExp:
*
* ```adblock
* example.org#%#//scriptlet('remove-node-text', '/[a-z]*[0-9]/', '/text/')
* ```
*
* ```html
* <!-- before -->
* <qrce3>some text</qrce3>
* <span>some text</span>
*
* <!-- after -->
* <qrce3></qrce3>
* <span>some text</span>
* ```
*
* 3. Remove node's text content, matching parent node:
*
* ```adblock
* example.org#%#//scriptlet('remove-node-text', '#text', 'some text', '.container')
* ```
*
* ```html
* <!-- before -->
* <div class="container">
* some text
* </div>
* <div class="section">
* some text
* </div>
* <!-- after -->
* <div class="container">
* </div>
* <div class="section">
* some text
* </div>
* ```
*
* @added v1.9.37.
*/
/* eslint-enable max-len */
export function removeNodeText(source, nodeName, textMatch, parentSelector) {
const {
selector,
nodeNameMatch,
textContentMatch,
} = parseNodeTextParams(nodeName, textMatch);
/**
* 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) {
const ALL_TEXT_PATTERN = /^.*$/s;
const REPLACEMENT = '';
replaceNodeText(source, node, ALL_TEXT_PATTERN, REPLACEMENT);
}
});
// Apply dedicated handler to already rendered nodes...
if (document.documentElement) {
handleExistingNodes(selector, handleNodes, parentSelector);
}
// and newly added nodes
observeDocumentWithTimeout((mutations) => handleMutations(mutations, handleNodes, selector, parentSelector));
}
export const removeNodeTextNames = [
'remove-node-text',
// aliases are needed for matching the related scriptlet converted into our syntax
'remove-node-text.js',
'ubo-remove-node-text.js',
'rmnt.js',
'ubo-rmnt.js',
'ubo-remove-node-text',
'ubo-rmnt',
];
// eslint-disable-next-line prefer-destructuring
removeNodeText.primaryName = removeNodeTextNames[0];
removeNodeText.injections = [
observeDocumentWithTimeout,
handleExistingNodes,
handleMutations,
replaceNodeText,
isTargetNode,
parseNodeTextParams,
// following helpers should be imported and injected
// because they are used by helpers above
hit,
nodeListToArray,
getAddedNodes,
toRegExp,
];