wxt 0.18.15 → 0.19.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 (257) hide show
  1. package/bin/wxt.mjs +1 -1
  2. package/dist/browser/chrome.d.ts +17 -0
  3. package/dist/browser/chrome.mjs +7 -0
  4. package/dist/browser/index.d.ts +18 -0
  5. package/dist/browser/index.mjs +2 -0
  6. package/dist/builtin-modules/index.d.ts +2 -0
  7. package/dist/builtin-modules/index.mjs +2 -0
  8. package/dist/builtin-modules/unimport.d.ts +8 -0
  9. package/dist/builtin-modules/unimport.mjs +99 -0
  10. package/dist/cli/cli-utils.d.ts +25 -0
  11. package/dist/cli/cli-utils.mjs +58 -0
  12. package/dist/cli/commands.d.ts +2 -0
  13. package/dist/cli/commands.mjs +104 -0
  14. package/dist/cli/index.d.ts +1 -0
  15. package/dist/cli/index.mjs +11 -0
  16. package/dist/client/app-config.d.ts +2 -0
  17. package/dist/client/app-config.mjs +4 -0
  18. package/dist/client/content-scripts/content-script-context.d.ts +114 -0
  19. package/dist/client/content-scripts/content-script-context.mjs +169 -0
  20. package/dist/client/content-scripts/custom-events.d.ts +10 -0
  21. package/dist/client/content-scripts/custom-events.mjs +13 -0
  22. package/dist/client/content-scripts/index.d.ts +2 -0
  23. package/dist/client/content-scripts/index.mjs +2 -0
  24. package/dist/client/content-scripts/location-watcher.d.ts +12 -0
  25. package/dist/client/content-scripts/location-watcher.mjs +22 -0
  26. package/dist/client/content-scripts/ui/index.d.ts +23 -0
  27. package/dist/client/content-scripts/ui/index.mjs +188 -0
  28. package/dist/{client.d.ts → client/content-scripts/ui/types.d.ts} +17 -45
  29. package/dist/client/content-scripts/ui/types.mjs +0 -0
  30. package/dist/client/index.d.ts +7 -0
  31. package/dist/client/index.mjs +2 -0
  32. package/dist/core/build.d.ts +15 -0
  33. package/dist/core/build.mjs +6 -0
  34. package/dist/core/builders/vite/index.d.ts +3 -0
  35. package/dist/core/builders/vite/index.mjs +285 -0
  36. package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +7 -0
  37. package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +15 -0
  38. package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +13 -0
  39. package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +22 -0
  40. package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +14 -0
  41. package/dist/core/builders/vite/plugins/defineImportMeta.mjs +13 -0
  42. package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +7 -0
  43. package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +140 -0
  44. package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +6 -0
  45. package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -0
  46. package/dist/core/builders/vite/plugins/download.d.ts +10 -0
  47. package/dist/core/builders/vite/plugins/download.mjs +14 -0
  48. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +6 -0
  49. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +16 -0
  50. package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +11 -0
  51. package/dist/core/builders/vite/plugins/extensionApiMock.mjs +26 -0
  52. package/dist/core/builders/vite/plugins/globals.d.ts +3 -0
  53. package/dist/core/builders/vite/plugins/globals.mjs +15 -0
  54. package/dist/core/builders/vite/plugins/index.d.ts +17 -0
  55. package/dist/core/builders/vite/plugins/index.mjs +17 -0
  56. package/dist/core/builders/vite/plugins/multipageMove.d.ts +20 -0
  57. package/dist/core/builders/vite/plugins/multipageMove.mjs +59 -0
  58. package/dist/core/builders/vite/plugins/noopBackground.d.ts +6 -0
  59. package/dist/core/builders/vite/plugins/noopBackground.mjs +17 -0
  60. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +6 -0
  61. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +12 -0
  62. package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +6 -0
  63. package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +26 -0
  64. package/dist/core/builders/vite/plugins/resolveExtensionApi.d.ts +10 -0
  65. package/dist/core/builders/vite/plugins/resolveExtensionApi.mjs +15 -0
  66. package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +6 -0
  67. package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +30 -0
  68. package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +3 -0
  69. package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +12 -0
  70. package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +6 -0
  71. package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +56 -0
  72. package/dist/core/clean.d.ts +21 -0
  73. package/dist/core/clean.mjs +38 -0
  74. package/dist/core/create-server.d.ts +11 -0
  75. package/dist/core/create-server.mjs +206 -0
  76. package/dist/core/define-config.d.ts +2 -0
  77. package/dist/core/define-config.mjs +3 -0
  78. package/dist/core/define-runner-config.d.ts +2 -0
  79. package/dist/core/define-runner-config.mjs +3 -0
  80. package/dist/core/index.d.ts +8 -0
  81. package/dist/core/index.mjs +8 -0
  82. package/dist/core/initialize.d.ts +5 -0
  83. package/dist/core/initialize.mjs +128 -0
  84. package/dist/core/package-managers/bun.d.ts +2 -0
  85. package/dist/core/package-managers/bun.mjs +19 -0
  86. package/dist/core/package-managers/index.d.ts +2 -0
  87. package/dist/core/package-managers/index.mjs +65 -0
  88. package/dist/core/package-managers/npm.d.ts +17 -0
  89. package/dist/core/package-managers/npm.mjs +58 -0
  90. package/dist/core/package-managers/pnpm.d.ts +2 -0
  91. package/dist/core/package-managers/pnpm.mjs +21 -0
  92. package/dist/core/package-managers/types.d.ts +2 -0
  93. package/dist/core/package-managers/types.mjs +0 -0
  94. package/dist/core/package-managers/yarn.d.ts +2 -0
  95. package/dist/core/package-managers/yarn.mjs +31 -0
  96. package/dist/core/prepare.d.ts +2 -0
  97. package/dist/core/prepare.mjs +8 -0
  98. package/dist/core/runners/index.d.ts +2 -0
  99. package/dist/core/runners/index.mjs +12 -0
  100. package/dist/core/runners/manual.d.ts +5 -0
  101. package/dist/core/runners/manual.mjs +16 -0
  102. package/dist/core/runners/safari.d.ts +5 -0
  103. package/dist/core/runners/safari.mjs +16 -0
  104. package/dist/core/runners/web-ext.d.ts +5 -0
  105. package/dist/core/runners/web-ext.mjs +78 -0
  106. package/dist/core/runners/wsl.d.ts +5 -0
  107. package/dist/core/runners/wsl.mjs +16 -0
  108. package/dist/core/utils/arrays.d.ts +13 -0
  109. package/dist/{chunk-BERPNPEZ.js → core/utils/arrays.mjs} +6 -10
  110. package/dist/core/utils/building/build-entrypoints.d.ts +3 -0
  111. package/dist/core/utils/building/build-entrypoints.mjs +47 -0
  112. package/dist/core/utils/building/detect-dev-changes.d.ts +57 -0
  113. package/dist/core/utils/building/detect-dev-changes.mjs +93 -0
  114. package/dist/core/utils/building/find-entrypoints.d.ts +5 -0
  115. package/dist/core/utils/building/find-entrypoints.mjs +385 -0
  116. package/dist/core/utils/building/generate-wxt-dir.d.ts +5 -0
  117. package/dist/core/utils/building/generate-wxt-dir.mjs +192 -0
  118. package/dist/core/utils/building/group-entrypoints.d.ts +8 -0
  119. package/dist/core/utils/building/group-entrypoints.mjs +37 -0
  120. package/dist/core/utils/building/import-entrypoint.d.ts +16 -0
  121. package/dist/core/utils/building/import-entrypoint.mjs +97 -0
  122. package/dist/core/utils/building/index.d.ts +9 -0
  123. package/dist/core/utils/building/index.mjs +9 -0
  124. package/dist/core/utils/building/internal-build.d.ts +12 -0
  125. package/dist/core/utils/building/internal-build.mjs +112 -0
  126. package/dist/core/utils/building/rebuild.d.ts +23 -0
  127. package/dist/core/utils/building/rebuild.mjs +39 -0
  128. package/dist/core/utils/building/resolve-config.d.ts +11 -0
  129. package/dist/core/utils/building/resolve-config.mjs +364 -0
  130. package/dist/core/utils/cache.d.ts +8 -0
  131. package/dist/core/utils/cache.mjs +21 -0
  132. package/dist/core/utils/cli.d.ts +3 -0
  133. package/dist/core/utils/cli.mjs +26 -0
  134. package/dist/core/utils/constants.d.ts +5 -0
  135. package/dist/core/utils/constants.mjs +1 -0
  136. package/dist/core/utils/content-scripts.d.ts +11 -0
  137. package/dist/core/utils/content-scripts.mjs +60 -0
  138. package/dist/core/utils/content-security-policy.d.ts +14 -0
  139. package/dist/core/utils/content-security-policy.mjs +39 -0
  140. package/dist/core/utils/entrypoints.d.ts +25 -0
  141. package/dist/core/utils/entrypoints.mjs +31 -0
  142. package/dist/core/utils/eslint.d.ts +1 -0
  143. package/dist/core/utils/eslint.mjs +11 -0
  144. package/dist/core/utils/fs.d.ts +13 -0
  145. package/dist/core/utils/fs.mjs +15 -0
  146. package/dist/core/utils/globals.d.ts +11 -0
  147. package/dist/core/utils/globals.mjs +53 -0
  148. package/dist/core/utils/i18n.d.ts +11 -0
  149. package/dist/core/utils/i18n.mjs +35 -0
  150. package/dist/core/utils/log/index.d.ts +4 -0
  151. package/dist/core/utils/log/index.mjs +4 -0
  152. package/dist/core/utils/log/printBuildSummary.d.ts +2 -0
  153. package/dist/core/utils/log/printBuildSummary.mjs +32 -0
  154. package/dist/core/utils/log/printFileList.d.ts +1 -0
  155. package/dist/core/utils/log/printFileList.mjs +42 -0
  156. package/dist/core/utils/log/printHeader.d.ts +1 -0
  157. package/dist/core/utils/log/printHeader.mjs +7 -0
  158. package/dist/core/utils/log/printTable.d.ts +1 -0
  159. package/dist/core/utils/log/printTable.mjs +22 -0
  160. package/dist/core/utils/manifest.d.ts +44 -0
  161. package/dist/core/utils/manifest.mjs +512 -0
  162. package/dist/core/utils/network.d.ts +7 -0
  163. package/dist/core/utils/network.mjs +38 -0
  164. package/dist/core/utils/package.d.ts +6 -0
  165. package/dist/core/utils/package.mjs +14 -0
  166. package/dist/core/utils/paths.d.ts +11 -0
  167. package/dist/core/utils/paths.mjs +10 -0
  168. package/dist/core/utils/strings.d.ts +14 -0
  169. package/dist/core/utils/strings.mjs +18 -0
  170. package/dist/core/utils/testing/fake-objects.d.ts +4556 -0
  171. package/dist/core/utils/testing/fake-objects.mjs +322 -0
  172. package/dist/core/utils/time.d.ts +9 -0
  173. package/dist/core/utils/time.mjs +17 -0
  174. package/dist/core/utils/transform.d.ts +9 -0
  175. package/dist/core/utils/transform.mjs +17 -0
  176. package/dist/core/utils/types.d.ts +10 -0
  177. package/dist/core/utils/types.mjs +0 -0
  178. package/dist/core/utils/validation.d.ts +15 -0
  179. package/dist/core/utils/validation.mjs +55 -0
  180. package/dist/core/utils/virtual-modules.d.ts +22 -0
  181. package/dist/core/utils/virtual-modules.mjs +14 -0
  182. package/dist/core/utils/wsl.d.ts +4 -0
  183. package/dist/core/utils/wsl.mjs +4 -0
  184. package/dist/core/wxt.d.ts +19 -0
  185. package/dist/core/wxt.mjs +41 -0
  186. package/dist/core/zip.d.ts +7 -0
  187. package/dist/core/zip.mjs +137 -0
  188. package/dist/index.d.ts +4 -79
  189. package/dist/index.mjs +3 -0
  190. package/dist/modules.d.ts +10 -20
  191. package/dist/{chunk-6XSIWUWF.js → modules.mjs} +7 -16
  192. package/dist/sandbox/define-app-config.d.ts +19 -0
  193. package/dist/sandbox/define-app-config.mjs +3 -0
  194. package/dist/sandbox/define-background.d.ts +3 -0
  195. package/dist/sandbox/define-background.mjs +4 -0
  196. package/dist/sandbox/define-content-script.d.ts +2 -0
  197. package/dist/sandbox/define-content-script.mjs +3 -0
  198. package/dist/sandbox/define-unlisted-script.d.ts +3 -0
  199. package/dist/sandbox/define-unlisted-script.mjs +4 -0
  200. package/dist/sandbox/define-wxt-plugin.d.ts +2 -0
  201. package/dist/sandbox/define-wxt-plugin.mjs +3 -0
  202. package/dist/sandbox/dev-server-websocket.d.ts +21 -0
  203. package/dist/sandbox/dev-server-websocket.mjs +37 -0
  204. package/dist/sandbox/index.d.ts +11 -0
  205. package/dist/sandbox/index.mjs +6 -0
  206. package/dist/sandbox/utils/logger.d.ts +9 -0
  207. package/dist/sandbox/utils/logger.mjs +15 -0
  208. package/dist/storage.d.ts +39 -16
  209. package/dist/{storage.js → storage.mjs} +30 -41
  210. package/dist/testing/fake-browser.d.ts +1 -0
  211. package/dist/testing/fake-browser.mjs +1 -0
  212. package/dist/testing/index.d.ts +5 -0
  213. package/dist/testing/index.mjs +2 -0
  214. package/dist/{testing.d.ts → testing/wxt-vitest-plugin.d.ts} +3 -15
  215. package/dist/testing/wxt-vitest-plugin.mjs +26 -0
  216. package/dist/{index-nWRfwAJi.d.cts → types.d.ts} +150 -264
  217. package/dist/types.mjs +0 -0
  218. package/dist/version.d.ts +1 -0
  219. package/dist/version.mjs +1 -0
  220. package/dist/virtual/{background-entrypoint.js → background-entrypoint.mjs} +31 -40
  221. package/dist/virtual/{content-script-isolated-world-entrypoint.js → content-script-isolated-world-entrypoint.mjs} +9 -13
  222. package/dist/virtual/{content-script-main-world-entrypoint.js → content-script-main-world-entrypoint.mjs} +9 -14
  223. package/dist/virtual/mock-browser.mjs +6 -0
  224. package/dist/virtual/{reload-html.js → reload-html.mjs} +8 -9
  225. package/dist/virtual/{unlisted-script-entrypoint.js → unlisted-script-entrypoint.mjs} +8 -12
  226. package/package.json +47 -57
  227. package/dist/browser.d.ts +0 -18
  228. package/dist/browser.js +0 -6
  229. package/dist/chunk-BM6QYGAW.js +0 -1063
  230. package/dist/chunk-FNTE2L27.js +0 -7
  231. package/dist/chunk-FP7RYLVL.js +0 -3617
  232. package/dist/chunk-KPD5J7PZ.js +0 -1065
  233. package/dist/chunk-QGM4M3NI.js +0 -37
  234. package/dist/chunk-SGKCDMVR.js +0 -38
  235. package/dist/cli.d.ts +0 -2
  236. package/dist/cli.js +0 -4438
  237. package/dist/client.js +0 -424
  238. package/dist/define-app-config-bg54F_lV.d.ts +0 -294
  239. package/dist/execa-4UBDUBJZ.js +0 -7244
  240. package/dist/execa-QLUM2B3W.js +0 -7245
  241. package/dist/index-nWRfwAJi.d.ts +0 -1401
  242. package/dist/index.cjs +0 -14473
  243. package/dist/index.d.cts +0 -81
  244. package/dist/index.js +0 -696
  245. package/dist/modules.cjs +0 -96
  246. package/dist/modules.d.cts +0 -119
  247. package/dist/modules.js +0 -17
  248. package/dist/prompt-25QIVJDC.js +0 -755
  249. package/dist/prompt-7BMKNSWS.js +0 -754
  250. package/dist/sandbox.d.ts +0 -16
  251. package/dist/sandbox.js +0 -36
  252. package/dist/storage.cjs +0 -439
  253. package/dist/storage.d.cts +0 -200
  254. package/dist/testing.cjs +0 -2815
  255. package/dist/testing.d.cts +0 -30
  256. package/dist/testing.js +0 -40
  257. package/dist/virtual/mock-browser.js +0 -6
package/dist/client.js DELETED
@@ -1,424 +0,0 @@
1
- import {
2
- browser
3
- } from "./chunk-FNTE2L27.js";
4
-
5
- // src/sandbox/utils/logger.ts
6
- function print(method, ...args) {
7
- if (import.meta.env.MODE === "production") return;
8
- if (typeof args[0] === "string") {
9
- const message = args.shift();
10
- method(`[wxt] ${message}`, ...args);
11
- } else {
12
- method("[wxt]", ...args);
13
- }
14
- }
15
- var logger = {
16
- debug: (...args) => print(console.debug, ...args),
17
- log: (...args) => print(console.log, ...args),
18
- warn: (...args) => print(console.warn, ...args),
19
- error: (...args) => print(console.error, ...args)
20
- };
21
-
22
- // src/client/content-scripts/custom-events.ts
23
- var WxtLocationChangeEvent = class _WxtLocationChangeEvent extends Event {
24
- constructor(newUrl, oldUrl) {
25
- super(_WxtLocationChangeEvent.EVENT_NAME, {});
26
- this.newUrl = newUrl;
27
- this.oldUrl = oldUrl;
28
- }
29
- static EVENT_NAME = getUniqueEventName("wxt:locationchange");
30
- };
31
- function getUniqueEventName(eventName) {
32
- const entrypointName = typeof import.meta.env === "undefined" ? "build" : import.meta.env.ENTRYPOINT;
33
- return `${browser.runtime.id}:${entrypointName}:${eventName}`;
34
- }
35
-
36
- // src/client/content-scripts/location-watcher.ts
37
- function createLocationWatcher(ctx) {
38
- let interval;
39
- let oldUrl;
40
- return {
41
- /**
42
- * Ensure the location watcher is actively looking for URL changes. If it's already watching,
43
- * this is a noop.
44
- */
45
- run() {
46
- if (interval != null) return;
47
- oldUrl = new URL(location.href);
48
- interval = ctx.setInterval(() => {
49
- let newUrl = new URL(location.href);
50
- if (newUrl.href !== oldUrl.href) {
51
- window.dispatchEvent(new WxtLocationChangeEvent(newUrl, oldUrl));
52
- oldUrl = newUrl;
53
- }
54
- }, 1e3);
55
- }
56
- };
57
- }
58
-
59
- // src/client/content-scripts/content-script-context.ts
60
- var ContentScriptContext = class _ContentScriptContext {
61
- constructor(contentScriptName, options) {
62
- this.contentScriptName = contentScriptName;
63
- this.options = options;
64
- this.#abortController = new AbortController();
65
- if (this.#isTopFrame) {
66
- this.#stopOldScripts();
67
- }
68
- this.setTimeout(() => {
69
- this.#listenForNewerScripts();
70
- });
71
- }
72
- static SCRIPT_STARTED_MESSAGE_TYPE = "wxt:content-script-started";
73
- #isTopFrame = window.self === window.top;
74
- #abortController;
75
- #locationWatcher = createLocationWatcher(this);
76
- get signal() {
77
- return this.#abortController.signal;
78
- }
79
- abort(reason) {
80
- return this.#abortController.abort(reason);
81
- }
82
- get isInvalid() {
83
- if (browser.runtime.id == null) {
84
- this.notifyInvalidated();
85
- }
86
- return this.signal.aborted;
87
- }
88
- get isValid() {
89
- return !this.isInvalid;
90
- }
91
- /**
92
- * Add a listener that is called when the content script's context is invalidated.
93
- *
94
- * @returns A function to remove the listener.
95
- *
96
- * @example
97
- * browser.runtime.onMessage.addListener(cb);
98
- * const removeInvalidatedListener = ctx.onInvalidated(() => {
99
- * browser.runtime.onMessage.removeListener(cb);
100
- * })
101
- * // ...
102
- * removeInvalidatedListener();
103
- */
104
- onInvalidated(cb) {
105
- this.signal.addEventListener("abort", cb);
106
- return () => this.signal.removeEventListener("abort", cb);
107
- }
108
- /**
109
- * Return a promise that never resolves. Useful if you have an async function that shouldn't run
110
- * after the context is expired.
111
- *
112
- * @example
113
- * const getValueFromStorage = async () => {
114
- * if (ctx.isInvalid) return ctx.block();
115
- *
116
- * // ...
117
- * }
118
- */
119
- block() {
120
- return new Promise(() => {
121
- });
122
- }
123
- /**
124
- * Wrapper around `window.setInterval` that automatically clears the interval when invalidated.
125
- */
126
- setInterval(handler, timeout) {
127
- const id = setInterval(() => {
128
- if (this.isValid) handler();
129
- }, timeout);
130
- this.onInvalidated(() => clearInterval(id));
131
- return id;
132
- }
133
- /**
134
- * Wrapper around `window.setTimeout` that automatically clears the interval when invalidated.
135
- */
136
- setTimeout(handler, timeout) {
137
- const id = setTimeout(() => {
138
- if (this.isValid) handler();
139
- }, timeout);
140
- this.onInvalidated(() => clearTimeout(id));
141
- return id;
142
- }
143
- /**
144
- * Wrapper around `window.requestAnimationFrame` that automatically cancels the request when
145
- * invalidated.
146
- */
147
- requestAnimationFrame(callback) {
148
- const id = requestAnimationFrame((...args) => {
149
- if (this.isValid) callback(...args);
150
- });
151
- this.onInvalidated(() => cancelAnimationFrame(id));
152
- return id;
153
- }
154
- /**
155
- * Wrapper around `window.requestIdleCallback` that automatically cancels the request when
156
- * invalidated.
157
- */
158
- requestIdleCallback(callback, options) {
159
- const id = requestIdleCallback((...args) => {
160
- if (!this.signal.aborted) callback(...args);
161
- }, options);
162
- this.onInvalidated(() => cancelIdleCallback(id));
163
- return id;
164
- }
165
- /**
166
- * Call `target.addEventListener` and remove the event listener when the context is invalidated.
167
- *
168
- * Includes additional events useful for content scripts:
169
- *
170
- * - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
171
- * scripts are not reloaded when navigating this way, so this can be used to reset the content
172
- * script state on URL change, or run custom code.
173
- *
174
- * @example
175
- * ctx.addEventListener(document, "visibilitychange", () => {
176
- * // ...
177
- * });
178
- * ctx.addEventListener(document, "wxt:locationchange", () => {
179
- * // ...
180
- * });
181
- */
182
- addEventListener(target, type, handler, options) {
183
- if (type === "wxt:locationchange") {
184
- if (this.isValid) this.#locationWatcher.run();
185
- }
186
- target.addEventListener?.(
187
- type.startsWith("wxt:") ? getUniqueEventName(type) : type,
188
- // @ts-expect-error: Event don't match, but that's OK, EventTarget doesn't allow custom types in the callback
189
- handler,
190
- {
191
- ...options,
192
- signal: this.signal
193
- }
194
- );
195
- }
196
- /**
197
- * @internal
198
- * Abort the abort controller and execute all `onInvalidated` listeners.
199
- */
200
- notifyInvalidated() {
201
- this.abort("Content script context invalidated");
202
- logger.debug(
203
- `Content script "${this.contentScriptName}" context invalidated`
204
- );
205
- }
206
- #stopOldScripts() {
207
- window.postMessage(
208
- {
209
- type: _ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE,
210
- contentScriptName: this.contentScriptName
211
- },
212
- "*"
213
- );
214
- }
215
- #listenForNewerScripts() {
216
- const cb = (event) => {
217
- if (event.data?.type === _ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE && event.data?.contentScriptName === this.contentScriptName) {
218
- this.notifyInvalidated();
219
- }
220
- };
221
- addEventListener("message", cb);
222
- this.onInvalidated(() => removeEventListener("message", cb));
223
- }
224
- };
225
-
226
- // src/client/content-scripts/ui/index.ts
227
- import { createIsolatedElement } from "@webext-core/isolated-element";
228
- function createIntegratedUi(ctx, options) {
229
- const wrapper = document.createElement(options.tag || "div");
230
- wrapper.setAttribute("data-wxt-integrated", "");
231
- let mounted = void 0;
232
- const mount = () => {
233
- applyPosition(wrapper, void 0, options);
234
- mountUi(wrapper, options);
235
- mounted = options.onMount?.(wrapper);
236
- };
237
- const remove = () => {
238
- options.onRemove?.(mounted);
239
- wrapper.remove();
240
- mounted = void 0;
241
- };
242
- ctx.onInvalidated(remove);
243
- return {
244
- get mounted() {
245
- return mounted;
246
- },
247
- wrapper,
248
- mount,
249
- remove
250
- };
251
- }
252
- function createIframeUi(ctx, options) {
253
- const wrapper = document.createElement("div");
254
- wrapper.setAttribute("data-wxt-iframe", "");
255
- const iframe = document.createElement("iframe");
256
- iframe.src = browser.runtime.getURL(options.page);
257
- wrapper.appendChild(iframe);
258
- let mounted = void 0;
259
- const mount = () => {
260
- applyPosition(wrapper, iframe, options);
261
- mountUi(wrapper, options);
262
- mounted = options.onMount?.(wrapper, iframe);
263
- };
264
- const remove = () => {
265
- options.onRemove?.(mounted);
266
- wrapper.remove();
267
- mounted = void 0;
268
- };
269
- ctx.onInvalidated(remove);
270
- return {
271
- get mounted() {
272
- return mounted;
273
- },
274
- iframe,
275
- wrapper,
276
- mount,
277
- remove
278
- };
279
- }
280
- async function createShadowRootUi(ctx, options) {
281
- const css = [options.css ?? ""];
282
- if (ctx.options?.cssInjectionMode === "ui") {
283
- const entryCss = await loadCss();
284
- css.push(entryCss.replaceAll(":root", ":host"));
285
- }
286
- const {
287
- isolatedElement: uiContainer,
288
- parentElement: shadowHost,
289
- shadow
290
- } = await createIsolatedElement({
291
- name: options.name,
292
- css: {
293
- textContent: css.join("\n").trim()
294
- },
295
- mode: options.mode ?? "open",
296
- isolateEvents: options.isolateEvents
297
- });
298
- shadowHost.setAttribute("data-wxt-shadow-root", "");
299
- let mounted;
300
- const mount = () => {
301
- mountUi(shadowHost, options);
302
- applyPosition(shadowHost, shadow.querySelector("html"), options);
303
- mounted = options.onMount(uiContainer, shadow, shadowHost);
304
- };
305
- const remove = () => {
306
- options.onRemove?.(mounted);
307
- shadowHost.remove();
308
- while (uiContainer.lastChild)
309
- uiContainer.removeChild(uiContainer.lastChild);
310
- mounted = void 0;
311
- };
312
- ctx.onInvalidated(remove);
313
- return {
314
- shadow,
315
- shadowHost,
316
- uiContainer,
317
- mount,
318
- remove,
319
- get mounted() {
320
- return mounted;
321
- }
322
- };
323
- }
324
- function applyPosition(root, positionedElement, options) {
325
- if (options.position === "inline") return;
326
- if (options.zIndex != null) root.style.zIndex = String(options.zIndex);
327
- root.style.overflow = "visible";
328
- root.style.position = "relative";
329
- root.style.width = "0";
330
- root.style.height = "0";
331
- root.style.display = "block";
332
- if (positionedElement) {
333
- if (options.position === "overlay") {
334
- positionedElement.style.position = "absolute";
335
- if (options.alignment?.startsWith("bottom-"))
336
- positionedElement.style.bottom = "0";
337
- else positionedElement.style.top = "0";
338
- if (options.alignment?.endsWith("-right"))
339
- positionedElement.style.right = "0";
340
- else positionedElement.style.left = "0";
341
- } else {
342
- positionedElement.style.position = "fixed";
343
- positionedElement.style.top = "0";
344
- positionedElement.style.bottom = "0";
345
- positionedElement.style.left = "0";
346
- positionedElement.style.right = "0";
347
- }
348
- }
349
- }
350
- function getAnchor(options) {
351
- if (options.anchor == null) return document.body;
352
- let resolved = typeof options.anchor === "function" ? options.anchor() : options.anchor;
353
- if (typeof resolved === "string") {
354
- if (resolved.startsWith("/")) {
355
- const result = document.evaluate(
356
- resolved,
357
- document,
358
- null,
359
- XPathResult.FIRST_ORDERED_NODE_TYPE,
360
- null
361
- );
362
- return result.singleNodeValue ?? void 0;
363
- } else {
364
- return document.querySelector(resolved) ?? void 0;
365
- }
366
- }
367
- return resolved ?? void 0;
368
- }
369
- function mountUi(root, options) {
370
- const anchor = getAnchor(options);
371
- if (anchor == null)
372
- throw Error(
373
- "Failed to mount content script UI: could not find anchor element"
374
- );
375
- switch (options.append) {
376
- case void 0:
377
- case "last":
378
- anchor.append(root);
379
- break;
380
- case "first":
381
- anchor.prepend(root);
382
- break;
383
- case "replace":
384
- anchor.replaceWith(root);
385
- break;
386
- case "after":
387
- anchor.parentElement?.insertBefore(root, anchor.nextElementSibling);
388
- break;
389
- case "before":
390
- anchor.parentElement?.insertBefore(root, anchor);
391
- break;
392
- default:
393
- options.append(anchor, root);
394
- break;
395
- }
396
- }
397
- async function loadCss() {
398
- const url = browser.runtime.getURL(
399
- `/content-scripts/${import.meta.env.ENTRYPOINT}.css`
400
- );
401
- try {
402
- const res = await fetch(url);
403
- return await res.text();
404
- } catch (err) {
405
- logger.warn(
406
- `Failed to load styles @ ${url}. Did you forget to import the stylesheet in your entrypoint?`,
407
- err
408
- );
409
- return "";
410
- }
411
- }
412
-
413
- // src/client/app-config.ts
414
- import appConfig from "virtual:app-config";
415
- function useAppConfig() {
416
- return appConfig;
417
- }
418
- export {
419
- ContentScriptContext,
420
- createIframeUi,
421
- createIntegratedUi,
422
- createShadowRootUi,
423
- useAppConfig
424
- };