@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,95 @@
1
+ import { type CosmeticRule, type ProceduralPlan, type ResponseHeaderRule, type ScriptletRule } from "../parser/types.js";
2
+ import { PublicSuffixList } from "../psl/psl.js";
3
+ import { type ScriptletCall } from "../scriptlets/index.js";
4
+ /** What a document gets when it starts loading. */
5
+ export interface DocumentCosmetics {
6
+ /** A stylesheet: the selectors to hide, and the `:style()` rules. */
7
+ css: string;
8
+ /** The procedural filters, for the page-side engine to run. */
9
+ procedural: ProceduralPlan[];
10
+ /** The scriptlets to run in the page's own world, before its scripts. */
11
+ scriptlets: ScriptletCall[];
12
+ /**
13
+ * Whether generic filters apply: the page then asks for the ones that
14
+ * match the classes and ids it uses (`genericFor`).
15
+ */
16
+ generic: boolean;
17
+ }
18
+ export interface CosmeticStats {
19
+ /** Cosmetic filters limited to hostnames, procedural ones included. */
20
+ specific: number;
21
+ /** Generic filters found by a class or id the page uses. */
22
+ genericByName: number;
23
+ /** Generic filters every page gets: no class or id to find them by. */
24
+ genericAlways: number;
25
+ procedural: number;
26
+ scriptlets: number;
27
+ /** Scriptlet names without an implementation, with their filter counts. */
28
+ unknownScriptlets: Record<string, number>;
29
+ /** Filters of untrusted lists that asked for a trusted scriptlet. */
30
+ untrustedScriptlets: number;
31
+ }
32
+ /**
33
+ * Decides what a document gets beyond its requests: the selectors to hide,
34
+ * the procedural filters, the scriptlets and the response headers to drop.
35
+ * Add every list's rules, then ask.
36
+ */
37
+ export declare class CosmeticEngine {
38
+ readonly stats: CosmeticStats;
39
+ private readonly psl;
40
+ /** Filters by the hostname, entity or `*` they are limited to. */
41
+ private readonly specific;
42
+ private readonly specificRegex;
43
+ /** The bodies of the filters an exception lifts, by hostname. */
44
+ private readonly exceptions;
45
+ private readonly exceptionRegex;
46
+ /** Generic filters by a class (`.name`) or id (`#name`) they need. */
47
+ private readonly genericByName;
48
+ private readonly genericAlways;
49
+ private readonly scriptlets;
50
+ private readonly scriptletRegex;
51
+ /** Call ids an exception lifts, by hostname; the empty id lifts them all. */
52
+ private readonly scriptletExceptions;
53
+ private readonly scriptletExceptionRegex;
54
+ private readonly headers;
55
+ private readonly headerExceptions;
56
+ private readonly documents;
57
+ constructor(options?: {
58
+ psl?: PublicSuffixList;
59
+ });
60
+ /** Adds a list's cosmetic, scriptlet and response header filters. */
61
+ add(rules: {
62
+ cosmetic: readonly CosmeticRule[];
63
+ scriptlets: readonly ScriptletRule[];
64
+ responseHeaders: readonly ResponseHeaderRule[];
65
+ }): void;
66
+ /** Where a filter applies: its hostnames, or everywhere without any. */
67
+ private hostsOf;
68
+ private addCosmetic;
69
+ private addScriptlet;
70
+ /**
71
+ * What filters can be filed under for a hostname: itself and its parents
72
+ * (`a.b.example`, `b.example`, `example`), the same as entities
73
+ * (`a.b.*`, `b.*`), and `*`.
74
+ */
75
+ private keys;
76
+ /** Everything filed for a hostname in a map and its regular expressions. */
77
+ private lookUp;
78
+ /**
79
+ * What a document on `hostname` gets as it starts loading. `switches` are
80
+ * the bits of `NetworkEngine.cosmeticSwitches()` for the document:
81
+ * `generichide`, `specifichide` and `elemhide` exceptions.
82
+ */
83
+ forDocument(hostname: string, switches?: number): DocumentCosmetics;
84
+ /**
85
+ * The stylesheet of the generic filters that need one of the classes or
86
+ * ids a page uses. The page asks as it finds new ones, so each is asked
87
+ * about once.
88
+ */
89
+ genericFor(hostname: string, classes: readonly string[], ids: readonly string[]): string;
90
+ /**
91
+ * The response headers `##^responseheader()` filters remove from a
92
+ * document on `hostname`, lowercase.
93
+ */
94
+ headersToRemove(hostname: string): string[];
95
+ }
@@ -0,0 +1,268 @@
1
+ import { selectorKey } from "../parser/selector.js";
2
+ import { T_ELEMHIDE, T_GENERICHIDE, T_SPECIFICHIDE, } from "../parser/types.js";
3
+ import { PublicSuffixList } from "../psl/psl.js";
4
+ import { findResource } from "../resources/index.js";
5
+ import { findScriptlet } from "../scriptlets/index.js";
6
+ import { tryRegex } from "../util.js";
7
+ const CACHE_SIZE = 256;
8
+ function push(map, key, value) {
9
+ const bucket = map.get(key);
10
+ if (bucket === undefined)
11
+ map.set(key, [value]);
12
+ else
13
+ bucket.push(value);
14
+ }
15
+ function add(map, key, value) {
16
+ const bucket = map.get(key);
17
+ if (bucket === undefined)
18
+ map.set(key, new Set([value]));
19
+ else
20
+ bucket.add(value);
21
+ }
22
+ /** The CSS of a filter that needs no JavaScript. */
23
+ function cssOf(rule, selector = rule.selector) {
24
+ const css = `${selector}{${rule.style ?? "display:none!important;"}}`;
25
+ return rule.media === undefined ? css : `@media ${rule.media}{${css}}`;
26
+ }
27
+ /**
28
+ * Files what applies to a hostname under it, and under `*` what applies
29
+ * everywhere. A regular expression goes in `regexes`, to be tried on each
30
+ * hostname.
31
+ */
32
+ function file(hosts, value, map, regexes) {
33
+ for (const host of hosts) {
34
+ if (!host.startsWith("/"))
35
+ map(host, value);
36
+ else {
37
+ const regex = tryRegex(host.slice(1, -1));
38
+ if (regex !== undefined)
39
+ regexes.push({ regex, value });
40
+ }
41
+ }
42
+ }
43
+ /**
44
+ * Decides what a document gets beyond its requests: the selectors to hide,
45
+ * the procedural filters, the scriptlets and the response headers to drop.
46
+ * Add every list's rules, then ask.
47
+ */
48
+ export class CosmeticEngine {
49
+ stats = {
50
+ specific: 0,
51
+ genericByName: 0,
52
+ genericAlways: 0,
53
+ procedural: 0,
54
+ scriptlets: 0,
55
+ unknownScriptlets: {},
56
+ untrustedScriptlets: 0,
57
+ };
58
+ psl;
59
+ /** Filters by the hostname, entity or `*` they are limited to. */
60
+ specific = new Map();
61
+ specificRegex = [];
62
+ /** The bodies of the filters an exception lifts, by hostname. */
63
+ exceptions = new Map();
64
+ exceptionRegex = [];
65
+ /** Generic filters by a class (`.name`) or id (`#name`) they need. */
66
+ genericByName = new Map();
67
+ genericAlways = [];
68
+ scriptlets = new Map();
69
+ scriptletRegex = [];
70
+ /** Call ids an exception lifts, by hostname; the empty id lifts them all. */
71
+ scriptletExceptions = new Map();
72
+ scriptletExceptionRegex = [];
73
+ headers = new Map();
74
+ headerExceptions = new Map();
75
+ documents = new Map();
76
+ constructor(options = {}) {
77
+ this.psl = options.psl ?? new PublicSuffixList();
78
+ }
79
+ /** Adds a list's cosmetic, scriptlet and response header filters. */
80
+ add(rules) {
81
+ this.documents.clear();
82
+ for (const rule of rules.cosmetic)
83
+ this.addCosmetic(rule);
84
+ for (const rule of rules.scriptlets)
85
+ this.addScriptlet(rule);
86
+ for (const rule of rules.responseHeaders) {
87
+ const map = rule.exception ? this.headerExceptions : this.headers;
88
+ for (const host of this.hostsOf(rule.hosts))
89
+ add(map, host, rule.header);
90
+ }
91
+ }
92
+ /** Where a filter applies: its hostnames, or everywhere without any. */
93
+ hostsOf(hosts) {
94
+ return hosts.include.length === 0 ? ["*"] : hosts.include;
95
+ }
96
+ addCosmetic(rule) {
97
+ const except = (host, key) => add(this.exceptions, host, key);
98
+ // A negated hostname is an exception for that hostname.
99
+ file(rule.hosts.exclude, rule.key, except, this.exceptionRegex);
100
+ if (rule.exception) {
101
+ file(this.hostsOf(rule.hosts), rule.key, except, this.exceptionRegex);
102
+ return;
103
+ }
104
+ if (rule.procedural !== undefined)
105
+ this.stats.procedural++;
106
+ if (rule.hosts.include.length > 0) {
107
+ this.stats.specific++;
108
+ file(rule.hosts.include, rule, (host, value) => push(this.specific, host, value), this.specificRegex);
109
+ return;
110
+ }
111
+ const key = selectorKey(rule.selector);
112
+ if (key === undefined) {
113
+ this.stats.genericAlways++;
114
+ this.genericAlways.push(rule);
115
+ }
116
+ else {
117
+ this.stats.genericByName++;
118
+ push(this.genericByName, key, rule);
119
+ }
120
+ }
121
+ addScriptlet(rule) {
122
+ const scriptlet = findScriptlet(rule.name);
123
+ const name = scriptlet?.name ?? rule.name;
124
+ const id = rule.name === "" ? "" : [name, ...rule.args].join("\x00");
125
+ const except = (host, value) => add(this.scriptletExceptions, host, value);
126
+ file(rule.hosts.exclude, id, except, this.scriptletExceptionRegex);
127
+ if (rule.exception) {
128
+ file(this.hostsOf(rule.hosts), id, except, this.scriptletExceptionRegex);
129
+ return;
130
+ }
131
+ if (scriptlet === undefined) {
132
+ const unknown = this.stats.unknownScriptlets;
133
+ unknown[rule.name] = (unknown[rule.name] ?? 0) + 1;
134
+ return;
135
+ }
136
+ if (scriptlet.trusted === true && !rule.trusted) {
137
+ this.stats.untrustedScriptlets++;
138
+ return;
139
+ }
140
+ this.stats.scriptlets++;
141
+ // A `war:` argument names a redirect resource, which the page doesn't
142
+ // have: its text goes with the call.
143
+ let resources;
144
+ for (const arg of rule.args) {
145
+ if (!arg.startsWith("war:"))
146
+ continue;
147
+ const body = findResource(arg.slice(4))?.body;
148
+ if (typeof body === "string")
149
+ (resources ??= {})[arg.slice(4)] = body;
150
+ }
151
+ file(rule.hosts.include, { id, name, args: rule.args, ...(resources && { resources }) }, (host, value) => push(this.scriptlets, host, value), this.scriptletRegex);
152
+ }
153
+ /**
154
+ * What filters can be filed under for a hostname: itself and its parents
155
+ * (`a.b.example`, `b.example`, `example`), the same as entities
156
+ * (`a.b.*`, `b.*`), and `*`.
157
+ */
158
+ keys(hostname) {
159
+ const keys = ["*"];
160
+ const entity = this.psl.entity(hostname);
161
+ for (let start = 0; start !== -1;) {
162
+ keys.push(start === 0 ? hostname : hostname.slice(start));
163
+ if (start < entity.length - 2)
164
+ keys.push(entity.slice(start));
165
+ const dot = hostname.indexOf(".", start);
166
+ start = dot === -1 ? -1 : dot + 1;
167
+ }
168
+ return keys;
169
+ }
170
+ /** Everything filed for a hostname in a map and its regular expressions. */
171
+ *lookUp(hostname, map, regexes) {
172
+ for (const key of this.keys(hostname)) {
173
+ const bucket = map.get(key);
174
+ if (bucket !== undefined)
175
+ yield* bucket;
176
+ }
177
+ for (const { regex, value } of regexes) {
178
+ if (regex.test(hostname))
179
+ yield value;
180
+ }
181
+ }
182
+ /**
183
+ * What a document on `hostname` gets as it starts loading. `switches` are
184
+ * the bits of `NetworkEngine.cosmeticSwitches()` for the document:
185
+ * `generichide`, `specifichide` and `elemhide` exceptions.
186
+ */
187
+ forDocument(hostname, switches = 0) {
188
+ const cacheKey = `${switches} ${hostname}`;
189
+ const cached = this.documents.get(cacheKey);
190
+ if (cached !== undefined)
191
+ return cached;
192
+ const css = new Set();
193
+ const procedural = [];
194
+ const generic = (switches & (T_ELEMHIDE | T_GENERICHIDE)) === 0;
195
+ if (!(switches & T_ELEMHIDE)) {
196
+ const lifted = new Set(this.lookUp(hostname, this.exceptions, this.exceptionRegex));
197
+ const apply = (rule) => {
198
+ if (lifted.has(rule.key))
199
+ return;
200
+ if (rule.procedural === undefined)
201
+ css.add(cssOf(rule));
202
+ else
203
+ procedural.push(rule.procedural);
204
+ };
205
+ for (const rule of this.lookUp(hostname, this.specific, this.specificRegex)) {
206
+ // `*##selector` is a generic filter that every page gets.
207
+ const everywhere = rule.hosts.include.includes("*");
208
+ if (everywhere ? generic : !(switches & T_SPECIFICHIDE))
209
+ apply(rule);
210
+ }
211
+ if (generic)
212
+ this.genericAlways.forEach(apply);
213
+ }
214
+ const liftedCalls = new Set(this.lookUp(hostname, this.scriptletExceptions, this.scriptletExceptionRegex));
215
+ const calls = new Map();
216
+ if (!liftedCalls.has("")) {
217
+ for (const { id, ...call } of this.lookUp(hostname, this.scriptlets, this.scriptletRegex)) {
218
+ if (!liftedCalls.has(id))
219
+ calls.set(id, call);
220
+ }
221
+ }
222
+ const result = {
223
+ css: [...css].join("\n"),
224
+ procedural,
225
+ scriptlets: [...calls.values()],
226
+ generic,
227
+ };
228
+ if (this.documents.size >= CACHE_SIZE)
229
+ this.documents.clear();
230
+ this.documents.set(cacheKey, result);
231
+ return result;
232
+ }
233
+ /**
234
+ * The stylesheet of the generic filters that need one of the classes or
235
+ * ids a page uses. The page asks as it finds new ones, so each is asked
236
+ * about once.
237
+ */
238
+ genericFor(hostname, classes, ids) {
239
+ const lifted = new Set(this.lookUp(hostname, this.exceptions, this.exceptionRegex));
240
+ const css = [];
241
+ const collect = (prefix, names) => {
242
+ for (const name of names) {
243
+ const key = prefix + name;
244
+ for (const rule of this.genericByName.get(key) ?? []) {
245
+ if (lifted.has(rule.key))
246
+ continue;
247
+ // A bare class or id never hides the page itself.
248
+ css.push(rule.selector === key
249
+ ? cssOf(rule, `${key}:not(html):not(body)`)
250
+ : cssOf(rule));
251
+ }
252
+ }
253
+ };
254
+ collect(".", classes);
255
+ collect("#", ids);
256
+ return css.join("\n");
257
+ }
258
+ /**
259
+ * The response headers `##^responseheader()` filters remove from a
260
+ * document on `hostname`, lowercase.
261
+ */
262
+ headersToRemove(hostname) {
263
+ if (this.headers.size === 0)
264
+ return [];
265
+ const lifted = new Set(this.lookUp(hostname, this.headerExceptions, []));
266
+ return [...new Set(this.lookUp(hostname, this.headers, []))].filter((header) => !lifted.has(header));
267
+ }
268
+ }
@@ -0,0 +1,84 @@
1
+ import type { CallbackResponse, HeadersReceivedResponse, IpcMain, OnBeforeRequestListenerDetails, OnHeadersReceivedListenerDetails, Session } from "electron";
2
+ import { Engine, type EngineOptions } from "../engine.js";
3
+ export interface ElectronBlockerOptions extends EngineOptions {
4
+ /** Electron's `ipcMain`: the preload asks the main process through it. */
5
+ ipcMain: IpcMain;
6
+ /**
7
+ * The path of the preload script. Defaults to the one in this package;
8
+ * pass `require.resolve("@tokioapp/adblock/preload")` when the package is
9
+ * bundled into the app's own code.
10
+ */
11
+ preloadPath?: string;
12
+ }
13
+ /** What the blocker did since it was created. */
14
+ export interface BlockerCounts {
15
+ /** Requests blocked. */
16
+ blocked: number;
17
+ /** Blocked requests answered with a redirect resource. */
18
+ redirected: number;
19
+ /** Requests sent to another URL (`removeparam=`, `urlskip=`…). */
20
+ rewritten: number;
21
+ /** Responses blocked, or given a policy or fewer headers. */
22
+ responses: number;
23
+ /** Documents given scriptlets or cosmetic filters. */
24
+ documents: number;
25
+ /** Scriptlets given to documents. */
26
+ scriptlets: number;
27
+ }
28
+ /**
29
+ * Applies uBlock Origin's filter lists in Electron sessions: it blocks,
30
+ * redirects and rewrites requests, adds policies to documents, and gives
31
+ * each document its scriptlets and cosmetic filters through a preload.
32
+ *
33
+ * The app registers `adblockScheme` with its own schemes before `ready`,
34
+ * creates one blocker, and enables it in the sessions it wants filtered.
35
+ * In those sessions the blocker owns `webRequest.onBeforeRequest` and
36
+ * `webRequest.onHeadersReceived`: Electron keeps one listener per event.
37
+ */
38
+ export declare class ElectronBlocker {
39
+ /** The lists and what decides from them. */
40
+ readonly engine: Engine;
41
+ /** Resolves once the lists are in effect. It never rejects. */
42
+ readonly ready: Promise<void>;
43
+ readonly counts: BlockerCounts;
44
+ private readonly ipcMain;
45
+ private readonly preloadPath;
46
+ private readonly onError;
47
+ /** The sessions it is enabled in, with the id of their preload. */
48
+ private readonly sessions;
49
+ private readonly token;
50
+ private disposed;
51
+ constructor(options: ElectronBlockerOptions);
52
+ /** Starts filtering a session. Enabling one twice changes nothing. */
53
+ enable(session: Session): void;
54
+ /** Stops filtering a session. Pages already loaded keep what they got. */
55
+ disable(session: Session): void;
56
+ isEnabled(session: Session): boolean;
57
+ /** Looks for new versions of the lists. See `Engine.update()`. */
58
+ update(force?: boolean): Promise<boolean>;
59
+ /** Stops everything: every session, the updates and the IPC channels. */
60
+ dispose(): void;
61
+ /** The address a redirect resource is served from. */
62
+ private resourceUrl;
63
+ /**
64
+ * `webRequest.onBeforeRequest`: blocks, redirects or rewrites a request.
65
+ * A listener of the app's own can call it for the sessions it handles.
66
+ */
67
+ readonly onBeforeRequest: (details: OnBeforeRequestListenerDetails, callback: (response: CallbackResponse) => void) => void;
68
+ /** What filters ask about a request Electron describes. */
69
+ private requestOf;
70
+ private decide;
71
+ /**
72
+ * `webRequest.onHeadersReceived`: blocks a response by its headers, adds
73
+ * the policies of `csp=` and `permissions=` to a document and removes the
74
+ * headers `##^responseheader()` names.
75
+ */
76
+ readonly onHeadersReceived: (details: OnHeadersReceivedListenerDetails, callback: (response: HeadersReceivedResponse) => void) => void;
77
+ private decideResponse;
78
+ /** The protocol handler: serves the redirect resources. */
79
+ private readonly serve;
80
+ /** A document asks what it gets: answered at once, from memory. */
81
+ private readonly onDocument;
82
+ /** A page asks for the generic filters of its classes and ids. */
83
+ private readonly onGeneric;
84
+ }