@tokioapp/adblock 0.0.0-stage → 0.1.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 +72 -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,66 @@
1
+ import { matcher, wrapMethod } from "./helpers.js";
2
+ /**
3
+ * A delay argument: a number (that delay), `min-max`, `min-` or `-max`
4
+ * (a range), with a leading `!` to invert. Nothing matches any delay.
5
+ */
6
+ function delayMatcher(pattern = "") {
7
+ const negated = pattern.startsWith("!");
8
+ if (negated)
9
+ pattern = pattern.slice(1);
10
+ if (pattern === "")
11
+ return () => true;
12
+ const range = /^(\d*)-(\d*)$/.exec(pattern);
13
+ const min = range === null ? Number(pattern) : Number(range[1] || 0);
14
+ const max = range === null ? min : Number(range[2] || Infinity);
15
+ return (delay) => (delay >= min && delay <= max) !== negated;
16
+ }
17
+ /** Makes the calls of a timer that match do nothing. */
18
+ function preventTimer(timer, needle, delay) {
19
+ // Without arguments the scriptlet only logs, and there is no logger.
20
+ if (needle === undefined && delay === undefined)
21
+ return;
22
+ const callbackMatches = matcher(needle, true);
23
+ const delayMatches = delayMatcher(delay);
24
+ wrapMethod(window, timer, (original, _self, args) => {
25
+ if (callbackMatches(String(args[0])) &&
26
+ delayMatches(Number(args[1] ?? 0))) {
27
+ args[0] = function () { };
28
+ }
29
+ return original(...args);
30
+ });
31
+ }
32
+ /** Multiplies the delay of the calls of a timer that match. */
33
+ function adjustTimer(timer, needle = "", delay = "1000", boost = "0.05") {
34
+ const callbackMatches = matcher(needle);
35
+ const wanted = delay === "*" ? undefined : Number(delay || 1000);
36
+ // Fifty times faster or slower at most.
37
+ const factor = Math.min(Math.max(Number(boost) || 0.05, 0.02), 50);
38
+ wrapMethod(window, timer, (original, _self, args) => {
39
+ const requested = Number(args[1] ?? 0);
40
+ if ((wanted === undefined || requested === wanted) &&
41
+ callbackMatches(String(args[0]))) {
42
+ args[1] = requested * factor;
43
+ }
44
+ return original(...args);
45
+ });
46
+ }
47
+ export const preventSetTimeout = {
48
+ name: "prevent-setTimeout",
49
+ aliases: ["no-setTimeout-if", "nostif", "setTimeout-defuser"],
50
+ run: (needle, delay) => preventTimer("setTimeout", needle, delay),
51
+ };
52
+ export const preventSetInterval = {
53
+ name: "prevent-setInterval",
54
+ aliases: ["no-setInterval-if", "nosiif", "setInterval-defuser"],
55
+ run: (needle, delay) => preventTimer("setInterval", needle, delay),
56
+ };
57
+ export const adjustSetTimeout = {
58
+ name: "adjust-setTimeout",
59
+ aliases: ["nano-setTimeout-booster", "nano-stb"],
60
+ run: (needle, delay, boost) => adjustTimer("setTimeout", needle, delay, boost),
61
+ };
62
+ export const adjustSetInterval = {
63
+ name: "adjust-setInterval",
64
+ aliases: ["nano-setInterval-booster", "nano-sib"],
65
+ run: (needle, delay, boost) => adjustTimer("setInterval", needle, delay, boost),
66
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A scriptlet: a function a filter (`example.com##+js(name, arg, …)`) runs in
3
+ * the page's own world, before the page's scripts.
4
+ *
5
+ * Every scriptlet is bundled with the helpers it imports into one function
6
+ * (`scripts/build`), which the preload hands to the page. So a scriptlet can
7
+ * import from `./helpers.js` and other files of this folder, and nothing
8
+ * else: no Node, no Electron, nothing from outside `src/scriptlets`.
9
+ */
10
+ export interface Scriptlet {
11
+ /** The name filters call it by, without `.js`. */
12
+ name: string;
13
+ /** Other names filters call it by. */
14
+ aliases?: readonly string[];
15
+ /**
16
+ * Only for filters of a trusted list: the scriptlet runs what the filter
17
+ * says, where the others can only pick from fixed behaviors.
18
+ */
19
+ trusted?: boolean;
20
+ /** Runs in the page. Arguments are the filter's, as text. */
21
+ run: (...args: string[]) => void;
22
+ }
23
+ /** A scriptlet a page gets, by its own name, with a filter's arguments. */
24
+ export interface ScriptletCall {
25
+ name: string;
26
+ args: string[];
27
+ /**
28
+ * The text of the redirect resources the arguments name (`war:noop.js`),
29
+ * by name. Main adds it: the resources aren't in the page.
30
+ */
31
+ resources?: Record<string, string>;
32
+ }
@@ -0,0 +1 @@
1
+ export {};
package/dist/util.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /** What was thrown, as an Error. */
2
+ export declare function toError(thrown: unknown): Error;
3
+ /** The regular expression, or undefined when the source or flags are bad. */
4
+ export declare function tryRegex(source: string, flags?: string): RegExp | undefined;
5
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
package/dist/util.js ADDED
@@ -0,0 +1,16 @@
1
+ /** What was thrown, as an Error. */
2
+ export function toError(thrown) {
3
+ return thrown instanceof Error ? thrown : new Error(String(thrown));
4
+ }
5
+ /** The regular expression, or undefined when the source or flags are bad. */
6
+ export function tryRegex(source, flags) {
7
+ try {
8
+ return new RegExp(source, flags);
9
+ }
10
+ catch {
11
+ return undefined;
12
+ }
13
+ }
14
+ export function isRecord(value) {
15
+ return typeof value === "object" && value !== null;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,85 @@
1
1
  {
2
2
  "name": "@tokioapp/adblock",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A clean-room, MIT content blocker for Electron that applies uBlock Origin's filter lists: network, cosmetic, procedural and scriptlet rules, with redirect resources.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./preload": "./dist/preload.cjs",
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "THIRD_PARTY_NOTICES.md"
19
+ ],
20
+ "sideEffects": false,
21
+ "engines": {
22
+ "node": ">=24"
23
+ },
24
+ "packageManager": "yarn@1.22.22",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.build.json && tsx scripts/build/index.ts",
30
+ "clean": "node -e \"for (const dir of ['dist', 'coverage']) fs.rmSync(dir, { recursive: true, force: true })\"",
31
+ "test": "vitest run",
32
+ "test:watch": "vitest",
33
+ "test:coverage": "vitest run --coverage",
34
+ "typecheck": "tsc --noEmit -p .",
35
+ "lint": "oxlint --type-aware",
36
+ "format": "prettier --write .",
37
+ "format:check": "prettier --check .",
38
+ "lists": "tsx scripts/lists/index.ts",
39
+ "bench": "node --expose-gc --import tsx scripts/bench/index.ts",
40
+ "smoke": "yarn build && tsx scripts/smoke/index.ts",
41
+ "prepublishOnly": "yarn build",
42
+ "prepare": "husky"
43
+ },
44
+ "author": "Akuqt",
45
+ "license": "MIT",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/tokio-app/adblock.git"
49
+ },
50
+ "keywords": [
51
+ "adblock",
52
+ "content-blocker",
53
+ "electron",
54
+ "ublock-origin",
55
+ "filter-lists",
56
+ "tokio"
57
+ ],
58
+ "bugs": {
59
+ "url": "https://github.com/tokio-app/adblock/issues"
60
+ },
61
+ "homepage": "https://tokio.ialfaro.com",
62
+ "peerDependencies": {
63
+ "electron": ">=35.0.0"
64
+ },
65
+ "devDependencies": {
66
+ "@commitlint/cli": "^21.2.4",
67
+ "@commitlint/config-conventional": "^21.2.4",
68
+ "@types/jsdom": "^30.0.0",
69
+ "@types/node": "^24.19.2",
70
+ "@vitest/coverage-v8": "^5.0.5",
71
+ "electron": "^45.3.0",
72
+ "esbuild": "^0.28.2",
73
+ "fallow": "^3.34.1",
74
+ "husky": "^9.1.7",
75
+ "jsdom": "^31.0.1",
76
+ "lint-staged": "^17.6.0",
77
+ "oxlint": "^1.89.0",
78
+ "oxlint-tsgolint": "^7.0.2004",
79
+ "prettier": "^3.9.10",
80
+ "tsx": "^4.23.15",
81
+ "typescript": "^7.0.2",
82
+ "vite": "^7.3.7",
83
+ "vitest": "^5.0.5"
84
+ }
85
+ }