@tokioapp/adblock 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/THIRD_PARTY_NOTICES.md +50 -0
- package/dist/content/cosmetics.d.ts +22 -0
- package/dist/content/cosmetics.js +173 -0
- package/dist/content/mainWorld.d.ts +8 -0
- package/dist/content/mainWorld.js +10 -0
- package/dist/content/preload.d.ts +1 -0
- package/dist/content/preload.js +34 -0
- package/dist/content/procedural.d.ts +19 -0
- package/dist/content/procedural.js +212 -0
- package/dist/cosmetic/engine.d.ts +95 -0
- package/dist/cosmetic/engine.js +268 -0
- package/dist/electron/blocker.d.ts +84 -0
- package/dist/electron/blocker.js +320 -0
- package/dist/electron/channels.d.ts +10 -0
- package/dist/electron/channels.js +6 -0
- package/dist/electron/scheme.d.ts +21 -0
- package/dist/electron/scheme.js +21 -0
- package/dist/engine.d.ts +86 -0
- package/dist/engine.js +163 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/lists/defaults.d.ts +6 -0
- package/dist/lists/defaults.js +59 -0
- package/dist/lists/manager.d.ts +86 -0
- package/dist/lists/manager.js +154 -0
- package/dist/lists/preprocessor.d.ts +21 -0
- package/dist/lists/preprocessor.js +152 -0
- package/dist/lists/storage.d.ts +37 -0
- package/dist/lists/storage.js +66 -0
- package/dist/network/engine.d.ts +105 -0
- package/dist/network/engine.js +431 -0
- package/dist/network/filterIndex.d.ts +40 -0
- package/dist/network/filterIndex.js +343 -0
- package/dist/network/modifiers.d.ts +23 -0
- package/dist/network/modifiers.js +192 -0
- package/dist/network/pattern.d.ts +7 -0
- package/dist/network/pattern.js +145 -0
- package/dist/network/request.d.ts +50 -0
- package/dist/network/request.js +136 -0
- package/dist/network/tokens.d.ts +5 -0
- package/dist/network/tokens.js +150 -0
- package/dist/parser/cosmetic.d.ts +21 -0
- package/dist/parser/cosmetic.js +171 -0
- package/dist/parser/hosts.d.ts +20 -0
- package/dist/parser/hosts.js +143 -0
- package/dist/parser/index.d.ts +40 -0
- package/dist/parser/index.js +158 -0
- package/dist/parser/network.d.ts +6 -0
- package/dist/parser/network.js +474 -0
- package/dist/parser/selector.d.ts +25 -0
- package/dist/parser/selector.js +522 -0
- package/dist/parser/types.d.ts +175 -0
- package/dist/parser/types.js +75 -0
- package/dist/preload.cjs +1988 -0
- package/dist/psl/psl.d.ts +32 -0
- package/dist/psl/psl.js +119 -0
- package/dist/resources/binary.d.ts +9 -0
- package/dist/resources/binary.js +54 -0
- package/dist/resources/defusers.d.ts +29 -0
- package/dist/resources/defusers.js +180 -0
- package/dist/resources/documents.d.ts +12 -0
- package/dist/resources/documents.js +56 -0
- package/dist/resources/ima.d.ts +6 -0
- package/dist/resources/ima.js +313 -0
- package/dist/resources/index.d.ts +14 -0
- package/dist/resources/index.js +85 -0
- package/dist/resources/surrogates.d.ts +30 -0
- package/dist/resources/surrogates.js +486 -0
- package/dist/scriptlets/abort.d.ts +5 -0
- package/dist/scriptlets/abort.js +97 -0
- package/dist/scriptlets/dom/attributes.d.ts +5 -0
- package/dist/scriptlets/dom/attributes.js +92 -0
- package/dist/scriptlets/dom/click.d.ts +2 -0
- package/dist/scriptlets/dom/click.js +107 -0
- package/dist/scriptlets/dom/cookies.d.ts +16 -0
- package/dist/scriptlets/dom/cookies.js +194 -0
- package/dist/scriptlets/dom/createHtml.d.ts +2 -0
- package/dist/scriptlets/dom/createHtml.js +27 -0
- package/dist/scriptlets/dom/dialogs.d.ts +3 -0
- package/dist/scriptlets/dom/dialogs.js +21 -0
- package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
- package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
- package/dist/scriptlets/dom/index.d.ts +3 -0
- package/dist/scriptlets/dom/index.js +38 -0
- package/dist/scriptlets/dom/navigation.d.ts +5 -0
- package/dist/scriptlets/dom/navigation.js +60 -0
- package/dist/scriptlets/dom/nodeText.d.ts +3 -0
- package/dist/scriptlets/dom/nodeText.js +98 -0
- package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
- package/dist/scriptlets/dom/spoofCss.js +58 -0
- package/dist/scriptlets/dom/storage.d.ts +5 -0
- package/dist/scriptlets/dom/storage.js +50 -0
- package/dist/scriptlets/dom/watch.d.ts +8 -0
- package/dist/scriptlets/dom/watch.js +41 -0
- package/dist/scriptlets/events.d.ts +2 -0
- package/dist/scriptlets/events.js +42 -0
- package/dist/scriptlets/helpers.d.ts +58 -0
- package/dist/scriptlets/helpers.js +206 -0
- package/dist/scriptlets/index.d.ts +6 -0
- package/dist/scriptlets/index.js +47 -0
- package/dist/scriptlets/network/edit.d.ts +9 -0
- package/dist/scriptlets/network/edit.js +104 -0
- package/dist/scriptlets/network/eval.d.ts +3 -0
- package/dist/scriptlets/network/eval.js +22 -0
- package/dist/scriptlets/network/fetch.d.ts +10 -0
- package/dist/scriptlets/network/fetch.js +145 -0
- package/dist/scriptlets/network/index.d.ts +3 -0
- package/dist/scriptlets/network/index.js +43 -0
- package/dist/scriptlets/network/jsonPath.d.ts +13 -0
- package/dist/scriptlets/network/jsonPath.js +482 -0
- package/dist/scriptlets/network/media.d.ts +3 -0
- package/dist/scriptlets/network/media.js +97 -0
- package/dist/scriptlets/network/methods.d.ts +5 -0
- package/dist/scriptlets/network/methods.js +139 -0
- package/dist/scriptlets/network/prune.d.ts +13 -0
- package/dist/scriptlets/network/prune.js +194 -0
- package/dist/scriptlets/network/rendering.d.ts +4 -0
- package/dist/scriptlets/network/rendering.js +46 -0
- package/dist/scriptlets/network/replace.d.ts +5 -0
- package/dist/scriptlets/network/replace.js +168 -0
- package/dist/scriptlets/network/request.d.ts +19 -0
- package/dist/scriptlets/network/request.js +124 -0
- package/dist/scriptlets/network/util.d.ts +19 -0
- package/dist/scriptlets/network/util.js +80 -0
- package/dist/scriptlets/network/webrtc.d.ts +3 -0
- package/dist/scriptlets/network/webrtc.js +62 -0
- package/dist/scriptlets/network/windowOpen.d.ts +2 -0
- package/dist/scriptlets/network/windowOpen.js +28 -0
- package/dist/scriptlets/network/xhr.d.ts +9 -0
- package/dist/scriptlets/network/xhr.js +177 -0
- package/dist/scriptlets/runner.d.ts +8 -0
- package/dist/scriptlets/runner.js +19 -0
- package/dist/scriptlets/setConstant.d.ts +5 -0
- package/dist/scriptlets/setConstant.js +117 -0
- package/dist/scriptlets/timers.d.ts +5 -0
- package/dist/scriptlets/timers.js +66 -0
- package/dist/scriptlets/types.d.ts +32 -0
- package/dist/scriptlets/types.js +1 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +16 -0
- package/package.json +83 -4
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
// uBO-flavored JSONPath: the part of RFC 9535 its wiki lists as supported,
|
|
2
|
+
// plus regex selectors, the string operators, assignments and chaining.
|
|
3
|
+
import { isObject } from "./util.js";
|
|
4
|
+
/** An edit's result for a root the query removed (a JSONL line drops). */
|
|
5
|
+
export const REMOVED = Symbol("removed");
|
|
6
|
+
const NOTHING = Symbol("nothing");
|
|
7
|
+
const { apply } = Reflect;
|
|
8
|
+
const { keys, getPrototypeOf } = Object;
|
|
9
|
+
const hasOwn = Object.hasOwn;
|
|
10
|
+
const { parse } = JSON;
|
|
11
|
+
class QueryError extends Error {
|
|
12
|
+
}
|
|
13
|
+
function fail() {
|
|
14
|
+
throw new QueryError();
|
|
15
|
+
}
|
|
16
|
+
function children(node) {
|
|
17
|
+
const { value } = node;
|
|
18
|
+
if (!isObject(value))
|
|
19
|
+
return [];
|
|
20
|
+
const names = Array.isArray(value)
|
|
21
|
+
? value.map((_, index) => index)
|
|
22
|
+
: keys(value);
|
|
23
|
+
return names.map((key) => ({ value: value[key], owner: value, key }));
|
|
24
|
+
}
|
|
25
|
+
function descendants(node, seen, out) {
|
|
26
|
+
out.push(node);
|
|
27
|
+
if (isObject(node.value) && !seen.has(node.value)) {
|
|
28
|
+
seen.add(node.value);
|
|
29
|
+
for (const child of children(node))
|
|
30
|
+
descendants(child, seen, out);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** A member a name selects: own, or inherited on what isn't plain data. */
|
|
35
|
+
function has(object, name) {
|
|
36
|
+
if (hasOwn(object, name))
|
|
37
|
+
return true;
|
|
38
|
+
const proto = getPrototypeOf(object);
|
|
39
|
+
return proto !== Object.prototype && proto !== null && name in object;
|
|
40
|
+
}
|
|
41
|
+
function select(path, root, current) {
|
|
42
|
+
let nodes = [{ value: current }];
|
|
43
|
+
for (const { deep, selectors } of path) {
|
|
44
|
+
const next = [];
|
|
45
|
+
for (const node of nodes) {
|
|
46
|
+
const targets = deep ? descendants(node, new Set(), []) : [node];
|
|
47
|
+
for (const target of targets) {
|
|
48
|
+
for (const selector of selectors)
|
|
49
|
+
selector(target, root, next);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
nodes = next;
|
|
53
|
+
}
|
|
54
|
+
return nodes;
|
|
55
|
+
}
|
|
56
|
+
const byName = (name) => ({ value }, _root, out) => {
|
|
57
|
+
if (isObject(value) && !Array.isArray(value) && has(value, name)) {
|
|
58
|
+
out.push({ value: value[name], owner: value, key: name });
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const byRegex = (regex) => ({ value }, _root, out) => {
|
|
62
|
+
if (!isObject(value) || Array.isArray(value))
|
|
63
|
+
return;
|
|
64
|
+
for (const key of keys(value)) {
|
|
65
|
+
regex.lastIndex = 0;
|
|
66
|
+
if (regex.test(key))
|
|
67
|
+
out.push({ value: value[key], owner: value, key });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const byIndex = (index) => ({ value }, _root, out) => {
|
|
71
|
+
if (!Array.isArray(value))
|
|
72
|
+
return;
|
|
73
|
+
const at = index < 0 ? value.length + index : index;
|
|
74
|
+
if (at >= 0 && at < value.length) {
|
|
75
|
+
out.push({ value: value[at], owner: value, key: at });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const wildcard = (node, _root, out) => {
|
|
79
|
+
out.push(...children(node));
|
|
80
|
+
};
|
|
81
|
+
function equal(a, b) {
|
|
82
|
+
if (a === b)
|
|
83
|
+
return true;
|
|
84
|
+
if (!isObject(a) || !isObject(b) || Array.isArray(a) !== Array.isArray(b)) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const names = keys(a);
|
|
88
|
+
return (names.length === keys(b).length &&
|
|
89
|
+
names.every((name) => hasOwn(b, name) && equal(a[name], b[name])));
|
|
90
|
+
}
|
|
91
|
+
function less(a, b) {
|
|
92
|
+
return (((typeof a === "number" && typeof b === "number") ||
|
|
93
|
+
(typeof a === "string" && typeof b === "string")) &&
|
|
94
|
+
a < b);
|
|
95
|
+
}
|
|
96
|
+
const COMPARISONS = {
|
|
97
|
+
"==": equal,
|
|
98
|
+
"!=": (a, b) => !equal(a, b),
|
|
99
|
+
"<=": (a, b) => less(a, b) || equal(a, b),
|
|
100
|
+
">=": (a, b) => less(b, a) || equal(a, b),
|
|
101
|
+
"^=": (a, b) => typeof a === "string" && typeof b === "string" && a.startsWith(b),
|
|
102
|
+
"$=": (a, b) => typeof a === "string" && typeof b === "string" && a.endsWith(b),
|
|
103
|
+
"*=": (a, b) => typeof a === "string" && typeof b === "string" && a.includes(b),
|
|
104
|
+
"<": less,
|
|
105
|
+
">": (a, b) => less(b, a),
|
|
106
|
+
};
|
|
107
|
+
const NAME = /[A-Za-z_\u0080-][\w\u0080-]*/y;
|
|
108
|
+
const NUMBER = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?/y;
|
|
109
|
+
const INTEGER = /-?(?:0|[1-9]\d*)/y;
|
|
110
|
+
const QUANTIFIER = /(\d*)(,?)(\d*)\}/y;
|
|
111
|
+
const ESCAPES = {
|
|
112
|
+
b: "\b",
|
|
113
|
+
f: "\f",
|
|
114
|
+
n: "\n",
|
|
115
|
+
r: "\r",
|
|
116
|
+
t: "\t",
|
|
117
|
+
"/": "/",
|
|
118
|
+
"\\": "\\",
|
|
119
|
+
"'": "'",
|
|
120
|
+
'"': '"',
|
|
121
|
+
};
|
|
122
|
+
/** Parses a query; throws a QueryError on anything it doesn't support. */
|
|
123
|
+
function parseQuery(text, trusted) {
|
|
124
|
+
let pos = 0;
|
|
125
|
+
const eat = (token) => {
|
|
126
|
+
if (!text.startsWith(token, pos))
|
|
127
|
+
return false;
|
|
128
|
+
pos += token.length;
|
|
129
|
+
return true;
|
|
130
|
+
};
|
|
131
|
+
const skip = () => {
|
|
132
|
+
while (/[ \t\n\r]/.test(text[pos] ?? ""))
|
|
133
|
+
pos++;
|
|
134
|
+
};
|
|
135
|
+
const sticky = (regex) => {
|
|
136
|
+
regex.lastIndex = pos;
|
|
137
|
+
const match = regex.exec(text);
|
|
138
|
+
if (match === null)
|
|
139
|
+
fail();
|
|
140
|
+
pos = regex.lastIndex;
|
|
141
|
+
return match;
|
|
142
|
+
};
|
|
143
|
+
const string = () => {
|
|
144
|
+
const quote = text[pos++];
|
|
145
|
+
let out = "";
|
|
146
|
+
while (pos < text.length) {
|
|
147
|
+
const char = text[pos++];
|
|
148
|
+
if (char === quote)
|
|
149
|
+
return out;
|
|
150
|
+
if (char !== "\\") {
|
|
151
|
+
out += char;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const escape = text[pos++] ?? "";
|
|
155
|
+
if (escape === "u") {
|
|
156
|
+
const hex = text.slice(pos, pos + 4);
|
|
157
|
+
if (!/^[\da-fA-F]{4}$/.test(hex))
|
|
158
|
+
fail();
|
|
159
|
+
out += String.fromCharCode(parseInt(hex, 16));
|
|
160
|
+
pos += 4;
|
|
161
|
+
}
|
|
162
|
+
else
|
|
163
|
+
out += ESCAPES[escape] ?? fail();
|
|
164
|
+
}
|
|
165
|
+
return fail();
|
|
166
|
+
};
|
|
167
|
+
const regex = () => {
|
|
168
|
+
let end = pos + 1;
|
|
169
|
+
let inClass = false;
|
|
170
|
+
for (; end < text.length; end++) {
|
|
171
|
+
const char = text[end];
|
|
172
|
+
if (char === "\\")
|
|
173
|
+
end++;
|
|
174
|
+
else if (char === "[")
|
|
175
|
+
inClass = true;
|
|
176
|
+
else if (char === "]")
|
|
177
|
+
inClass = false;
|
|
178
|
+
else if (char === "/" && !inClass)
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
if (end >= text.length)
|
|
182
|
+
fail();
|
|
183
|
+
const source = text.slice(pos + 1, end);
|
|
184
|
+
pos = end + 1;
|
|
185
|
+
const flags = sticky(/[dgimsuvy]*/y)[0];
|
|
186
|
+
try {
|
|
187
|
+
return new RegExp(source, flags);
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return fail();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const operand = () => {
|
|
194
|
+
const char = text[pos];
|
|
195
|
+
if (char === "@" || char === "$") {
|
|
196
|
+
pos++;
|
|
197
|
+
const query = path();
|
|
198
|
+
const nodes = (current, root) => select(query, root, char === "@" ? current : root);
|
|
199
|
+
return [
|
|
200
|
+
(current, root) => {
|
|
201
|
+
const found = nodes(current, root);
|
|
202
|
+
return found.length === 1 ? found[0].value : NOTHING;
|
|
203
|
+
},
|
|
204
|
+
(current, root) => nodes(current, root).length > 0,
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
let literal;
|
|
208
|
+
if (char === '"' || char === "'")
|
|
209
|
+
literal = string();
|
|
210
|
+
else if (eat("true"))
|
|
211
|
+
literal = true;
|
|
212
|
+
else if (eat("false"))
|
|
213
|
+
literal = false;
|
|
214
|
+
else if (eat("null"))
|
|
215
|
+
literal = null;
|
|
216
|
+
else
|
|
217
|
+
literal = Number(sticky(NUMBER)[0]);
|
|
218
|
+
return [() => literal];
|
|
219
|
+
};
|
|
220
|
+
const filter = () => {
|
|
221
|
+
skip();
|
|
222
|
+
const negated = eat("!");
|
|
223
|
+
skip();
|
|
224
|
+
const [left, exists] = operand();
|
|
225
|
+
skip();
|
|
226
|
+
const op = Object.keys(COMPARISONS).find((one) => text.startsWith(one, pos));
|
|
227
|
+
let test;
|
|
228
|
+
if (op !== undefined && !negated) {
|
|
229
|
+
pos += op.length;
|
|
230
|
+
skip();
|
|
231
|
+
const [right] = operand();
|
|
232
|
+
const compare = COMPARISONS[op];
|
|
233
|
+
test = (current, root) => compare(left(current, root), right(current, root));
|
|
234
|
+
}
|
|
235
|
+
else if (text[pos] === "=" && text[pos + 1] === "/" && !negated) {
|
|
236
|
+
pos++;
|
|
237
|
+
const pattern = regex();
|
|
238
|
+
test = (current, root) => {
|
|
239
|
+
const value = left(current, root);
|
|
240
|
+
pattern.lastIndex = 0;
|
|
241
|
+
return typeof value === "string" && pattern.test(value);
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
else if (op === undefined && exists !== undefined) {
|
|
245
|
+
test = (current, root) => exists(current, root) !== negated;
|
|
246
|
+
}
|
|
247
|
+
else
|
|
248
|
+
return fail();
|
|
249
|
+
return (node, root, out) => {
|
|
250
|
+
for (const child of children(node)) {
|
|
251
|
+
if (test(child.value, root))
|
|
252
|
+
out.push(child);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
const selector = () => {
|
|
257
|
+
const char = text[pos];
|
|
258
|
+
if (char === '"' || char === "'")
|
|
259
|
+
return byName(string());
|
|
260
|
+
if (char === "*") {
|
|
261
|
+
pos++;
|
|
262
|
+
return wildcard;
|
|
263
|
+
}
|
|
264
|
+
if (char === "?") {
|
|
265
|
+
pos++;
|
|
266
|
+
return filter();
|
|
267
|
+
}
|
|
268
|
+
if (char === "/")
|
|
269
|
+
return byRegex(regex());
|
|
270
|
+
return byIndex(Number(sticky(INTEGER)[0]));
|
|
271
|
+
};
|
|
272
|
+
const bracket = () => {
|
|
273
|
+
pos++;
|
|
274
|
+
const selectors = [];
|
|
275
|
+
do {
|
|
276
|
+
skip();
|
|
277
|
+
selectors.push(selector());
|
|
278
|
+
skip();
|
|
279
|
+
} while (eat(","));
|
|
280
|
+
if (!eat("]"))
|
|
281
|
+
fail();
|
|
282
|
+
return selectors;
|
|
283
|
+
};
|
|
284
|
+
// After `.` or `..`: a name, `*`, a regex, or (uBO's) a bracket.
|
|
285
|
+
const member = () => {
|
|
286
|
+
const char = text[pos];
|
|
287
|
+
if (char === "[")
|
|
288
|
+
return bracket();
|
|
289
|
+
if (char === "*") {
|
|
290
|
+
pos++;
|
|
291
|
+
return [wildcard];
|
|
292
|
+
}
|
|
293
|
+
if (char === "/")
|
|
294
|
+
return [byRegex(regex())];
|
|
295
|
+
return [byName(sticky(NAME)[0])];
|
|
296
|
+
};
|
|
297
|
+
function path() {
|
|
298
|
+
const segments = [];
|
|
299
|
+
for (;;) {
|
|
300
|
+
const before = pos;
|
|
301
|
+
skip();
|
|
302
|
+
if (eat(".."))
|
|
303
|
+
segments.push({ deep: true, selectors: member() });
|
|
304
|
+
else if (eat("."))
|
|
305
|
+
segments.push({ deep: false, selectors: member() });
|
|
306
|
+
else if (text[pos] === "[") {
|
|
307
|
+
segments.push({ deep: false, selectors: bracket() });
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
pos = before;
|
|
311
|
+
return segments;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const assignment = () => {
|
|
316
|
+
skip();
|
|
317
|
+
if (pos === text.length)
|
|
318
|
+
return undefined;
|
|
319
|
+
if (!trusted)
|
|
320
|
+
fail();
|
|
321
|
+
const merge = eat("+=");
|
|
322
|
+
if (!merge && !eat("="))
|
|
323
|
+
fail();
|
|
324
|
+
const rest = text.slice(pos).trim();
|
|
325
|
+
if (merge) {
|
|
326
|
+
if (!isObject(parse(rest)))
|
|
327
|
+
fail();
|
|
328
|
+
return ({ value }) => {
|
|
329
|
+
if (isObject(value))
|
|
330
|
+
Object.assign(value, parse(rest));
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const call = /^(repl|call)\((.*)\)$/s.exec(rest);
|
|
334
|
+
if (call === null) {
|
|
335
|
+
parse(rest);
|
|
336
|
+
return (_node, set) => set(parse(rest));
|
|
337
|
+
}
|
|
338
|
+
const spec = parse(call[2]);
|
|
339
|
+
return call[1] === "repl" ? replacer(spec) : caller(spec);
|
|
340
|
+
};
|
|
341
|
+
const steps = [];
|
|
342
|
+
for (;;) {
|
|
343
|
+
skip();
|
|
344
|
+
if (!eat("$"))
|
|
345
|
+
fail();
|
|
346
|
+
const query = path();
|
|
347
|
+
skip();
|
|
348
|
+
if (eat("{")) {
|
|
349
|
+
const [, min, comma, max] = sticky(QUANTIFIER);
|
|
350
|
+
if (min === "" && max === "")
|
|
351
|
+
fail();
|
|
352
|
+
const low = Number(min || 0);
|
|
353
|
+
const high = max !== "" ? Number(max) : comma ? Infinity : low;
|
|
354
|
+
skip();
|
|
355
|
+
// A quantifier never ends the chain.
|
|
356
|
+
if (!eat(";"))
|
|
357
|
+
fail();
|
|
358
|
+
steps.push({ path: query, min: low, max: high });
|
|
359
|
+
}
|
|
360
|
+
else if (eat(";")) {
|
|
361
|
+
steps.push({ path: query, min: 1, max: Infinity });
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
return { steps, last: query, assign: assignment() };
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
function replacer(spec) {
|
|
369
|
+
if (!isObject(spec) || typeof spec.replacement !== "string")
|
|
370
|
+
fail();
|
|
371
|
+
const replacement = spec.replacement;
|
|
372
|
+
let find;
|
|
373
|
+
if (typeof spec.pattern === "string") {
|
|
374
|
+
const pattern = spec.pattern;
|
|
375
|
+
find = (text) => text.split(pattern).join(replacement);
|
|
376
|
+
}
|
|
377
|
+
else if (typeof spec.regex === "string") {
|
|
378
|
+
const regex = new RegExp(spec.regex, "g");
|
|
379
|
+
find = (text) => text.replace(regex, replacement);
|
|
380
|
+
}
|
|
381
|
+
else
|
|
382
|
+
fail();
|
|
383
|
+
return ({ value }, set) => {
|
|
384
|
+
if (typeof value === "string")
|
|
385
|
+
set(find(value));
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
const OBJ = "${obj}";
|
|
389
|
+
const KEY = "${key}";
|
|
390
|
+
const VAL = "${val}";
|
|
391
|
+
function caller(spec) {
|
|
392
|
+
if (!Array.isArray(spec))
|
|
393
|
+
fail();
|
|
394
|
+
const [context, name, ...rest] = spec;
|
|
395
|
+
if (typeof name !== "string" || (context !== null && context !== OBJ)) {
|
|
396
|
+
fail();
|
|
397
|
+
}
|
|
398
|
+
return (node) => {
|
|
399
|
+
const self = context === null ? window : node.owner;
|
|
400
|
+
if (!isObject(self) && typeof self !== "function")
|
|
401
|
+
return;
|
|
402
|
+
const method = self[name];
|
|
403
|
+
if (typeof method !== "function")
|
|
404
|
+
return;
|
|
405
|
+
const args = rest.map((arg) => arg === OBJ
|
|
406
|
+
? node.owner
|
|
407
|
+
: arg === KEY
|
|
408
|
+
? node.key
|
|
409
|
+
: arg === VAL
|
|
410
|
+
? node.value
|
|
411
|
+
: arg);
|
|
412
|
+
try {
|
|
413
|
+
apply(method, self, args);
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
// A call the page's object refuses changes nothing.
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function remove(nodes) {
|
|
421
|
+
const byOwner = new Map();
|
|
422
|
+
for (const { owner, key } of nodes) {
|
|
423
|
+
if (owner === undefined || key === undefined)
|
|
424
|
+
continue;
|
|
425
|
+
byOwner.set(owner, (byOwner.get(owner) ?? new Set()).add(key));
|
|
426
|
+
}
|
|
427
|
+
for (const [owner, names] of byOwner) {
|
|
428
|
+
if (Array.isArray(owner)) {
|
|
429
|
+
// From the end, so the indices left to remove stay right.
|
|
430
|
+
for (const index of [...names].map(Number).sort((a, b) => b - a)) {
|
|
431
|
+
owner.splice(index, 1);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else
|
|
435
|
+
for (const name of names)
|
|
436
|
+
delete owner[name];
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Compiles a query into an edit of a value, in place: it gives the new root
|
|
441
|
+
* (`REMOVED` when the query selected the root itself), or undefined when
|
|
442
|
+
* the query selected nothing. Assignments need `trusted`. An invalid or
|
|
443
|
+
* unsupported query gives undefined.
|
|
444
|
+
*/
|
|
445
|
+
export function compileJsonPath(query, trusted) {
|
|
446
|
+
let parsed;
|
|
447
|
+
try {
|
|
448
|
+
parsed = parseQuery(query.startsWith("v2:") ? query.slice(3) : query, trusted);
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
const { steps, last, assign } = parsed;
|
|
454
|
+
return (root) => {
|
|
455
|
+
for (const { path, min, max } of steps) {
|
|
456
|
+
const count = select(path, root, root).length;
|
|
457
|
+
if (count < min || count > max)
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
const nodes = select(last, root, root);
|
|
461
|
+
if (nodes.length === 0)
|
|
462
|
+
return undefined;
|
|
463
|
+
let result = root;
|
|
464
|
+
if (assign === undefined) {
|
|
465
|
+
if (nodes.some((node) => node.owner === undefined)) {
|
|
466
|
+
return { root: REMOVED };
|
|
467
|
+
}
|
|
468
|
+
remove(nodes);
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
for (const node of nodes) {
|
|
472
|
+
assign(node, (value) => {
|
|
473
|
+
if (node.owner === undefined)
|
|
474
|
+
result = value;
|
|
475
|
+
else
|
|
476
|
+
node.owner[node.key] = value;
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return { root: result };
|
|
481
|
+
};
|
|
482
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { matcher, parseRegex } from "../helpers.js";
|
|
2
|
+
import { editFetchResponses } from "./fetch.js";
|
|
3
|
+
import { editXhrResponses } from "./xhr.js";
|
|
4
|
+
/** Applies a text edit to the fetch and xhr responses from URLs that match. */
|
|
5
|
+
function onResponses(url, edit) {
|
|
6
|
+
const urlMatches = matcher(url);
|
|
7
|
+
const matches = (details) => urlMatches(String(details.url));
|
|
8
|
+
editFetchResponses(matches, edit);
|
|
9
|
+
editXhrResponses(matches, edit);
|
|
10
|
+
}
|
|
11
|
+
/** The nodes a CSS selector or an `xpath(…)` finds in a document. */
|
|
12
|
+
function finder(query) {
|
|
13
|
+
const xpath = /^xpath\((.+)\)$/s.exec(query);
|
|
14
|
+
if (xpath === null)
|
|
15
|
+
return (document) => [...document.querySelectorAll(query)];
|
|
16
|
+
return (document) => {
|
|
17
|
+
const found = document.evaluate(xpath[1], document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
|
18
|
+
const nodes = [];
|
|
19
|
+
for (let i = 0; i < found.snapshotLength; i++)
|
|
20
|
+
nodes.push(found.snapshotItem(i));
|
|
21
|
+
return nodes;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export const xmlPrune = {
|
|
25
|
+
name: "xml-prune",
|
|
26
|
+
run(selector = "", needle = "", url = "") {
|
|
27
|
+
if (selector === "")
|
|
28
|
+
return;
|
|
29
|
+
const find = finder(selector);
|
|
30
|
+
const required = needle === "" ? undefined : finder(needle);
|
|
31
|
+
const Parser = DOMParser;
|
|
32
|
+
const Serializer = XMLSerializer;
|
|
33
|
+
onResponses(url, (text) => {
|
|
34
|
+
if (!text.trimStart().startsWith("<"))
|
|
35
|
+
return undefined;
|
|
36
|
+
const document = new Parser().parseFromString(text, "text/xml");
|
|
37
|
+
if (document.getElementsByTagName("parsererror").length > 0)
|
|
38
|
+
return undefined;
|
|
39
|
+
if (required !== undefined && required(document).length === 0)
|
|
40
|
+
return undefined;
|
|
41
|
+
const nodes = find(document);
|
|
42
|
+
if (nodes.length === 0)
|
|
43
|
+
return undefined;
|
|
44
|
+
for (const node of nodes) {
|
|
45
|
+
if (node instanceof Attr)
|
|
46
|
+
node.ownerElement?.removeAttributeNode(node);
|
|
47
|
+
else
|
|
48
|
+
node.parentNode?.removeChild(node);
|
|
49
|
+
}
|
|
50
|
+
return new Serializer().serializeToString(document);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
/** Removes the lines that match, a segment's #EXTINF line with its URL. */
|
|
55
|
+
function pruneLines(text, matches) {
|
|
56
|
+
const lines = text.split("\n");
|
|
57
|
+
const kept = [];
|
|
58
|
+
for (let i = 0; i < lines.length; i++) {
|
|
59
|
+
const line = lines[i];
|
|
60
|
+
if (!matches(line))
|
|
61
|
+
kept.push(line);
|
|
62
|
+
else if (line.startsWith("#EXTINF"))
|
|
63
|
+
i++;
|
|
64
|
+
else if (!line.startsWith("#") && kept.at(-1)?.startsWith("#EXTINF")) {
|
|
65
|
+
kept.pop();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return kept.join("\n");
|
|
69
|
+
}
|
|
70
|
+
/** Removes what a multiline regex matches, where it is whole lines. */
|
|
71
|
+
function pruneMatches(text, regex) {
|
|
72
|
+
const global = new RegExp(regex.source, regex.flags.replace("g", "") + "g");
|
|
73
|
+
return text.replace(global, (match, ...rest) => {
|
|
74
|
+
const start = rest.find((one) => typeof one === "number");
|
|
75
|
+
const end = start + match.length;
|
|
76
|
+
const whole = (start === 0 || text[start - 1] === "\n") &&
|
|
77
|
+
(end === text.length || text[end] === "\n" || match.endsWith("\n"));
|
|
78
|
+
return whole ? "" : match;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export const m3uPrune = {
|
|
82
|
+
name: "m3u-prune",
|
|
83
|
+
run(needle = "", url = "") {
|
|
84
|
+
if (needle === "")
|
|
85
|
+
return;
|
|
86
|
+
const regex = parseRegex(needle);
|
|
87
|
+
const matches = matcher(needle);
|
|
88
|
+
onResponses(url, (text) => {
|
|
89
|
+
if (!text.trimStart().startsWith("#EXTM3U"))
|
|
90
|
+
return undefined;
|
|
91
|
+
const next = regex?.multiline === true
|
|
92
|
+
? pruneMatches(text, regex)
|
|
93
|
+
: pruneLines(text, matches);
|
|
94
|
+
return next === text ? undefined : next;
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Scriptlet } from "../types.js";
|
|
2
|
+
export declare const trustedOverrideElementMethod: Scriptlet;
|
|
3
|
+
export declare const trustedSuppressNativeMethod: Scriptlet;
|
|
4
|
+
export declare const callNothrow: Scriptlet;
|
|
5
|
+
export declare const trustedPreventDomBypass: Scriptlet;
|