@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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -2
  3. package/THIRD_PARTY_NOTICES.md +50 -0
  4. package/dist/content/cosmetics.d.ts +22 -0
  5. package/dist/content/cosmetics.js +173 -0
  6. package/dist/content/mainWorld.d.ts +8 -0
  7. package/dist/content/mainWorld.js +10 -0
  8. package/dist/content/preload.d.ts +1 -0
  9. package/dist/content/preload.js +34 -0
  10. package/dist/content/procedural.d.ts +19 -0
  11. package/dist/content/procedural.js +212 -0
  12. package/dist/cosmetic/engine.d.ts +95 -0
  13. package/dist/cosmetic/engine.js +268 -0
  14. package/dist/electron/blocker.d.ts +84 -0
  15. package/dist/electron/blocker.js +320 -0
  16. package/dist/electron/channels.d.ts +10 -0
  17. package/dist/electron/channels.js +6 -0
  18. package/dist/electron/scheme.d.ts +21 -0
  19. package/dist/electron/scheme.js +21 -0
  20. package/dist/engine.d.ts +86 -0
  21. package/dist/engine.js +163 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +12 -0
  24. package/dist/lists/defaults.d.ts +6 -0
  25. package/dist/lists/defaults.js +59 -0
  26. package/dist/lists/manager.d.ts +86 -0
  27. package/dist/lists/manager.js +154 -0
  28. package/dist/lists/preprocessor.d.ts +21 -0
  29. package/dist/lists/preprocessor.js +152 -0
  30. package/dist/lists/storage.d.ts +37 -0
  31. package/dist/lists/storage.js +66 -0
  32. package/dist/network/engine.d.ts +105 -0
  33. package/dist/network/engine.js +431 -0
  34. package/dist/network/filterIndex.d.ts +40 -0
  35. package/dist/network/filterIndex.js +343 -0
  36. package/dist/network/modifiers.d.ts +23 -0
  37. package/dist/network/modifiers.js +192 -0
  38. package/dist/network/pattern.d.ts +7 -0
  39. package/dist/network/pattern.js +145 -0
  40. package/dist/network/request.d.ts +50 -0
  41. package/dist/network/request.js +136 -0
  42. package/dist/network/tokens.d.ts +5 -0
  43. package/dist/network/tokens.js +150 -0
  44. package/dist/parser/cosmetic.d.ts +21 -0
  45. package/dist/parser/cosmetic.js +171 -0
  46. package/dist/parser/hosts.d.ts +20 -0
  47. package/dist/parser/hosts.js +143 -0
  48. package/dist/parser/index.d.ts +40 -0
  49. package/dist/parser/index.js +158 -0
  50. package/dist/parser/network.d.ts +6 -0
  51. package/dist/parser/network.js +474 -0
  52. package/dist/parser/selector.d.ts +25 -0
  53. package/dist/parser/selector.js +522 -0
  54. package/dist/parser/types.d.ts +175 -0
  55. package/dist/parser/types.js +75 -0
  56. package/dist/preload.cjs +1988 -0
  57. package/dist/psl/psl.d.ts +32 -0
  58. package/dist/psl/psl.js +119 -0
  59. package/dist/resources/binary.d.ts +9 -0
  60. package/dist/resources/binary.js +54 -0
  61. package/dist/resources/defusers.d.ts +29 -0
  62. package/dist/resources/defusers.js +180 -0
  63. package/dist/resources/documents.d.ts +12 -0
  64. package/dist/resources/documents.js +56 -0
  65. package/dist/resources/ima.d.ts +6 -0
  66. package/dist/resources/ima.js +313 -0
  67. package/dist/resources/index.d.ts +14 -0
  68. package/dist/resources/index.js +85 -0
  69. package/dist/resources/surrogates.d.ts +30 -0
  70. package/dist/resources/surrogates.js +486 -0
  71. package/dist/scriptlets/abort.d.ts +5 -0
  72. package/dist/scriptlets/abort.js +97 -0
  73. package/dist/scriptlets/dom/attributes.d.ts +5 -0
  74. package/dist/scriptlets/dom/attributes.js +92 -0
  75. package/dist/scriptlets/dom/click.d.ts +2 -0
  76. package/dist/scriptlets/dom/click.js +107 -0
  77. package/dist/scriptlets/dom/cookies.d.ts +16 -0
  78. package/dist/scriptlets/dom/cookies.js +194 -0
  79. package/dist/scriptlets/dom/createHtml.d.ts +2 -0
  80. package/dist/scriptlets/dom/createHtml.js +27 -0
  81. package/dist/scriptlets/dom/dialogs.d.ts +3 -0
  82. package/dist/scriptlets/dom/dialogs.js +21 -0
  83. package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
  84. package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
  85. package/dist/scriptlets/dom/index.d.ts +3 -0
  86. package/dist/scriptlets/dom/index.js +38 -0
  87. package/dist/scriptlets/dom/navigation.d.ts +5 -0
  88. package/dist/scriptlets/dom/navigation.js +60 -0
  89. package/dist/scriptlets/dom/nodeText.d.ts +3 -0
  90. package/dist/scriptlets/dom/nodeText.js +98 -0
  91. package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
  92. package/dist/scriptlets/dom/spoofCss.js +58 -0
  93. package/dist/scriptlets/dom/storage.d.ts +5 -0
  94. package/dist/scriptlets/dom/storage.js +50 -0
  95. package/dist/scriptlets/dom/watch.d.ts +8 -0
  96. package/dist/scriptlets/dom/watch.js +41 -0
  97. package/dist/scriptlets/events.d.ts +2 -0
  98. package/dist/scriptlets/events.js +42 -0
  99. package/dist/scriptlets/helpers.d.ts +58 -0
  100. package/dist/scriptlets/helpers.js +206 -0
  101. package/dist/scriptlets/index.d.ts +6 -0
  102. package/dist/scriptlets/index.js +47 -0
  103. package/dist/scriptlets/network/edit.d.ts +9 -0
  104. package/dist/scriptlets/network/edit.js +104 -0
  105. package/dist/scriptlets/network/eval.d.ts +3 -0
  106. package/dist/scriptlets/network/eval.js +22 -0
  107. package/dist/scriptlets/network/fetch.d.ts +10 -0
  108. package/dist/scriptlets/network/fetch.js +145 -0
  109. package/dist/scriptlets/network/index.d.ts +3 -0
  110. package/dist/scriptlets/network/index.js +43 -0
  111. package/dist/scriptlets/network/jsonPath.d.ts +13 -0
  112. package/dist/scriptlets/network/jsonPath.js +482 -0
  113. package/dist/scriptlets/network/media.d.ts +3 -0
  114. package/dist/scriptlets/network/media.js +97 -0
  115. package/dist/scriptlets/network/methods.d.ts +5 -0
  116. package/dist/scriptlets/network/methods.js +139 -0
  117. package/dist/scriptlets/network/prune.d.ts +13 -0
  118. package/dist/scriptlets/network/prune.js +194 -0
  119. package/dist/scriptlets/network/rendering.d.ts +4 -0
  120. package/dist/scriptlets/network/rendering.js +46 -0
  121. package/dist/scriptlets/network/replace.d.ts +5 -0
  122. package/dist/scriptlets/network/replace.js +168 -0
  123. package/dist/scriptlets/network/request.d.ts +19 -0
  124. package/dist/scriptlets/network/request.js +124 -0
  125. package/dist/scriptlets/network/util.d.ts +19 -0
  126. package/dist/scriptlets/network/util.js +80 -0
  127. package/dist/scriptlets/network/webrtc.d.ts +3 -0
  128. package/dist/scriptlets/network/webrtc.js +62 -0
  129. package/dist/scriptlets/network/windowOpen.d.ts +2 -0
  130. package/dist/scriptlets/network/windowOpen.js +28 -0
  131. package/dist/scriptlets/network/xhr.d.ts +9 -0
  132. package/dist/scriptlets/network/xhr.js +177 -0
  133. package/dist/scriptlets/runner.d.ts +8 -0
  134. package/dist/scriptlets/runner.js +19 -0
  135. package/dist/scriptlets/setConstant.d.ts +5 -0
  136. package/dist/scriptlets/setConstant.js +117 -0
  137. package/dist/scriptlets/timers.d.ts +5 -0
  138. package/dist/scriptlets/timers.js +66 -0
  139. package/dist/scriptlets/types.d.ts +32 -0
  140. package/dist/scriptlets/types.js +1 -0
  141. package/dist/util.d.ts +5 -0
  142. package/dist/util.js +16 -0
  143. package/package.json +83 -4
@@ -0,0 +1,104 @@
1
+ import { tokens, wrapMethod } from "../helpers.js";
2
+ import { editFetchRequests, editFetchResponses, } from "./fetch.js";
3
+ import { REMOVED, compileJsonPath } from "./jsonPath.js";
4
+ import { requestMatcher } from "./request.js";
5
+ import { onMethod, position } from "./util.js";
6
+ import { editXhrRequests, editXhrResponses } from "./xhr.js";
7
+ const { parse, stringify } = JSON;
8
+ /** The value to use after an edit: a removed root stays as it was. */
9
+ function edited(edit, data) {
10
+ const result = edit(data);
11
+ return result === undefined || result.root === REMOVED ? data : result.root;
12
+ }
13
+ /** A text edit that parses JSON, edits it and gives its text back. */
14
+ export function jsonTextEdit(edit) {
15
+ return (text) => {
16
+ const result = edit(parse(text));
17
+ return result === undefined || result.root === REMOVED
18
+ ? undefined
19
+ : stringify(result.root);
20
+ };
21
+ }
22
+ /** The same for JSONL, line by line: a removed root drops its line. */
23
+ function jsonlTextEdit(edit) {
24
+ return (text) => {
25
+ let changed = false;
26
+ const lines = text.split("\n").flatMap((line) => {
27
+ let data;
28
+ try {
29
+ data = parse(line);
30
+ }
31
+ catch {
32
+ return [line];
33
+ }
34
+ const result = edit(data);
35
+ if (result === undefined)
36
+ return [line];
37
+ changed = true;
38
+ return result.root === REMOVED ? [] : [stringify(result.root)];
39
+ });
40
+ return changed ? lines.join("\n") : undefined;
41
+ };
42
+ }
43
+ /** The scriptlet and its `trusted-` twin, which may assign values. */
44
+ function withTrusted(name, run, queryAt = 0) {
45
+ const make = (trusted) => ({
46
+ name: trusted ? `trusted-${name}` : name,
47
+ ...(trusted ? { trusted } : {}),
48
+ run(...args) {
49
+ const edit = compileJsonPath(args[queryAt] ?? "", trusted);
50
+ if (edit !== undefined)
51
+ run(edit, args);
52
+ },
53
+ });
54
+ return [make(false), make(true)];
55
+ }
56
+ function onRequests(install, toText) {
57
+ return (edit, args) => install(requestMatcher(tokens(args, 1).propsToMatch), toText(edit));
58
+ }
59
+ export const EDIT_SCRIPTLETS = [
60
+ ...withTrusted("json-edit", (edit) => {
61
+ wrapMethod(JSON, "parse", (original, _self, args) => edited(edit, original(...args)));
62
+ }),
63
+ ...withTrusted("json-edit-xhr-response", onRequests(editXhrResponses, jsonTextEdit)),
64
+ ...withTrusted("json-edit-xhr-request", onRequests(editXhrRequests, jsonTextEdit)),
65
+ ...withTrusted("json-edit-fetch-response", onRequests(editFetchResponses, jsonTextEdit)),
66
+ ...withTrusted("json-edit-fetch-request", onRequests(editFetchRequests, jsonTextEdit)),
67
+ ...withTrusted("jsonl-edit-xhr-response", onRequests(editXhrResponses, jsonlTextEdit)),
68
+ ...withTrusted("jsonl-edit-fetch-response", onRequests(editFetchResponses, jsonlTextEdit)),
69
+ ...withTrusted("edit-outbound-object", (edit, [chain = ""]) => onMethod(chain, (original, _self, args) => edited(edit, original(...args))), 1),
70
+ ...withTrusted("edit-inbound-object", (edit, [chain = "", at = ""]) => onMethod(chain, (original, _self, args) => {
71
+ const index = position(at, args.length);
72
+ if (index !== undefined && index >= 0 && index < args.length) {
73
+ args[index] = edited(edit, args[index]);
74
+ }
75
+ return original(...args);
76
+ }), 2),
77
+ ...withTrusted("edit-this-object", (edit, [chain = ""]) => onMethod(chain, (original, self, args) => {
78
+ edit(self);
79
+ return original(...args);
80
+ }), 1),
81
+ ...withTrusted("edit-element-object", (edit, [selector = "", , timeout = ""]) => {
82
+ // Each element is edited once, as soon as it is in the document.
83
+ const done = new WeakSet();
84
+ const scan = () => {
85
+ try {
86
+ for (const element of document.querySelectorAll(selector)) {
87
+ if (done.has(element))
88
+ continue;
89
+ done.add(element);
90
+ edit(element);
91
+ }
92
+ }
93
+ catch {
94
+ observer.disconnect();
95
+ }
96
+ };
97
+ const observer = new MutationObserver(scan);
98
+ observer.observe(document, { childList: true, subtree: true });
99
+ scan();
100
+ const ms = Number(timeout);
101
+ if (ms > 0)
102
+ setTimeout(() => observer.disconnect(), ms);
103
+ }, 1),
104
+ ];
@@ -0,0 +1,3 @@
1
+ import type { Scriptlet } from "../types.js";
2
+ export declare const noEval: Scriptlet;
3
+ export declare const noEvalIf: Scriptlet;
@@ -0,0 +1,22 @@
1
+ import { matcher, wrapMethod } from "../helpers.js";
2
+ // Any wrapper makes the page's direct `eval(…)` calls indirect: the code
3
+ // they let through runs in the global scope, not the caller's.
4
+ function preventEval(needle) {
5
+ const matches = matcher(needle);
6
+ wrapMethod(window, "eval", (original, _self, args) => matches(String(args[0])) ? undefined : original(...args));
7
+ }
8
+ // Without a logger, the silent one is the same.
9
+ export const noEval = {
10
+ name: "noeval",
11
+ aliases: ["noeval-silent"],
12
+ run: () => preventEval(""),
13
+ };
14
+ export const noEvalIf = {
15
+ name: "noeval-if",
16
+ aliases: ["prevent-eval-if"],
17
+ run(needle = "") {
18
+ // Without a needle the scriptlet only logs, and there is no logger.
19
+ if (needle !== "")
20
+ preventEval(needle);
21
+ },
22
+ };
@@ -0,0 +1,10 @@
1
+ import type { Scriptlet } from "../types.js";
2
+ import { type Details } from "./request.js";
3
+ /** A change to a body's text: the new text, or undefined to keep it. */
4
+ export type TextEdit = (text: string) => string | undefined;
5
+ /** Hands the body of each fetch response that matches to `edit`. */
6
+ export declare function editFetchResponses(matches: (details: Details) => boolean, edit: TextEdit): void;
7
+ /** Hands the text body of each fetch request that matches to `edit`. */
8
+ export declare function editFetchRequests(matches: (details: Details) => boolean, edit: TextEdit): void;
9
+ export declare const preventFetchScriptlet: Scriptlet;
10
+ export declare const trustedPreventFetch: Scriptlet;
@@ -0,0 +1,145 @@
1
+ import { wrapMethod } from "../helpers.js";
2
+ import { fetchDetails, requestMatcher, responseBody, } from "./request.js";
3
+ import { isObject } from "./util.js";
4
+ const { apply } = Reflect;
5
+ const { defineProperties } = Object;
6
+ const NativePromise = Promise;
7
+ const then = Promise.prototype.then;
8
+ function tryEdit(edit, text) {
9
+ try {
10
+ return edit(text);
11
+ }
12
+ catch {
13
+ return undefined;
14
+ }
15
+ }
16
+ /** A Response with `body` and what the page reads of `like`. */
17
+ function responseLike(NativeResponse, like, body) {
18
+ try {
19
+ const response = new NativeResponse(body, {
20
+ status: like.status,
21
+ statusText: like.statusText,
22
+ headers: like.headers,
23
+ });
24
+ // The constructor can't set these: they shadow the prototype's.
25
+ const own = {};
26
+ for (const name of ["url", "type", "redirected", "ok"]) {
27
+ if (name in like)
28
+ own[name] = { value: like[name], configurable: true };
29
+ }
30
+ defineProperties(response, own);
31
+ return response;
32
+ }
33
+ catch {
34
+ return undefined;
35
+ }
36
+ }
37
+ /** Hands the body of each fetch response that matches to `edit`. */
38
+ export function editFetchResponses(matches, edit) {
39
+ if (typeof Response !== "function")
40
+ return;
41
+ const NativeResponse = Response;
42
+ const { clone, text } = Response.prototype;
43
+ wrapMethod(window, "fetch", (original, _self, args) => {
44
+ const result = original(...args);
45
+ if (!matches(fetchDetails(args)))
46
+ return result;
47
+ return apply(then, result, [
48
+ (response) => apply(then, apply(text, apply(clone, response, []), []), [
49
+ (body) => {
50
+ const edited = tryEdit(edit, body);
51
+ if (edited === undefined)
52
+ return response;
53
+ const { status, statusText, headers, url, type, redirected } = response;
54
+ return (responseLike(NativeResponse, { status, statusText, headers, url, type, redirected }, edited) ?? response);
55
+ },
56
+ () => response,
57
+ ]),
58
+ ]);
59
+ });
60
+ }
61
+ /** Hands the text body of each fetch request that matches to `edit`. */
62
+ export function editFetchRequests(matches, edit) {
63
+ const NativeRequest = typeof Request === "function" ? Request : undefined;
64
+ const { clone, text } = NativeRequest?.prototype ?? {};
65
+ wrapMethod(window, "fetch", (original, _self, args) => {
66
+ const [input, init] = args;
67
+ if (!matches(fetchDetails(args)))
68
+ return original(...args);
69
+ if (isObject(init) && typeof init.body === "string") {
70
+ const edited = tryEdit(edit, init.body);
71
+ if (edited !== undefined)
72
+ return original(input, { ...init, body: edited });
73
+ }
74
+ else if (NativeRequest !== undefined &&
75
+ input instanceof NativeRequest &&
76
+ input.body !== null) {
77
+ // The body of a Request can only be read asynchronously.
78
+ return apply(then, apply(text, apply(clone, input, []), []), [
79
+ (body) => {
80
+ const edited = tryEdit(edit, body);
81
+ return edited === undefined
82
+ ? original(...args)
83
+ : original(new NativeRequest(input, { body: edited }), init);
84
+ },
85
+ () => original(...args),
86
+ ]);
87
+ }
88
+ return original(...args);
89
+ });
90
+ }
91
+ const TYPES = new Set(["basic", "cors", "default", "error", "opaque"]);
92
+ const STATUS_TEXTS = new Set(["", "Not Found", "OK"]);
93
+ /** The `ok`, `type` and `statusText` a defused response gets. */
94
+ function responseProps(text) {
95
+ const props = {};
96
+ let given = text;
97
+ try {
98
+ if (text.startsWith("{"))
99
+ given = JSON.parse(text);
100
+ }
101
+ catch {
102
+ return props;
103
+ }
104
+ if (typeof given === "string")
105
+ given = { type: given };
106
+ if (!isObject(given))
107
+ return props;
108
+ if (typeof given.ok === "boolean")
109
+ props.ok = given.ok;
110
+ if (TYPES.has(given.type))
111
+ props.type = given.type;
112
+ if (STATUS_TEXTS.has(given.statusText)) {
113
+ props.statusText = given.statusText;
114
+ }
115
+ return props;
116
+ }
117
+ function preventFetch(args, trusted) {
118
+ // Without arguments the scriptlet only logs, and there is no logger.
119
+ if (args.every((arg) => arg === ""))
120
+ return;
121
+ const [props = "", directive = "", responseType = ""] = args;
122
+ const matches = requestMatcher(props, true);
123
+ const extra = responseProps(responseType);
124
+ const body = responseBody(directive, trusted);
125
+ if (typeof Response !== "function")
126
+ return;
127
+ const NativeResponse = Response;
128
+ wrapMethod(window, "fetch", (original, _self, callArgs) => {
129
+ const details = fetchDetails(callArgs);
130
+ if (!matches(details))
131
+ return original(...callArgs);
132
+ const response = responseLike(NativeResponse, { status: 200, statusText: "OK", url: details.url, ...extra }, body());
133
+ return new NativePromise((resolve) => resolve(response));
134
+ });
135
+ }
136
+ export const preventFetchScriptlet = {
137
+ name: "prevent-fetch",
138
+ aliases: ["no-fetch-if"],
139
+ run: (...args) => preventFetch(args, false),
140
+ };
141
+ export const trustedPreventFetch = {
142
+ name: "trusted-prevent-fetch",
143
+ trusted: true,
144
+ run: (...args) => preventFetch(args, true),
145
+ };
@@ -0,0 +1,3 @@
1
+ import type { Scriptlet } from "../types.js";
2
+ /** The scriptlets that act on requests, responses and the data they carry. */
3
+ export declare const NETWORK_SCRIPTLETS: readonly Scriptlet[];
@@ -0,0 +1,43 @@
1
+ import { EDIT_SCRIPTLETS } from "./edit.js";
2
+ import { noEval, noEvalIf } from "./eval.js";
3
+ import { preventFetchScriptlet, trustedPreventFetch } from "./fetch.js";
4
+ import { m3uPrune, xmlPrune } from "./media.js";
5
+ import { callNothrow, trustedOverrideElementMethod, trustedPreventDomBypass, trustedSuppressNativeMethod, } from "./methods.js";
6
+ import { evaldataPrune, jsonPrune, jsonPruneFetchResponse, jsonPruneXhrResponse, trustedPruneInboundObject, trustedPruneOutboundObject, } from "./prune.js";
7
+ import { preventCanvas, preventInnerHtml, preventRequestAnimationFrame, } from "./rendering.js";
8
+ import { trustedReplaceArgument, trustedReplaceFetchResponse, trustedReplaceOutboundText, trustedReplaceXhrResponse, } from "./replace.js";
9
+ import { noWebrtc, webrtcIf } from "./webrtc.js";
10
+ import { preventWindowOpen } from "./windowOpen.js";
11
+ import { preventXhrScriptlet, trustedPreventXhr } from "./xhr.js";
12
+ /** The scriptlets that act on requests, responses and the data they carry. */
13
+ export const NETWORK_SCRIPTLETS = [
14
+ ...EDIT_SCRIPTLETS,
15
+ callNothrow,
16
+ evaldataPrune,
17
+ jsonPrune,
18
+ jsonPruneFetchResponse,
19
+ jsonPruneXhrResponse,
20
+ m3uPrune,
21
+ noEval,
22
+ noEvalIf,
23
+ noWebrtc,
24
+ preventCanvas,
25
+ preventFetchScriptlet,
26
+ preventInnerHtml,
27
+ preventRequestAnimationFrame,
28
+ preventWindowOpen,
29
+ preventXhrScriptlet,
30
+ trustedOverrideElementMethod,
31
+ trustedPreventDomBypass,
32
+ trustedPreventFetch,
33
+ trustedPreventXhr,
34
+ trustedPruneInboundObject,
35
+ trustedPruneOutboundObject,
36
+ trustedReplaceArgument,
37
+ trustedReplaceFetchResponse,
38
+ trustedReplaceOutboundText,
39
+ trustedReplaceXhrResponse,
40
+ trustedSuppressNativeMethod,
41
+ webrtcIf,
42
+ xmlPrune,
43
+ ];
@@ -0,0 +1,13 @@
1
+ type Value = unknown;
2
+ /** An edit's result for a root the query removed (a JSONL line drops). */
3
+ export declare const REMOVED: unique symbol;
4
+ /**
5
+ * Compiles a query into an edit of a value, in place: it gives the new root
6
+ * (`REMOVED` when the query selected the root itself), or undefined when
7
+ * the query selected nothing. Assignments need `trusted`. An invalid or
8
+ * unsupported query gives undefined.
9
+ */
10
+ export declare function compileJsonPath(query: string, trusted: boolean): ((root: Value) => {
11
+ root: Value;
12
+ } | undefined) | undefined;
13
+ export {};