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.
- package/bin/wxt.mjs +1 -1
- package/dist/browser/chrome.d.ts +17 -0
- package/dist/browser/chrome.mjs +7 -0
- package/dist/browser/index.d.ts +18 -0
- package/dist/browser/index.mjs +2 -0
- package/dist/builtin-modules/index.d.ts +2 -0
- package/dist/builtin-modules/index.mjs +2 -0
- package/dist/builtin-modules/unimport.d.ts +8 -0
- package/dist/builtin-modules/unimport.mjs +99 -0
- package/dist/cli/cli-utils.d.ts +25 -0
- package/dist/cli/cli-utils.mjs +58 -0
- package/dist/cli/commands.d.ts +2 -0
- package/dist/cli/commands.mjs +104 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.mjs +11 -0
- package/dist/client/app-config.d.ts +2 -0
- package/dist/client/app-config.mjs +4 -0
- package/dist/client/content-scripts/content-script-context.d.ts +114 -0
- package/dist/client/content-scripts/content-script-context.mjs +169 -0
- package/dist/client/content-scripts/custom-events.d.ts +10 -0
- package/dist/client/content-scripts/custom-events.mjs +13 -0
- package/dist/client/content-scripts/index.d.ts +2 -0
- package/dist/client/content-scripts/index.mjs +2 -0
- package/dist/client/content-scripts/location-watcher.d.ts +12 -0
- package/dist/client/content-scripts/location-watcher.mjs +22 -0
- package/dist/client/content-scripts/ui/index.d.ts +23 -0
- package/dist/client/content-scripts/ui/index.mjs +188 -0
- package/dist/{client.d.ts → client/content-scripts/ui/types.d.ts} +17 -45
- package/dist/client/content-scripts/ui/types.mjs +0 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.mjs +2 -0
- package/dist/core/build.d.ts +15 -0
- package/dist/core/build.mjs +6 -0
- package/dist/core/builders/vite/index.d.ts +3 -0
- package/dist/core/builders/vite/index.mjs +285 -0
- package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +7 -0
- package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +15 -0
- package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +13 -0
- package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +22 -0
- package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +14 -0
- package/dist/core/builders/vite/plugins/defineImportMeta.mjs +13 -0
- package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +7 -0
- package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +140 -0
- package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +6 -0
- package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -0
- package/dist/core/builders/vite/plugins/download.d.ts +10 -0
- package/dist/core/builders/vite/plugins/download.mjs +14 -0
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +6 -0
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +16 -0
- package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +11 -0
- package/dist/core/builders/vite/plugins/extensionApiMock.mjs +26 -0
- package/dist/core/builders/vite/plugins/globals.d.ts +3 -0
- package/dist/core/builders/vite/plugins/globals.mjs +15 -0
- package/dist/core/builders/vite/plugins/index.d.ts +17 -0
- package/dist/core/builders/vite/plugins/index.mjs +17 -0
- package/dist/core/builders/vite/plugins/multipageMove.d.ts +20 -0
- package/dist/core/builders/vite/plugins/multipageMove.mjs +59 -0
- package/dist/core/builders/vite/plugins/noopBackground.d.ts +6 -0
- package/dist/core/builders/vite/plugins/noopBackground.mjs +17 -0
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +6 -0
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +12 -0
- package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +6 -0
- package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +26 -0
- package/dist/core/builders/vite/plugins/resolveExtensionApi.d.ts +10 -0
- package/dist/core/builders/vite/plugins/resolveExtensionApi.mjs +15 -0
- package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +6 -0
- package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +30 -0
- package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +3 -0
- package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +12 -0
- package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +6 -0
- package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +56 -0
- package/dist/core/clean.d.ts +21 -0
- package/dist/core/clean.mjs +38 -0
- package/dist/core/create-server.d.ts +11 -0
- package/dist/core/create-server.mjs +206 -0
- package/dist/core/define-config.d.ts +2 -0
- package/dist/core/define-config.mjs +3 -0
- package/dist/core/define-runner-config.d.ts +2 -0
- package/dist/core/define-runner-config.mjs +3 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.mjs +8 -0
- package/dist/core/initialize.d.ts +5 -0
- package/dist/core/initialize.mjs +128 -0
- package/dist/core/package-managers/bun.d.ts +2 -0
- package/dist/core/package-managers/bun.mjs +19 -0
- package/dist/core/package-managers/index.d.ts +2 -0
- package/dist/core/package-managers/index.mjs +65 -0
- package/dist/core/package-managers/npm.d.ts +17 -0
- package/dist/core/package-managers/npm.mjs +58 -0
- package/dist/core/package-managers/pnpm.d.ts +2 -0
- package/dist/core/package-managers/pnpm.mjs +21 -0
- package/dist/core/package-managers/types.d.ts +2 -0
- package/dist/core/package-managers/types.mjs +0 -0
- package/dist/core/package-managers/yarn.d.ts +2 -0
- package/dist/core/package-managers/yarn.mjs +31 -0
- package/dist/core/prepare.d.ts +2 -0
- package/dist/core/prepare.mjs +8 -0
- package/dist/core/runners/index.d.ts +2 -0
- package/dist/core/runners/index.mjs +12 -0
- package/dist/core/runners/manual.d.ts +5 -0
- package/dist/core/runners/manual.mjs +16 -0
- package/dist/core/runners/safari.d.ts +5 -0
- package/dist/core/runners/safari.mjs +16 -0
- package/dist/core/runners/web-ext.d.ts +5 -0
- package/dist/core/runners/web-ext.mjs +78 -0
- package/dist/core/runners/wsl.d.ts +5 -0
- package/dist/core/runners/wsl.mjs +16 -0
- package/dist/core/utils/arrays.d.ts +13 -0
- package/dist/{chunk-BERPNPEZ.js → core/utils/arrays.mjs} +6 -10
- package/dist/core/utils/building/build-entrypoints.d.ts +3 -0
- package/dist/core/utils/building/build-entrypoints.mjs +47 -0
- package/dist/core/utils/building/detect-dev-changes.d.ts +57 -0
- package/dist/core/utils/building/detect-dev-changes.mjs +93 -0
- package/dist/core/utils/building/find-entrypoints.d.ts +5 -0
- package/dist/core/utils/building/find-entrypoints.mjs +385 -0
- package/dist/core/utils/building/generate-wxt-dir.d.ts +5 -0
- package/dist/core/utils/building/generate-wxt-dir.mjs +192 -0
- package/dist/core/utils/building/group-entrypoints.d.ts +8 -0
- package/dist/core/utils/building/group-entrypoints.mjs +37 -0
- package/dist/core/utils/building/import-entrypoint.d.ts +16 -0
- package/dist/core/utils/building/import-entrypoint.mjs +97 -0
- package/dist/core/utils/building/index.d.ts +9 -0
- package/dist/core/utils/building/index.mjs +9 -0
- package/dist/core/utils/building/internal-build.d.ts +12 -0
- package/dist/core/utils/building/internal-build.mjs +112 -0
- package/dist/core/utils/building/rebuild.d.ts +23 -0
- package/dist/core/utils/building/rebuild.mjs +39 -0
- package/dist/core/utils/building/resolve-config.d.ts +11 -0
- package/dist/core/utils/building/resolve-config.mjs +364 -0
- package/dist/core/utils/cache.d.ts +8 -0
- package/dist/core/utils/cache.mjs +21 -0
- package/dist/core/utils/cli.d.ts +3 -0
- package/dist/core/utils/cli.mjs +26 -0
- package/dist/core/utils/constants.d.ts +5 -0
- package/dist/core/utils/constants.mjs +1 -0
- package/dist/core/utils/content-scripts.d.ts +11 -0
- package/dist/core/utils/content-scripts.mjs +60 -0
- package/dist/core/utils/content-security-policy.d.ts +14 -0
- package/dist/core/utils/content-security-policy.mjs +39 -0
- package/dist/core/utils/entrypoints.d.ts +25 -0
- package/dist/core/utils/entrypoints.mjs +31 -0
- package/dist/core/utils/eslint.d.ts +1 -0
- package/dist/core/utils/eslint.mjs +11 -0
- package/dist/core/utils/fs.d.ts +13 -0
- package/dist/core/utils/fs.mjs +15 -0
- package/dist/core/utils/globals.d.ts +11 -0
- package/dist/core/utils/globals.mjs +53 -0
- package/dist/core/utils/i18n.d.ts +11 -0
- package/dist/core/utils/i18n.mjs +35 -0
- package/dist/core/utils/log/index.d.ts +4 -0
- package/dist/core/utils/log/index.mjs +4 -0
- package/dist/core/utils/log/printBuildSummary.d.ts +2 -0
- package/dist/core/utils/log/printBuildSummary.mjs +32 -0
- package/dist/core/utils/log/printFileList.d.ts +1 -0
- package/dist/core/utils/log/printFileList.mjs +42 -0
- package/dist/core/utils/log/printHeader.d.ts +1 -0
- package/dist/core/utils/log/printHeader.mjs +7 -0
- package/dist/core/utils/log/printTable.d.ts +1 -0
- package/dist/core/utils/log/printTable.mjs +22 -0
- package/dist/core/utils/manifest.d.ts +44 -0
- package/dist/core/utils/manifest.mjs +512 -0
- package/dist/core/utils/network.d.ts +7 -0
- package/dist/core/utils/network.mjs +38 -0
- package/dist/core/utils/package.d.ts +6 -0
- package/dist/core/utils/package.mjs +14 -0
- package/dist/core/utils/paths.d.ts +11 -0
- package/dist/core/utils/paths.mjs +10 -0
- package/dist/core/utils/strings.d.ts +14 -0
- package/dist/core/utils/strings.mjs +18 -0
- package/dist/core/utils/testing/fake-objects.d.ts +4556 -0
- package/dist/core/utils/testing/fake-objects.mjs +322 -0
- package/dist/core/utils/time.d.ts +9 -0
- package/dist/core/utils/time.mjs +17 -0
- package/dist/core/utils/transform.d.ts +9 -0
- package/dist/core/utils/transform.mjs +17 -0
- package/dist/core/utils/types.d.ts +10 -0
- package/dist/core/utils/types.mjs +0 -0
- package/dist/core/utils/validation.d.ts +15 -0
- package/dist/core/utils/validation.mjs +55 -0
- package/dist/core/utils/virtual-modules.d.ts +22 -0
- package/dist/core/utils/virtual-modules.mjs +14 -0
- package/dist/core/utils/wsl.d.ts +4 -0
- package/dist/core/utils/wsl.mjs +4 -0
- package/dist/core/wxt.d.ts +19 -0
- package/dist/core/wxt.mjs +41 -0
- package/dist/core/zip.d.ts +7 -0
- package/dist/core/zip.mjs +137 -0
- package/dist/index.d.ts +4 -79
- package/dist/index.mjs +3 -0
- package/dist/modules.d.ts +10 -20
- package/dist/{chunk-6XSIWUWF.js → modules.mjs} +7 -16
- package/dist/sandbox/define-app-config.d.ts +19 -0
- package/dist/sandbox/define-app-config.mjs +3 -0
- package/dist/sandbox/define-background.d.ts +3 -0
- package/dist/sandbox/define-background.mjs +4 -0
- package/dist/sandbox/define-content-script.d.ts +2 -0
- package/dist/sandbox/define-content-script.mjs +3 -0
- package/dist/sandbox/define-unlisted-script.d.ts +3 -0
- package/dist/sandbox/define-unlisted-script.mjs +4 -0
- package/dist/sandbox/define-wxt-plugin.d.ts +2 -0
- package/dist/sandbox/define-wxt-plugin.mjs +3 -0
- package/dist/sandbox/dev-server-websocket.d.ts +21 -0
- package/dist/sandbox/dev-server-websocket.mjs +37 -0
- package/dist/sandbox/index.d.ts +11 -0
- package/dist/sandbox/index.mjs +6 -0
- package/dist/sandbox/utils/logger.d.ts +9 -0
- package/dist/sandbox/utils/logger.mjs +15 -0
- package/dist/storage.d.ts +39 -16
- package/dist/{storage.js → storage.mjs} +30 -41
- package/dist/testing/fake-browser.d.ts +1 -0
- package/dist/testing/fake-browser.mjs +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.mjs +2 -0
- package/dist/{testing.d.ts → testing/wxt-vitest-plugin.d.ts} +3 -15
- package/dist/testing/wxt-vitest-plugin.mjs +26 -0
- package/dist/{index-nWRfwAJi.d.cts → types.d.ts} +150 -264
- package/dist/types.mjs +0 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.mjs +1 -0
- package/dist/virtual/{background-entrypoint.js → background-entrypoint.mjs} +31 -40
- package/dist/virtual/{content-script-isolated-world-entrypoint.js → content-script-isolated-world-entrypoint.mjs} +9 -13
- package/dist/virtual/{content-script-main-world-entrypoint.js → content-script-main-world-entrypoint.mjs} +9 -14
- package/dist/virtual/mock-browser.mjs +6 -0
- package/dist/virtual/{reload-html.js → reload-html.mjs} +8 -9
- package/dist/virtual/{unlisted-script-entrypoint.js → unlisted-script-entrypoint.mjs} +8 -12
- package/package.json +47 -57
- package/dist/browser.d.ts +0 -18
- package/dist/browser.js +0 -6
- package/dist/chunk-BM6QYGAW.js +0 -1063
- package/dist/chunk-FNTE2L27.js +0 -7
- package/dist/chunk-FP7RYLVL.js +0 -3617
- package/dist/chunk-KPD5J7PZ.js +0 -1065
- package/dist/chunk-QGM4M3NI.js +0 -37
- package/dist/chunk-SGKCDMVR.js +0 -38
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -4438
- package/dist/client.js +0 -424
- package/dist/define-app-config-bg54F_lV.d.ts +0 -294
- package/dist/execa-4UBDUBJZ.js +0 -7244
- package/dist/execa-QLUM2B3W.js +0 -7245
- package/dist/index-nWRfwAJi.d.ts +0 -1401
- package/dist/index.cjs +0 -14473
- package/dist/index.d.cts +0 -81
- package/dist/index.js +0 -696
- package/dist/modules.cjs +0 -96
- package/dist/modules.d.cts +0 -119
- package/dist/modules.js +0 -17
- package/dist/prompt-25QIVJDC.js +0 -755
- package/dist/prompt-7BMKNSWS.js +0 -754
- package/dist/sandbox.d.ts +0 -16
- package/dist/sandbox.js +0 -36
- package/dist/storage.cjs +0 -439
- package/dist/storage.d.cts +0 -200
- package/dist/testing.cjs +0 -2815
- package/dist/testing.d.cts +0 -30
- package/dist/testing.js +0 -40
- 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
|
-
};
|