@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,139 @@
|
|
|
1
|
+
import { abort, matcher, parseRegex, stackMatches } from "../helpers.js";
|
|
2
|
+
import { safeConstant } from "../setConstant.js";
|
|
3
|
+
import { isObject, onMethod } from "./util.js";
|
|
4
|
+
const { parse } = JSON;
|
|
5
|
+
export const trustedOverrideElementMethod = {
|
|
6
|
+
name: "trusted-override-element-method",
|
|
7
|
+
trusted: true,
|
|
8
|
+
run(chain = "", selector = "", disposition = "") {
|
|
9
|
+
// `debug` only stops in a debugger, and there is no logger to open.
|
|
10
|
+
if (chain === "" || disposition === "debug")
|
|
11
|
+
return;
|
|
12
|
+
// No disposition: the call does nothing, like an empty function.
|
|
13
|
+
const value = disposition === "" || disposition === "throw"
|
|
14
|
+
? undefined
|
|
15
|
+
: safeConstant(disposition);
|
|
16
|
+
if (typeof value === "symbol")
|
|
17
|
+
return;
|
|
18
|
+
onMethod(chain, (original, self, args) => {
|
|
19
|
+
let matches = self instanceof Element;
|
|
20
|
+
try {
|
|
21
|
+
matches &&= selector === "" || self.matches(selector);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
matches = false;
|
|
25
|
+
}
|
|
26
|
+
if (!matches)
|
|
27
|
+
return original(...args);
|
|
28
|
+
if (disposition === "throw")
|
|
29
|
+
abort();
|
|
30
|
+
return value;
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
/** One part of a `trusted-suppress-native-method` signature. */
|
|
35
|
+
function argumentMatcher(text) {
|
|
36
|
+
if (text.trim() === "")
|
|
37
|
+
return () => true;
|
|
38
|
+
if (parseRegex(text) !== undefined) {
|
|
39
|
+
const matches = matcher(text);
|
|
40
|
+
return (value) => matches(String(value));
|
|
41
|
+
}
|
|
42
|
+
let expected;
|
|
43
|
+
try {
|
|
44
|
+
expected = text === "undefined" ? undefined : parse(text);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
expected = text;
|
|
48
|
+
}
|
|
49
|
+
return (value) => like(value, expected);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether a value fits what a signature expects: a string found in it (only
|
|
53
|
+
* `""` for the empty string), the same primitive, an object with those
|
|
54
|
+
* properties, or an array holding those values.
|
|
55
|
+
*/
|
|
56
|
+
function like(value, expected) {
|
|
57
|
+
if (typeof expected === "string") {
|
|
58
|
+
return (typeof value === "string" &&
|
|
59
|
+
(expected === "" ? value === "" : value.includes(expected)));
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(expected)) {
|
|
62
|
+
return (Array.isArray(value) &&
|
|
63
|
+
expected.every((one) => value.some((item) => same(item, one))));
|
|
64
|
+
}
|
|
65
|
+
if (isObject(expected)) {
|
|
66
|
+
return (isObject(value) &&
|
|
67
|
+
Object.keys(expected).every((name) => like(value[name], expected[name])));
|
|
68
|
+
}
|
|
69
|
+
return value === expected;
|
|
70
|
+
}
|
|
71
|
+
function same(value, expected) {
|
|
72
|
+
return isObject(expected) ? like(value, expected) : value === expected;
|
|
73
|
+
}
|
|
74
|
+
export const trustedSuppressNativeMethod = {
|
|
75
|
+
name: "trusted-suppress-native-method",
|
|
76
|
+
trusted: true,
|
|
77
|
+
run(chain = "", signature = "", how = "abort", stack = "") {
|
|
78
|
+
// Without a signature the scriptlet only logs, and there is no logger.
|
|
79
|
+
if (chain === "" || signature === "" || how === "debug")
|
|
80
|
+
return;
|
|
81
|
+
const matchers = signature.split("|").map(argumentMatcher);
|
|
82
|
+
onMethod(chain, (original, _self, args) => {
|
|
83
|
+
if (!matchers.every((matches, index) => matches(args[index])) ||
|
|
84
|
+
!stackMatches(stack)) {
|
|
85
|
+
return original(...args);
|
|
86
|
+
}
|
|
87
|
+
if (how === "prevent")
|
|
88
|
+
return undefined;
|
|
89
|
+
return abort();
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
export const callNothrow = {
|
|
94
|
+
name: "call-nothrow",
|
|
95
|
+
run(chain = "") {
|
|
96
|
+
onMethod(chain, (original, _self, args) => {
|
|
97
|
+
try {
|
|
98
|
+
return original(...args);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
export const trustedPreventDomBypass = {
|
|
107
|
+
name: "trusted-prevent-dom-bypass",
|
|
108
|
+
trusted: true,
|
|
109
|
+
run(chain = "", property = "") {
|
|
110
|
+
// An anonymous frame added by the page gets this window's (patched)
|
|
111
|
+
// method, or becomes this window when no method is named.
|
|
112
|
+
const own = window;
|
|
113
|
+
const patch = (frame) => {
|
|
114
|
+
try {
|
|
115
|
+
if (property === "") {
|
|
116
|
+
Object.defineProperty(frame, "contentWindow", { get: () => window });
|
|
117
|
+
}
|
|
118
|
+
else if (frame.contentWindow !== null) {
|
|
119
|
+
frame.contentWindow[property] = own[property];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// A frame of another origin keeps its own.
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
onMethod(chain, (original, _self, args) => {
|
|
127
|
+
const result = original(...args);
|
|
128
|
+
for (const arg of args) {
|
|
129
|
+
if (!(arg instanceof HTMLElement))
|
|
130
|
+
continue;
|
|
131
|
+
if (arg instanceof HTMLIFrameElement)
|
|
132
|
+
patch(arg);
|
|
133
|
+
for (const frame of arg.querySelectorAll("iframe"))
|
|
134
|
+
patch(frame);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Scriptlet } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The `json-prune` edit: removes the properties `prune` lists from an
|
|
4
|
+
* object, when every property `needle` lists is there (or, when `needle`
|
|
5
|
+
* is a regex, when the object's JSON matches it). Says whether it did.
|
|
6
|
+
*/
|
|
7
|
+
export declare function pruner(prune: string, needle?: string): (data: unknown) => boolean;
|
|
8
|
+
export declare const jsonPrune: Scriptlet;
|
|
9
|
+
export declare const jsonPruneFetchResponse: Scriptlet;
|
|
10
|
+
export declare const jsonPruneXhrResponse: Scriptlet;
|
|
11
|
+
export declare const evaldataPrune: Scriptlet;
|
|
12
|
+
export declare const trustedPruneInboundObject: Scriptlet;
|
|
13
|
+
export declare const trustedPruneOutboundObject: Scriptlet;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { parseRegex, stackMatches, tokens, wrapMethod } from "../helpers.js";
|
|
2
|
+
import { jsonTextEdit } from "./edit.js";
|
|
3
|
+
import { editFetchResponses } from "./fetch.js";
|
|
4
|
+
import { requestMatcher } from "./request.js";
|
|
5
|
+
import { isObject, onMethod } from "./util.js";
|
|
6
|
+
import { editXhrResponses } from "./xhr.js";
|
|
7
|
+
const { keys } = Object;
|
|
8
|
+
const { parse, stringify } = JSON;
|
|
9
|
+
const hasOwn = Object.hasOwn;
|
|
10
|
+
const then = Promise.prototype.then;
|
|
11
|
+
const { apply } = Reflect;
|
|
12
|
+
/** The keys of `node` a path segment names. */
|
|
13
|
+
function selected(node, key) {
|
|
14
|
+
if (key === "[]" || key === "[-]")
|
|
15
|
+
return Array.isArray(node) ? keys(node) : [];
|
|
16
|
+
if (key === "*" || key === "{-}")
|
|
17
|
+
return keys(node);
|
|
18
|
+
return hasOwn(node, key) ? [key] : [];
|
|
19
|
+
}
|
|
20
|
+
/** Whether a path, from segment `from`, leads to a property. */
|
|
21
|
+
function exists(node, path, from = 0) {
|
|
22
|
+
if (from === path.length)
|
|
23
|
+
return true;
|
|
24
|
+
if (!isObject(node))
|
|
25
|
+
return false;
|
|
26
|
+
return selected(node, path[from]).some((key) => exists(node[key], path, from + 1));
|
|
27
|
+
}
|
|
28
|
+
function remove(owner, names) {
|
|
29
|
+
if (names.length === 0)
|
|
30
|
+
return false;
|
|
31
|
+
if (Array.isArray(owner)) {
|
|
32
|
+
for (const index of names.map(Number).sort((a, b) => b - a)) {
|
|
33
|
+
owner.splice(index, 1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
for (const name of names)
|
|
38
|
+
delete owner[name];
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
function prunePath(node, path, from) {
|
|
42
|
+
if (!isObject(node))
|
|
43
|
+
return false;
|
|
44
|
+
const key = path[from];
|
|
45
|
+
const names = selected(node, key);
|
|
46
|
+
// `[-]` and `{-}` remove the entries the rest of the path leads into.
|
|
47
|
+
if (key === "[-]" || key === "{-}") {
|
|
48
|
+
return remove(node, names.filter((name) => exists(node[name], path, from + 1)));
|
|
49
|
+
}
|
|
50
|
+
if (from === path.length - 1)
|
|
51
|
+
return remove(node, names);
|
|
52
|
+
let pruned = false;
|
|
53
|
+
for (const name of names) {
|
|
54
|
+
if (prunePath(node[name], path, from + 1))
|
|
55
|
+
pruned = true;
|
|
56
|
+
}
|
|
57
|
+
return pruned;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The `json-prune` edit: removes the properties `prune` lists from an
|
|
61
|
+
* object, when every property `needle` lists is there (or, when `needle`
|
|
62
|
+
* is a regex, when the object's JSON matches it). Says whether it did.
|
|
63
|
+
*/
|
|
64
|
+
export function pruner(prune, needle = "") {
|
|
65
|
+
const toPath = (chain) => chain.split(".");
|
|
66
|
+
const paths = prune.split(/\s+/).filter(Boolean).map(toPath);
|
|
67
|
+
const regex = parseRegex(needle);
|
|
68
|
+
const needles = regex === undefined ? needle.split(/\s+/).filter(Boolean).map(toPath) : [];
|
|
69
|
+
return (data) => {
|
|
70
|
+
if (!isObject(data))
|
|
71
|
+
return false;
|
|
72
|
+
if (!needles.every((path) => exists(data, path)))
|
|
73
|
+
return false;
|
|
74
|
+
if (regex !== undefined) {
|
|
75
|
+
regex.lastIndex = 0;
|
|
76
|
+
if (!regex.test(stringify(data)))
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
let pruned = false;
|
|
80
|
+
for (const path of paths)
|
|
81
|
+
if (prunePath(data, path, 0))
|
|
82
|
+
pruned = true;
|
|
83
|
+
return pruned;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export const jsonPrune = {
|
|
87
|
+
name: "json-prune",
|
|
88
|
+
run(prune = "", needle = "", stack = "") {
|
|
89
|
+
// Without paths the scriptlet only logs, and there is no logger.
|
|
90
|
+
if (prune === "")
|
|
91
|
+
return;
|
|
92
|
+
const edit = pruner(prune, needle);
|
|
93
|
+
wrapMethod(JSON, "parse", (original, _self, args) => {
|
|
94
|
+
const data = original(...args);
|
|
95
|
+
if (stackMatches(stack))
|
|
96
|
+
edit(data);
|
|
97
|
+
return data;
|
|
98
|
+
});
|
|
99
|
+
if (typeof Response !== "function")
|
|
100
|
+
return;
|
|
101
|
+
wrapMethod(Response.prototype, "json", (original, _self, args) => {
|
|
102
|
+
// The stack is the caller's only until the promise settles.
|
|
103
|
+
if (!stackMatches(stack))
|
|
104
|
+
return original(...args);
|
|
105
|
+
return apply(then, original(...args), [
|
|
106
|
+
(data) => {
|
|
107
|
+
edit(data);
|
|
108
|
+
return data;
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
function pruneResponses(args, install) {
|
|
115
|
+
const [prune = "", needle = ""] = args;
|
|
116
|
+
if (prune === "")
|
|
117
|
+
return;
|
|
118
|
+
const options = tokens(args, 2);
|
|
119
|
+
const edit = pruner(prune, needle);
|
|
120
|
+
const props = requestMatcher(options.propsToMatch);
|
|
121
|
+
install((details) => props(details) && stackMatches(options.stackToMatch), jsonTextEdit((data) => (edit(data) ? { root: data } : undefined)));
|
|
122
|
+
}
|
|
123
|
+
export const jsonPruneFetchResponse = {
|
|
124
|
+
name: "json-prune-fetch-response",
|
|
125
|
+
run: (...args) => pruneResponses(args, editFetchResponses),
|
|
126
|
+
};
|
|
127
|
+
export const jsonPruneXhrResponse = {
|
|
128
|
+
name: "json-prune-xhr-response",
|
|
129
|
+
run: (...args) => pruneResponses(args, editXhrResponses),
|
|
130
|
+
};
|
|
131
|
+
export const evaldataPrune = {
|
|
132
|
+
name: "evaldata-prune",
|
|
133
|
+
run(prune = "", needle = "") {
|
|
134
|
+
if (prune === "")
|
|
135
|
+
return;
|
|
136
|
+
const edit = pruner(prune, needle);
|
|
137
|
+
wrapMethod(window, "eval", (original, _self, args) => {
|
|
138
|
+
let data;
|
|
139
|
+
try {
|
|
140
|
+
data = parse(String(args[0]));
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return original(...args);
|
|
144
|
+
}
|
|
145
|
+
// JSON the filter prunes is the result, as eval can't run all JSON.
|
|
146
|
+
return edit(data) ? data : original(...args);
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
/** A copy of an object, for `dontOverwrite`: what JSON keeps of it. */
|
|
151
|
+
function copy(value) {
|
|
152
|
+
try {
|
|
153
|
+
return parse(stringify(value));
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export const trustedPruneInboundObject = {
|
|
160
|
+
name: "trusted-prune-inbound-object",
|
|
161
|
+
trusted: true,
|
|
162
|
+
run(...args) {
|
|
163
|
+
const [chain = "", position = "", prune = "", needle = ""] = args;
|
|
164
|
+
const index = Number(position) - 1;
|
|
165
|
+
if (prune === "" || !Number.isInteger(index) || index < 0)
|
|
166
|
+
return;
|
|
167
|
+
const options = tokens(args, 4);
|
|
168
|
+
const edit = pruner(prune, needle);
|
|
169
|
+
onMethod(chain, (original, _self, callArgs) => {
|
|
170
|
+
if (isObject(callArgs[index]) && stackMatches(options.stackToMatch)) {
|
|
171
|
+
const object = options.dontOverwrite === undefined
|
|
172
|
+
? callArgs[index]
|
|
173
|
+
: copy(callArgs[index]);
|
|
174
|
+
if (edit(object))
|
|
175
|
+
callArgs[index] = object;
|
|
176
|
+
}
|
|
177
|
+
return original(...callArgs);
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
export const trustedPruneOutboundObject = {
|
|
182
|
+
name: "trusted-prune-outbound-object",
|
|
183
|
+
trusted: true,
|
|
184
|
+
run(chain = "", prune = "", needle = "") {
|
|
185
|
+
if (prune === "")
|
|
186
|
+
return;
|
|
187
|
+
const edit = pruner(prune, needle);
|
|
188
|
+
onMethod(chain, (original, _self, args) => {
|
|
189
|
+
const result = original(...args);
|
|
190
|
+
edit(result);
|
|
191
|
+
return result;
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { matcher, wrapMethod } from "../helpers.js";
|
|
2
|
+
import { wrapSetter } from "./util.js";
|
|
3
|
+
export const preventRequestAnimationFrame = {
|
|
4
|
+
name: "prevent-requestAnimationFrame",
|
|
5
|
+
aliases: ["no-requestAnimationFrame-if", "norafif"],
|
|
6
|
+
run(needle = "") {
|
|
7
|
+
// Without a needle the scriptlet only logs, and there is no logger.
|
|
8
|
+
if (needle === "")
|
|
9
|
+
return;
|
|
10
|
+
const matches = matcher(needle, true);
|
|
11
|
+
wrapMethod(window, "requestAnimationFrame", (original, _self, args) => {
|
|
12
|
+
// The page still gets a frame id it can cancel.
|
|
13
|
+
if (matches(String(args[0])))
|
|
14
|
+
args[0] = function () { };
|
|
15
|
+
return original(...args);
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
export const preventCanvas = {
|
|
20
|
+
name: "prevent-canvas",
|
|
21
|
+
run(type = "") {
|
|
22
|
+
// Unlike most scriptlets, no type means every kind of canvas.
|
|
23
|
+
const matches = matcher(type, true);
|
|
24
|
+
wrapMethod(HTMLCanvasElement.prototype, "getContext", (original, _self, args) => matches(String(args[0])) ? null : original(...args));
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
export const preventInnerHtml = {
|
|
28
|
+
name: "prevent-innerHTML",
|
|
29
|
+
run(selector = "", pattern = "") {
|
|
30
|
+
// Without arguments the scriptlet only logs, and there is no logger.
|
|
31
|
+
if (selector === "" && pattern === "")
|
|
32
|
+
return;
|
|
33
|
+
const matches = matcher(pattern, true);
|
|
34
|
+
wrapSetter(Element.prototype, "innerHTML", (write, self, value) => {
|
|
35
|
+
let target = true;
|
|
36
|
+
try {
|
|
37
|
+
target = selector === "" || self.matches(selector);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
target = false;
|
|
41
|
+
}
|
|
42
|
+
if (!target || !matches(String(value)))
|
|
43
|
+
write(value);
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Scriptlet } from "../types.js";
|
|
2
|
+
export declare const trustedReplaceFetchResponse: Scriptlet;
|
|
3
|
+
export declare const trustedReplaceXhrResponse: Scriptlet;
|
|
4
|
+
export declare const trustedReplaceOutboundText: Scriptlet;
|
|
5
|
+
export declare const trustedReplaceArgument: Scriptlet;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { lookUp, matcher, parseRegex, tokens } from "../helpers.js";
|
|
2
|
+
import { safeConstant } from "../setConstant.js";
|
|
3
|
+
import { editFetchResponses } from "./fetch.js";
|
|
4
|
+
import { requestMatcher } from "./request.js";
|
|
5
|
+
import { onMethod, position } from "./util.js";
|
|
6
|
+
import { editXhrResponses } from "./xhr.js";
|
|
7
|
+
const { apply, construct } = Reflect;
|
|
8
|
+
const { parse } = JSON;
|
|
9
|
+
/**
|
|
10
|
+
* Replaces `pattern` in a text: `*` is the whole text, `/…/` a regex (its
|
|
11
|
+
* flags say how often), anything else every occurrence of that text.
|
|
12
|
+
*/
|
|
13
|
+
function replacer(pattern, replacement) {
|
|
14
|
+
if (pattern === "*")
|
|
15
|
+
return () => replacement;
|
|
16
|
+
const regex = parseRegex(pattern);
|
|
17
|
+
if (regex === undefined) {
|
|
18
|
+
return (text) => text.split(pattern).join(replacement);
|
|
19
|
+
}
|
|
20
|
+
return (text) => {
|
|
21
|
+
regex.lastIndex = 0;
|
|
22
|
+
return text.replace(regex, replacement);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function replaceResponses(args, install) {
|
|
26
|
+
const [pattern = "", replacement = "", props = ""] = args;
|
|
27
|
+
if (pattern === "")
|
|
28
|
+
return;
|
|
29
|
+
const includes = matcher(tokens(args, 3).includes);
|
|
30
|
+
const replace = replacer(pattern, replacement);
|
|
31
|
+
install(requestMatcher(props), (text) => {
|
|
32
|
+
if (!includes(text))
|
|
33
|
+
return undefined;
|
|
34
|
+
const next = replace(text);
|
|
35
|
+
return next === text ? undefined : next;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export const trustedReplaceFetchResponse = {
|
|
39
|
+
name: "trusted-replace-fetch-response",
|
|
40
|
+
aliases: ["trusted-rpfr"],
|
|
41
|
+
trusted: true,
|
|
42
|
+
run: (...args) => replaceResponses(args, editFetchResponses),
|
|
43
|
+
};
|
|
44
|
+
export const trustedReplaceXhrResponse = {
|
|
45
|
+
name: "trusted-replace-xhr-response",
|
|
46
|
+
trusted: true,
|
|
47
|
+
run: (...args) => replaceResponses(args, editXhrResponses),
|
|
48
|
+
};
|
|
49
|
+
export const trustedReplaceOutboundText = {
|
|
50
|
+
name: "trusted-replace-outbound-text",
|
|
51
|
+
aliases: ["trusted-rpot"],
|
|
52
|
+
trusted: true,
|
|
53
|
+
run(...args) {
|
|
54
|
+
const [chain = "", pattern = "", raw = ""] = args;
|
|
55
|
+
// Without a pattern the scriptlet only logs, and there is no logger.
|
|
56
|
+
if (pattern === "")
|
|
57
|
+
return;
|
|
58
|
+
let replacement = raw;
|
|
59
|
+
if (raw.startsWith("json:")) {
|
|
60
|
+
try {
|
|
61
|
+
replacement = String(parse(raw.slice(5)));
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (raw.length > 1 && raw.startsWith("/") && raw.endsWith("/")) {
|
|
68
|
+
// The wiki writes regex replacements between slashes: `/$1$2/`.
|
|
69
|
+
replacement = raw.slice(1, -1);
|
|
70
|
+
}
|
|
71
|
+
const options = tokens(args, 3);
|
|
72
|
+
const condition = matcher(options.condition);
|
|
73
|
+
const base64 = options.encoding === "base64";
|
|
74
|
+
const decode = atob;
|
|
75
|
+
const encode = btoa;
|
|
76
|
+
const replace = replacer(pattern, replacement);
|
|
77
|
+
onMethod(chain, (original, _self, callArgs) => {
|
|
78
|
+
const result = original(...callArgs);
|
|
79
|
+
if (typeof result !== "string")
|
|
80
|
+
return result;
|
|
81
|
+
try {
|
|
82
|
+
const text = base64 ? decode(result) : result;
|
|
83
|
+
if (!condition(text))
|
|
84
|
+
return result;
|
|
85
|
+
const next = replace(text);
|
|
86
|
+
if (next === text)
|
|
87
|
+
return result;
|
|
88
|
+
return base64 ? encode(next) : next;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Not base64 after all, or not text btoa can encode.
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
/** What `repl:/pattern/replacement/flags` does to a value. */
|
|
98
|
+
function regexReplacement(text) {
|
|
99
|
+
const match = /^repl:\/((?:\\.|[^\\/])+)\/((?:\\.|[^\\/])*)\/([dgimsuvy]*)$/s.exec(text);
|
|
100
|
+
if (match === null)
|
|
101
|
+
return undefined;
|
|
102
|
+
let regex;
|
|
103
|
+
try {
|
|
104
|
+
regex = new RegExp(match[1], match[3]);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const replacement = match[2].replace(/\\\//g, "/");
|
|
110
|
+
return (value) => {
|
|
111
|
+
regex.lastIndex = 0;
|
|
112
|
+
return String(value).replace(regex, replacement);
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** The new value of a replaced argument, from the filter's text. */
|
|
116
|
+
function argumentReplacement(raw) {
|
|
117
|
+
if (raw.startsWith("repl:"))
|
|
118
|
+
return regexReplacement(raw);
|
|
119
|
+
if (raw.startsWith("json:")) {
|
|
120
|
+
try {
|
|
121
|
+
parse(raw.slice(5));
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return () => parse(raw.slice(5));
|
|
127
|
+
}
|
|
128
|
+
const value = safeConstant(raw);
|
|
129
|
+
return typeof value === "symbol" ? undefined : () => value;
|
|
130
|
+
}
|
|
131
|
+
export const trustedReplaceArgument = {
|
|
132
|
+
name: "trusted-replace-argument",
|
|
133
|
+
trusted: true,
|
|
134
|
+
run(...args) {
|
|
135
|
+
const [chain = "", at = "", raw = ""] = args;
|
|
136
|
+
// Without a position the scriptlet only logs, and there is no logger.
|
|
137
|
+
if (at === "")
|
|
138
|
+
return;
|
|
139
|
+
const replace = argumentReplacement(raw);
|
|
140
|
+
const target = lookUp(chain);
|
|
141
|
+
if (replace === undefined || target === undefined)
|
|
142
|
+
return;
|
|
143
|
+
const method = target.owner[target.property];
|
|
144
|
+
if (typeof method !== "function")
|
|
145
|
+
return;
|
|
146
|
+
const condition = matcher(tokens(args, 3).condition);
|
|
147
|
+
// `this` can be replaced too, so this traps the calls itself rather
|
|
148
|
+
// than through a wrapper that keeps it.
|
|
149
|
+
const edit = (self, callArgs) => {
|
|
150
|
+
if (at === "this")
|
|
151
|
+
return condition(String(self)) ? replace(self) : self;
|
|
152
|
+
const index = position(at, callArgs.length);
|
|
153
|
+
if (index !== undefined && index >= 0 && index < callArgs.length) {
|
|
154
|
+
if (condition(String(callArgs[index]))) {
|
|
155
|
+
callArgs[index] = replace(callArgs[index]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return self;
|
|
159
|
+
};
|
|
160
|
+
target.owner[target.property] = new Proxy(method, {
|
|
161
|
+
apply: (fn, self, callArgs) => apply(fn, edit(self, callArgs), callArgs),
|
|
162
|
+
construct: (fn, callArgs, newTarget) => {
|
|
163
|
+
edit(undefined, callArgs);
|
|
164
|
+
return construct(fn, callArgs, newTarget);
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** What a fetch or an XMLHttpRequest asks for, by `init` option name. */
|
|
2
|
+
export type Details = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* A space-separated list of conditions a request must all meet: text or
|
|
5
|
+
* `/regex/` to find in its URL (`!` inverts, when `negatable`), or
|
|
6
|
+
* `name:value` for one of its options. Nothing, or `*`, matches any request.
|
|
7
|
+
*/
|
|
8
|
+
export declare function requestMatcher(props?: string, negatable?: boolean): (details: Details) => boolean;
|
|
9
|
+
/** The URL as the request resolves it. */
|
|
10
|
+
export declare function absolute(url: unknown): string;
|
|
11
|
+
/** What a call of `fetch(input, init)` asks for. */
|
|
12
|
+
export declare function fetchDetails(args: readonly unknown[]): Details;
|
|
13
|
+
/**
|
|
14
|
+
* The body a defused request answers with, from a directive. Trusted
|
|
15
|
+
* filters can give any text; for the others an unknown directive is empty.
|
|
16
|
+
* Call it while the scriptlet is set up: a `war:` resource is only there
|
|
17
|
+
* then. The function it returns gives the body of each answer.
|
|
18
|
+
*/
|
|
19
|
+
export declare function responseBody(directive: string, trusted: boolean): () => string;
|