wxt 0.20.13 → 0.20.15
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/dist/browser.d.mts +20 -0
- package/dist/browser.mjs +18 -3
- package/dist/builtin-modules/index.mjs +7 -2
- package/dist/builtin-modules/unimport.mjs +69 -75
- package/dist/cli/cli-utils.mjs +54 -50
- package/dist/cli/commands.mjs +78 -103
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +9 -4
- package/dist/core/{build.d.ts → build.d.mts} +6 -2
- package/dist/core/build.mjs +23 -4
- package/dist/core/builders/vite/index.mjs +293 -343
- package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +11 -12
- package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +27 -20
- package/dist/core/builders/vite/plugins/defineImportMeta.mjs +17 -12
- package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +105 -136
- package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -14
- package/dist/core/builders/vite/plugins/download.mjs +23 -12
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +18 -14
- package/dist/core/builders/vite/plugins/extensionApiMock.mjs +31 -33
- package/dist/core/builders/vite/plugins/globals.mjs +14 -13
- package/dist/core/builders/vite/plugins/iifeFooter.mjs +17 -11
- package/dist/core/builders/vite/plugins/index.mjs +18 -16
- package/dist/core/builders/vite/plugins/noopBackground.mjs +21 -15
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +26 -23
- package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +26 -24
- package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +28 -26
- package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +11 -11
- package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +42 -47
- package/dist/core/{clean.d.ts → clean.d.mts} +7 -3
- package/dist/core/clean.mjs +38 -35
- package/dist/core/{create-server.d.ts → create-server.d.mts} +6 -2
- package/dist/core/create-server.mjs +236 -247
- package/dist/core/define-config.d.mts +6 -0
- package/dist/core/define-config.mjs +6 -2
- package/dist/core/define-web-ext-config.d.mts +13 -0
- package/dist/core/define-web-ext-config.mjs +16 -7
- package/dist/core/generate-wxt-dir.mjs +110 -148
- package/dist/core/index.d.mts +9 -0
- package/dist/core/index.mjs +12 -8
- package/dist/core/initialize.d.mts +8 -0
- package/dist/core/initialize.mjs +110 -124
- package/dist/core/keyboard-shortcuts.mjs +30 -28
- package/dist/core/package-managers/bun.mjs +18 -16
- package/dist/core/package-managers/deno.mjs +12 -8
- package/dist/core/package-managers/index.mjs +52 -60
- package/dist/core/package-managers/npm.mjs +51 -52
- package/dist/core/package-managers/pnpm.mjs +21 -18
- package/dist/core/package-managers/yarn.mjs +31 -28
- package/dist/core/prepare.d.mts +6 -0
- package/dist/core/prepare.mjs +12 -7
- package/dist/core/resolve-config.mjs +393 -462
- package/dist/core/runners/index.mjs +15 -10
- package/dist/core/runners/manual.mjs +13 -14
- package/dist/core/runners/safari.mjs +13 -14
- package/dist/core/runners/web-ext.mjs +62 -83
- package/dist/core/runners/wsl.mjs +13 -14
- package/dist/core/utils/arrays.mjs +23 -12
- package/dist/core/utils/building/build-entrypoints.mjs +44 -40
- package/dist/core/utils/building/detect-dev-changes.mjs +84 -105
- package/dist/core/utils/building/find-entrypoints.mjs +278 -359
- package/dist/core/utils/building/group-entrypoints.mjs +42 -35
- package/dist/core/utils/building/index.mjs +8 -6
- package/dist/core/utils/building/internal-build.mjs +86 -102
- package/dist/core/utils/building/rebuild.mjs +50 -30
- package/dist/core/utils/cache.mjs +29 -18
- package/dist/core/utils/constants.mjs +9 -1
- package/dist/core/utils/content-scripts.mjs +56 -54
- package/dist/core/utils/content-security-policy.mjs +36 -39
- package/dist/core/utils/entrypoints.mjs +51 -28
- package/dist/core/utils/env.mjs +22 -17
- package/dist/core/utils/environments/browser-environment.mjs +13 -12
- package/dist/core/utils/environments/environment.mjs +36 -33
- package/dist/core/utils/environments/extension-environment.mjs +15 -10
- package/dist/core/utils/environments/index.mjs +4 -2
- package/dist/core/utils/eslint.mjs +11 -10
- package/dist/core/utils/fs.mjs +23 -11
- package/dist/core/utils/globals.mjs +53 -51
- package/dist/core/utils/i18n.mjs +41 -32
- package/dist/core/utils/index.mjs +3 -0
- package/dist/core/utils/log/index.mjs +6 -4
- package/dist/core/utils/log/printBuildSummary.mjs +20 -25
- package/dist/core/utils/log/printFileList.mjs +30 -33
- package/dist/core/utils/log/printHeader.mjs +9 -5
- package/dist/core/utils/log/printTable.mjs +19 -21
- package/dist/core/utils/manifest.mjs +366 -479
- package/dist/core/utils/minimatch-multiple.mjs +26 -15
- package/dist/core/utils/network.mjs +27 -33
- package/dist/core/utils/number.mjs +7 -3
- package/dist/core/utils/package.mjs +20 -13
- package/dist/core/utils/paths.d.mts +8 -0
- package/dist/core/utils/paths.mjs +26 -7
- package/dist/core/utils/strings.mjs +19 -14
- package/dist/core/utils/syntax-errors.mjs +14 -16
- package/dist/core/utils/time.mjs +19 -15
- package/dist/core/utils/transform.mjs +129 -160
- package/dist/core/utils/types.d.mts +6 -0
- package/dist/core/utils/validation.mjs +43 -50
- package/dist/core/utils/virtual-modules.mjs +20 -12
- package/dist/core/utils/wsl.mjs +11 -3
- package/dist/core/wxt.mjs +65 -60
- package/dist/core/{zip.d.ts → zip.d.mts} +6 -2
- package/dist/core/zip.mjs +118 -140
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +13 -3
- package/dist/{modules.d.ts → modules.d.mts} +14 -16
- package/dist/modules.mjs +179 -51
- package/dist/testing/fake-browser.d.mts +2 -0
- package/dist/testing/fake-browser.mjs +3 -1
- package/dist/testing/index.d.mts +3 -0
- package/dist/testing/index.mjs +4 -2
- package/dist/testing/{wxt-vitest-plugin.d.ts → wxt-vitest-plugin.d.mts} +7 -9
- package/dist/testing/wxt-vitest-plugin.mjs +38 -19
- package/dist/types.d.mts +1475 -0
- package/dist/utils/app-config.d.mts +6 -0
- package/dist/utils/app-config.mjs +8 -2
- package/dist/utils/content-script-context.d.mts +134 -0
- package/dist/utils/content-script-context.mjs +187 -175
- package/dist/utils/content-script-ui/iframe.d.mts +42 -0
- package/dist/utils/content-script-ui/iframe.mjs +42 -27
- package/dist/utils/content-script-ui/integrated.d.mts +37 -0
- package/dist/utils/content-script-ui/integrated.mjs +36 -29
- package/dist/utils/content-script-ui/shadow-root.d.mts +80 -0
- package/dist/utils/content-script-ui/shadow-root.mjs +75 -82
- package/dist/utils/content-script-ui/shared.mjs +115 -150
- package/dist/utils/content-script-ui/types.d.mts +107 -0
- package/dist/utils/content-script-ui/types.mjs +1 -0
- package/dist/utils/{define-app-config.d.ts → define-app-config.d.mts} +5 -3
- package/dist/utils/define-app-config.mjs +22 -2
- package/dist/utils/define-background.d.mts +7 -0
- package/dist/utils/define-background.mjs +7 -3
- package/dist/utils/define-content-script.d.mts +6 -0
- package/dist/utils/define-content-script.mjs +6 -2
- package/dist/utils/define-unlisted-script.d.mts +7 -0
- package/dist/utils/define-unlisted-script.mjs +7 -3
- package/dist/utils/define-wxt-plugin.d.mts +6 -0
- package/dist/utils/define-wxt-plugin.mjs +6 -2
- package/dist/utils/inject-script.d.mts +41 -0
- package/dist/utils/inject-script.mjs +44 -34
- package/dist/utils/internal/custom-events.d.mts +9 -0
- package/dist/utils/internal/custom-events.mjs +18 -10
- package/dist/utils/internal/location-watcher.mjs +23 -20
- package/dist/utils/internal/logger.mjs +15 -12
- package/dist/utils/match-patterns.d.mts +1 -0
- package/dist/utils/match-patterns.mjs +3 -1
- package/dist/utils/{split-shadow-root-css.d.ts → split-shadow-root-css.d.mts} +6 -3
- package/dist/utils/split-shadow-root-css.mjs +16 -7
- package/dist/utils/storage.d.mts +1 -0
- package/dist/utils/storage.mjs +3 -1
- package/dist/version.d.mts +4 -0
- package/dist/version.mjs +5 -1
- package/dist/virtual/background-entrypoint.d.mts +4 -0
- package/dist/virtual/background-entrypoint.mjs +143 -161
- package/dist/virtual/content-script-isolated-world-entrypoint.d.mts +4 -0
- package/dist/virtual/content-script-isolated-world-entrypoint.mjs +26 -27
- package/dist/virtual/content-script-main-world-entrypoint.d.mts +4 -0
- package/dist/virtual/content-script-main-world-entrypoint.mjs +24 -24
- package/dist/virtual/mock-browser.d.mts +6 -0
- package/dist/virtual/mock-browser.mjs +5 -3
- package/dist/virtual/reload-html.d.mts +1 -0
- package/dist/virtual/reload-html.mjs +57 -53
- package/dist/virtual/unlisted-script-entrypoint.d.mts +4 -0
- package/dist/virtual/unlisted-script-entrypoint.mjs +35 -43
- package/package.json +56 -50
- package/dist/browser.d.ts +0 -30
- package/dist/builtin-modules/index.d.ts +0 -2
- package/dist/builtin-modules/unimport.d.ts +0 -5
- package/dist/cli/cli-utils.d.ts +0 -25
- package/dist/cli/commands.d.ts +0 -2
- package/dist/cli/index.d.ts +0 -1
- package/dist/core/builders/vite/index.d.ts +0 -7
- package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +0 -7
- package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +0 -13
- package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +0 -14
- package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +0 -7
- package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +0 -6
- package/dist/core/builders/vite/plugins/download.d.ts +0 -10
- package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +0 -6
- package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +0 -6
- package/dist/core/builders/vite/plugins/globals.d.ts +0 -3
- package/dist/core/builders/vite/plugins/iifeFooter.d.ts +0 -8
- package/dist/core/builders/vite/plugins/index.d.ts +0 -16
- package/dist/core/builders/vite/plugins/noopBackground.d.ts +0 -6
- package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +0 -6
- package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +0 -6
- package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +0 -6
- package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +0 -3
- package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +0 -6
- package/dist/core/define-config.d.ts +0 -2
- package/dist/core/define-web-ext-config.d.ts +0 -9
- package/dist/core/generate-wxt-dir.d.ts +0 -5
- package/dist/core/index.d.ts +0 -8
- package/dist/core/initialize.d.ts +0 -5
- package/dist/core/keyboard-shortcuts.d.ts +0 -12
- package/dist/core/package-managers/bun.d.ts +0 -2
- package/dist/core/package-managers/deno.d.ts +0 -2
- package/dist/core/package-managers/index.d.ts +0 -2
- package/dist/core/package-managers/npm.d.ts +0 -17
- package/dist/core/package-managers/pnpm.d.ts +0 -2
- package/dist/core/package-managers/types.d.ts +0 -2
- package/dist/core/package-managers/types.mjs +0 -0
- package/dist/core/package-managers/yarn.d.ts +0 -2
- package/dist/core/prepare.d.ts +0 -2
- package/dist/core/resolve-config.d.ts +0 -11
- package/dist/core/runners/index.d.ts +0 -2
- package/dist/core/runners/manual.d.ts +0 -5
- package/dist/core/runners/safari.d.ts +0 -5
- package/dist/core/runners/web-ext.d.ts +0 -5
- package/dist/core/runners/wsl.d.ts +0 -5
- package/dist/core/utils/arrays.d.ts +0 -13
- package/dist/core/utils/building/build-entrypoints.d.ts +0 -3
- package/dist/core/utils/building/detect-dev-changes.d.ts +0 -61
- package/dist/core/utils/building/find-entrypoints.d.ts +0 -5
- package/dist/core/utils/building/group-entrypoints.d.ts +0 -8
- package/dist/core/utils/building/index.d.ts +0 -6
- package/dist/core/utils/building/internal-build.d.ts +0 -12
- package/dist/core/utils/building/rebuild.d.ts +0 -23
- package/dist/core/utils/cache.d.ts +0 -8
- package/dist/core/utils/cli.d.ts +0 -3
- package/dist/core/utils/cli.mjs +0 -26
- package/dist/core/utils/constants.d.ts +0 -5
- package/dist/core/utils/content-scripts.d.ts +0 -12
- package/dist/core/utils/content-security-policy.d.ts +0 -14
- package/dist/core/utils/entrypoints.d.ts +0 -31
- package/dist/core/utils/env.d.ts +0 -5
- package/dist/core/utils/environments/browser-environment.d.ts +0 -3
- package/dist/core/utils/environments/environment.d.ts +0 -8
- package/dist/core/utils/environments/extension-environment.d.ts +0 -6
- package/dist/core/utils/environments/index.d.ts +0 -2
- package/dist/core/utils/eslint.d.ts +0 -1
- package/dist/core/utils/fs.d.ts +0 -13
- package/dist/core/utils/globals.d.ts +0 -11
- package/dist/core/utils/i18n.d.ts +0 -11
- package/dist/core/utils/log/index.d.ts +0 -4
- package/dist/core/utils/log/printBuildSummary.d.ts +0 -2
- package/dist/core/utils/log/printFileList.d.ts +0 -1
- package/dist/core/utils/log/printHeader.d.ts +0 -1
- package/dist/core/utils/log/printTable.d.ts +0 -1
- package/dist/core/utils/manifest.d.ts +0 -40
- package/dist/core/utils/minimatch-multiple.d.ts +0 -15
- package/dist/core/utils/network.d.ts +0 -7
- package/dist/core/utils/number.d.ts +0 -1
- package/dist/core/utils/package.d.ts +0 -6
- package/dist/core/utils/paths.d.ts +0 -11
- package/dist/core/utils/strings.d.ts +0 -14
- package/dist/core/utils/syntax-errors.d.ts +0 -11
- package/dist/core/utils/testing/fake-objects.d.ts +0 -11665
- package/dist/core/utils/testing/fake-objects.mjs +0 -326
- package/dist/core/utils/time.d.ts +0 -9
- package/dist/core/utils/transform.d.ts +0 -11
- package/dist/core/utils/types.d.ts +0 -13
- package/dist/core/utils/types.mjs +0 -0
- package/dist/core/utils/validation.d.ts +0 -15
- package/dist/core/utils/virtual-modules.d.ts +0 -22
- package/dist/core/utils/wsl.d.ts +0 -4
- package/dist/core/wxt.d.ts +0 -24
- package/dist/index.d.ts +0 -11
- package/dist/testing/fake-browser.d.ts +0 -10
- package/dist/testing/index.d.ts +0 -10
- package/dist/types.d.ts +0 -1459
- package/dist/types.mjs +0 -0
- package/dist/utils/app-config.d.ts +0 -2
- package/dist/utils/content-script-context.d.ts +0 -134
- package/dist/utils/content-script-ui/iframe.d.ts +0 -32
- package/dist/utils/content-script-ui/integrated.d.ts +0 -34
- package/dist/utils/content-script-ui/shadow-root.d.ts +0 -76
- package/dist/utils/content-script-ui/shared.d.ts +0 -5
- package/dist/utils/content-script-ui/types.d.ts +0 -104
- package/dist/utils/define-background.d.ts +0 -4
- package/dist/utils/define-content-script.d.ts +0 -3
- package/dist/utils/define-unlisted-script.d.ts +0 -4
- package/dist/utils/define-wxt-plugin.d.ts +0 -3
- package/dist/utils/inject-script.d.ts +0 -36
- package/dist/utils/internal/custom-events.d.ts +0 -10
- package/dist/utils/internal/dev-server-websocket.d.ts +0 -21
- package/dist/utils/internal/dev-server-websocket.mjs +0 -37
- package/dist/utils/internal/location-watcher.d.ts +0 -12
- package/dist/utils/internal/logger.d.ts +0 -9
- package/dist/utils/match-patterns.d.ts +0 -5
- package/dist/utils/storage.d.ts +0 -5
- package/dist/version.d.ts +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ContentScriptContext } from "../content-script-context.mjs";
|
|
2
|
+
import { ContentScriptUi, ContentScriptUiOptions } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/content-script-ui/integrated.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a content script UI without any isolation.
|
|
7
|
+
*
|
|
8
|
+
* @see https://wxt.dev/guide/essentials/content-scripts.html#integrated
|
|
9
|
+
*/
|
|
10
|
+
declare function createIntegratedUi<TMounted>(ctx: ContentScriptContext, options: IntegratedContentScriptUiOptions<TMounted>): IntegratedContentScriptUi<TMounted>;
|
|
11
|
+
/**
|
|
12
|
+
* Shared types for the different `wxt/utils/content-script-ui/*` modules.
|
|
13
|
+
* @module wxt/utils/content-script-ui/types
|
|
14
|
+
*/
|
|
15
|
+
interface IntegratedContentScriptUi<TMounted> extends ContentScriptUi<TMounted> {
|
|
16
|
+
/**
|
|
17
|
+
* A wrapper div that assists in positioning.
|
|
18
|
+
*/
|
|
19
|
+
wrapper: HTMLElement;
|
|
20
|
+
}
|
|
21
|
+
type IntegratedContentScriptUiOptions<TMounted> = ContentScriptUiOptions<TMounted> & {
|
|
22
|
+
/**
|
|
23
|
+
* Tag used to create the wrapper element.
|
|
24
|
+
*
|
|
25
|
+
* @default "div"
|
|
26
|
+
*/
|
|
27
|
+
tag?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Callback executed when mounting the UI. This function should create and append the UI to the
|
|
30
|
+
* `wrapper` element. It is called every time `ui.mount()` is called.
|
|
31
|
+
*
|
|
32
|
+
* Optionally return a value that can be accessed at `ui.mounted` or in the `onRemove` callback.
|
|
33
|
+
*/
|
|
34
|
+
onMount: (wrapper: HTMLElement) => TMounted;
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { IntegratedContentScriptUi, IntegratedContentScriptUiOptions, createIntegratedUi };
|
|
@@ -1,31 +1,38 @@
|
|
|
1
1
|
import { applyPosition, createMountFunctions, mountUi } from "./shared.mjs";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/utils/content-script-ui/integrated.ts
|
|
4
|
+
/**
|
|
5
|
+
* Create a content script UI without any isolation.
|
|
6
|
+
*
|
|
7
|
+
* @see https://wxt.dev/guide/essentials/content-scripts.html#integrated
|
|
8
|
+
*/
|
|
9
|
+
function createIntegratedUi(ctx, options) {
|
|
10
|
+
const wrapper = document.createElement(options.tag || "div");
|
|
11
|
+
let mounted;
|
|
12
|
+
const mount = () => {
|
|
13
|
+
applyPosition(wrapper, void 0, options);
|
|
14
|
+
mountUi(wrapper, options);
|
|
15
|
+
mounted = options.onMount?.(wrapper);
|
|
16
|
+
};
|
|
17
|
+
const remove = () => {
|
|
18
|
+
options.onRemove?.(mounted);
|
|
19
|
+
wrapper.replaceChildren();
|
|
20
|
+
wrapper.remove();
|
|
21
|
+
mounted = void 0;
|
|
22
|
+
};
|
|
23
|
+
const mountFunctions = createMountFunctions({
|
|
24
|
+
mount,
|
|
25
|
+
remove
|
|
26
|
+
}, options);
|
|
27
|
+
ctx.onInvalidated(remove);
|
|
28
|
+
return {
|
|
29
|
+
get mounted() {
|
|
30
|
+
return mounted;
|
|
31
|
+
},
|
|
32
|
+
wrapper,
|
|
33
|
+
...mountFunctions
|
|
34
|
+
};
|
|
31
35
|
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { createIntegratedUi };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ContentScriptContext } from "../content-script-context.mjs";
|
|
2
|
+
import { ContentScriptUi, ContentScriptUiOptions } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/content-script-ui/shadow-root.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a content script UI inside a [`ShadowRoot`](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot).
|
|
7
|
+
*
|
|
8
|
+
* > This function is async because it has to load the CSS via a network call.
|
|
9
|
+
*
|
|
10
|
+
* @see https://wxt.dev/guide/essentials/content-scripts.html#shadow-root
|
|
11
|
+
*/
|
|
12
|
+
declare function createShadowRootUi<TMounted>(ctx: ContentScriptContext, options: ShadowRootContentScriptUiOptions<TMounted>): Promise<ShadowRootContentScriptUi<TMounted>>;
|
|
13
|
+
interface ShadowRootContentScriptUi<TMounted> extends ContentScriptUi<TMounted> {
|
|
14
|
+
/**
|
|
15
|
+
* The `HTMLElement` hosting the shadow root used to isolate the UI's styles. This is the element
|
|
16
|
+
* that get's added to the DOM. This element's style is not isolated from the webpage.
|
|
17
|
+
*/
|
|
18
|
+
shadowHost: HTMLElement;
|
|
19
|
+
/**
|
|
20
|
+
* The container element inside the `ShadowRoot` whose styles are isolated. The UI is mounted
|
|
21
|
+
* inside this `HTMLElement`.
|
|
22
|
+
*/
|
|
23
|
+
uiContainer: HTMLElement;
|
|
24
|
+
/**
|
|
25
|
+
* The shadow root performing the isolation.
|
|
26
|
+
*/
|
|
27
|
+
shadow: ShadowRoot;
|
|
28
|
+
}
|
|
29
|
+
type ShadowRootContentScriptUiOptions<TMounted> = ContentScriptUiOptions<TMounted> & {
|
|
30
|
+
/**
|
|
31
|
+
* The name of the custom component used to host the ShadowRoot. Must be kebab-case.
|
|
32
|
+
*/
|
|
33
|
+
name: string;
|
|
34
|
+
/**
|
|
35
|
+
* Custom CSS text to apply to the UI. If your content script imports/generates CSS and you've
|
|
36
|
+
* set `cssInjectionMode: "ui"`, the imported CSS will be included automatically. You do not need
|
|
37
|
+
* to pass those styles in here. This is for any additional styles not in the imported CSS.
|
|
38
|
+
*/
|
|
39
|
+
css?: string;
|
|
40
|
+
/**
|
|
41
|
+
* ShadowRoot's mode.
|
|
42
|
+
*
|
|
43
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/mode
|
|
44
|
+
* @default "open"
|
|
45
|
+
*/
|
|
46
|
+
mode?: 'open' | 'closed';
|
|
47
|
+
/**
|
|
48
|
+
* When enabled, `event.stopPropagation` will be called on events trying to bubble out of the
|
|
49
|
+
* shadow root.
|
|
50
|
+
*
|
|
51
|
+
* - Set to `true` to stop the propagation of a default set of events,
|
|
52
|
+
* `["keyup", "keydown", "keypress"]`
|
|
53
|
+
* - Set to an array of event names to stop the propagation of a custom list of events
|
|
54
|
+
*/
|
|
55
|
+
isolateEvents?: boolean | string[];
|
|
56
|
+
/**
|
|
57
|
+
* By default, WXT adds `all: initial` to the shadow root before the rest of
|
|
58
|
+
* your CSS. This resets any inheritable CSS styles that
|
|
59
|
+
* [normally pierce the Shadow DOM](https://open-wc.org/guides/knowledge/styling/styles-piercing-shadow-dom/).
|
|
60
|
+
*
|
|
61
|
+
* WXT resets everything but:
|
|
62
|
+
* - **`rem` Units**: they continue to scale based off the webpage's HTML `font-size`.
|
|
63
|
+
* - **CSS Variables/Custom Properties**: CSS variables defined outside the shadow root can be accessed inside it.
|
|
64
|
+
* - **`@font-face` Definitions**: Fonts defined outside the shadow root can be used inside it.
|
|
65
|
+
*
|
|
66
|
+
* To disable this behavior and inherit styles from the webpage, set `inheritStyles: true`.
|
|
67
|
+
*
|
|
68
|
+
* @default false
|
|
69
|
+
*/
|
|
70
|
+
inheritStyles?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Callback executed when mounting the UI. This function should create and append the UI to the
|
|
73
|
+
* `uiContainer` element. It is called every time `ui.mount()` is called.
|
|
74
|
+
*
|
|
75
|
+
* Optionally return a value that can be accessed at `ui.mounted` or in the `onRemove` callback.
|
|
76
|
+
*/
|
|
77
|
+
onMount: (uiContainer: HTMLElement, shadow: ShadowRoot, shadowHost: HTMLElement) => TMounted;
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
export { ShadowRootContentScriptUi, ShadowRootContentScriptUiOptions, createShadowRootUi };
|
|
@@ -1,87 +1,80 @@
|
|
|
1
|
-
import { browser } from "wxt/browser";
|
|
2
|
-
import { createIsolatedElement } from "@webext-core/isolated-element";
|
|
3
|
-
import { applyPosition, createMountFunctions, mountUi } from "./shared.mjs";
|
|
4
1
|
import { logger } from "../internal/logger.mjs";
|
|
2
|
+
import { applyPosition, createMountFunctions, mountUi } from "./shared.mjs";
|
|
5
3
|
import { splitShadowRootCss } from "../split-shadow-root-css.mjs";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
shadowHost,
|
|
68
|
-
uiContainer,
|
|
69
|
-
...mountFunctions,
|
|
70
|
-
get mounted() {
|
|
71
|
-
return mounted;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
4
|
+
import { browser } from "wxt/browser";
|
|
5
|
+
import { createIsolatedElement } from "@webext-core/isolated-element";
|
|
6
|
+
|
|
7
|
+
//#region src/utils/content-script-ui/shadow-root.ts
|
|
8
|
+
/** @module wxt/utils/content-script-ui/shadow-root */
|
|
9
|
+
/**
|
|
10
|
+
* Create a content script UI inside a [`ShadowRoot`](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot).
|
|
11
|
+
*
|
|
12
|
+
* > This function is async because it has to load the CSS via a network call.
|
|
13
|
+
*
|
|
14
|
+
* @see https://wxt.dev/guide/essentials/content-scripts.html#shadow-root
|
|
15
|
+
*/
|
|
16
|
+
async function createShadowRootUi(ctx, options) {
|
|
17
|
+
const instanceId = Math.random().toString(36).substring(2, 15);
|
|
18
|
+
const css = [];
|
|
19
|
+
if (!options.inheritStyles) css.push(`/* WXT Shadow Root Reset */ :host{all:initial !important;}`);
|
|
20
|
+
if (options.css) css.push(options.css);
|
|
21
|
+
if (ctx.options?.cssInjectionMode === "ui") {
|
|
22
|
+
const entryCss = await loadCss();
|
|
23
|
+
css.push(entryCss.replaceAll(":root", ":host"));
|
|
24
|
+
}
|
|
25
|
+
const { shadowCss, documentCss } = splitShadowRootCss(css.join("\n").trim());
|
|
26
|
+
const { isolatedElement: uiContainer, parentElement: shadowHost, shadow } = await createIsolatedElement({
|
|
27
|
+
name: options.name,
|
|
28
|
+
css: { textContent: shadowCss },
|
|
29
|
+
mode: options.mode ?? "open",
|
|
30
|
+
isolateEvents: options.isolateEvents
|
|
31
|
+
});
|
|
32
|
+
let mounted;
|
|
33
|
+
const mount = () => {
|
|
34
|
+
mountUi(shadowHost, options);
|
|
35
|
+
applyPosition(shadowHost, shadow.querySelector("html"), options);
|
|
36
|
+
if (documentCss && !document.querySelector(`style[wxt-shadow-root-document-styles="${instanceId}"]`)) {
|
|
37
|
+
const style = document.createElement("style");
|
|
38
|
+
style.textContent = documentCss;
|
|
39
|
+
style.setAttribute("wxt-shadow-root-document-styles", instanceId);
|
|
40
|
+
(document.head ?? document.body).append(style);
|
|
41
|
+
}
|
|
42
|
+
mounted = options.onMount(uiContainer, shadow, shadowHost);
|
|
43
|
+
};
|
|
44
|
+
const remove = () => {
|
|
45
|
+
options.onRemove?.(mounted);
|
|
46
|
+
shadowHost.remove();
|
|
47
|
+
document.querySelector(`style[wxt-shadow-root-document-styles="${instanceId}"]`)?.remove();
|
|
48
|
+
while (uiContainer.lastChild) uiContainer.removeChild(uiContainer.lastChild);
|
|
49
|
+
mounted = void 0;
|
|
50
|
+
};
|
|
51
|
+
const mountFunctions = createMountFunctions({
|
|
52
|
+
mount,
|
|
53
|
+
remove
|
|
54
|
+
}, options);
|
|
55
|
+
ctx.onInvalidated(remove);
|
|
56
|
+
return {
|
|
57
|
+
shadow,
|
|
58
|
+
shadowHost,
|
|
59
|
+
uiContainer,
|
|
60
|
+
...mountFunctions,
|
|
61
|
+
get mounted() {
|
|
62
|
+
return mounted;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
74
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Load the CSS for the current entrypoint.
|
|
68
|
+
*/
|
|
75
69
|
async function loadCss() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
err
|
|
84
|
-
);
|
|
85
|
-
return "";
|
|
86
|
-
}
|
|
70
|
+
const url = browser.runtime.getURL(`/content-scripts/${import.meta.env.ENTRYPOINT}.css`);
|
|
71
|
+
try {
|
|
72
|
+
return await (await fetch(url)).text();
|
|
73
|
+
} catch (err) {
|
|
74
|
+
logger.warn(`Failed to load styles @ ${url}. Did you forget to import the stylesheet in your entrypoint?`, err);
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
87
77
|
}
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { createShadowRootUi };
|
|
@@ -1,157 +1,122 @@
|
|
|
1
|
+
import { logger } from "../internal/logger.mjs";
|
|
1
2
|
import { waitElement } from "@1natsu/wait-element";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
positionedElement.style.top = "0";
|
|
27
|
-
positionedElement.style.bottom = "0";
|
|
28
|
-
positionedElement.style.left = "0";
|
|
29
|
-
positionedElement.style.right = "0";
|
|
30
|
-
}
|
|
31
|
-
}
|
|
3
|
+
import { isExist, isNotExist } from "@1natsu/wait-element/detectors";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/content-script-ui/shared.ts
|
|
6
|
+
function applyPosition(root, positionedElement, options) {
|
|
7
|
+
if (options.position === "inline") return;
|
|
8
|
+
if (options.zIndex != null) root.style.zIndex = String(options.zIndex);
|
|
9
|
+
root.style.overflow = "visible";
|
|
10
|
+
root.style.position = "relative";
|
|
11
|
+
root.style.width = "0";
|
|
12
|
+
root.style.height = "0";
|
|
13
|
+
root.style.display = "block";
|
|
14
|
+
if (positionedElement) if (options.position === "overlay") {
|
|
15
|
+
positionedElement.style.position = "absolute";
|
|
16
|
+
if (options.alignment?.startsWith("bottom-")) positionedElement.style.bottom = "0";
|
|
17
|
+
else positionedElement.style.top = "0";
|
|
18
|
+
if (options.alignment?.endsWith("-right")) positionedElement.style.right = "0";
|
|
19
|
+
else positionedElement.style.left = "0";
|
|
20
|
+
} else {
|
|
21
|
+
positionedElement.style.position = "fixed";
|
|
22
|
+
positionedElement.style.top = "0";
|
|
23
|
+
positionedElement.style.bottom = "0";
|
|
24
|
+
positionedElement.style.left = "0";
|
|
25
|
+
positionedElement.style.right = "0";
|
|
26
|
+
}
|
|
32
27
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
resolved,
|
|
40
|
-
document,
|
|
41
|
-
null,
|
|
42
|
-
XPathResult.FIRST_ORDERED_NODE_TYPE,
|
|
43
|
-
null
|
|
44
|
-
);
|
|
45
|
-
return result.singleNodeValue ?? void 0;
|
|
46
|
-
} else {
|
|
47
|
-
return document.querySelector(resolved) ?? void 0;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return resolved ?? void 0;
|
|
28
|
+
function getAnchor(options) {
|
|
29
|
+
if (options.anchor == null) return document.body;
|
|
30
|
+
let resolved = typeof options.anchor === "function" ? options.anchor() : options.anchor;
|
|
31
|
+
if (typeof resolved === "string") if (resolved.startsWith("/")) return document.evaluate(resolved, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue ?? void 0;
|
|
32
|
+
else return document.querySelector(resolved) ?? void 0;
|
|
33
|
+
return resolved ?? void 0;
|
|
51
34
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
break;
|
|
75
|
-
default:
|
|
76
|
-
options.append(anchor, root);
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
35
|
+
function mountUi(root, options) {
|
|
36
|
+
const anchor = getAnchor(options);
|
|
37
|
+
if (anchor == null) throw Error("Failed to mount content script UI: could not find anchor element");
|
|
38
|
+
switch (options.append) {
|
|
39
|
+
case void 0:
|
|
40
|
+
case "last":
|
|
41
|
+
anchor.append(root);
|
|
42
|
+
break;
|
|
43
|
+
case "first":
|
|
44
|
+
anchor.prepend(root);
|
|
45
|
+
break;
|
|
46
|
+
case "replace":
|
|
47
|
+
anchor.replaceWith(root);
|
|
48
|
+
break;
|
|
49
|
+
case "after":
|
|
50
|
+
anchor.parentElement?.insertBefore(root, anchor.nextElementSibling);
|
|
51
|
+
break;
|
|
52
|
+
case "before":
|
|
53
|
+
anchor.parentElement?.insertBefore(root, anchor);
|
|
54
|
+
break;
|
|
55
|
+
default: options.append(anchor, root);
|
|
56
|
+
}
|
|
79
57
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
};
|
|
58
|
+
function createMountFunctions(baseFunctions, options) {
|
|
59
|
+
let autoMountInstance;
|
|
60
|
+
const stopAutoMount = () => {
|
|
61
|
+
autoMountInstance?.stopAutoMount();
|
|
62
|
+
autoMountInstance = void 0;
|
|
63
|
+
};
|
|
64
|
+
const mount = () => {
|
|
65
|
+
baseFunctions.mount();
|
|
66
|
+
};
|
|
67
|
+
const unmount = baseFunctions.remove;
|
|
68
|
+
const remove = () => {
|
|
69
|
+
stopAutoMount();
|
|
70
|
+
baseFunctions.remove();
|
|
71
|
+
};
|
|
72
|
+
const autoMount = (autoMountOptions) => {
|
|
73
|
+
if (autoMountInstance) logger.warn("autoMount is already set.");
|
|
74
|
+
autoMountInstance = autoMountUi({
|
|
75
|
+
mount,
|
|
76
|
+
unmount,
|
|
77
|
+
stopAutoMount
|
|
78
|
+
}, {
|
|
79
|
+
...options,
|
|
80
|
+
...autoMountOptions
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
mount,
|
|
85
|
+
remove,
|
|
86
|
+
autoMount
|
|
87
|
+
};
|
|
111
88
|
}
|
|
112
89
|
function autoMountUi(uiCallbacks, options) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (options.once) {
|
|
143
|
-
uiCallbacks.stopAutoMount();
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
} catch (error) {
|
|
147
|
-
if (abortController.signal.aborted && abortController.signal.reason === EXPLICIT_STOP_REASON) {
|
|
148
|
-
break;
|
|
149
|
-
} else {
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
observeElement(resolvedAnchor);
|
|
156
|
-
return { stopAutoMount: _stopAutoMount };
|
|
90
|
+
const abortController = new AbortController();
|
|
91
|
+
const EXPLICIT_STOP_REASON = "explicit_stop_auto_mount";
|
|
92
|
+
const _stopAutoMount = () => {
|
|
93
|
+
abortController.abort(EXPLICIT_STOP_REASON);
|
|
94
|
+
options.onStop?.();
|
|
95
|
+
};
|
|
96
|
+
let resolvedAnchor = typeof options.anchor === "function" ? options.anchor() : options.anchor;
|
|
97
|
+
if (resolvedAnchor instanceof Element) throw Error("autoMount and Element anchor option cannot be combined. Avoid passing `Element` directly or `() => Element` to the anchor.");
|
|
98
|
+
async function observeElement(selector) {
|
|
99
|
+
let isAnchorExist = !!getAnchor(options);
|
|
100
|
+
if (isAnchorExist) uiCallbacks.mount();
|
|
101
|
+
while (!abortController.signal.aborted) try {
|
|
102
|
+
isAnchorExist = !!await waitElement(selector ?? "body", {
|
|
103
|
+
customMatcher: () => getAnchor(options) ?? null,
|
|
104
|
+
detector: isAnchorExist ? isNotExist : isExist,
|
|
105
|
+
signal: abortController.signal
|
|
106
|
+
});
|
|
107
|
+
if (isAnchorExist) uiCallbacks.mount();
|
|
108
|
+
else {
|
|
109
|
+
uiCallbacks.unmount();
|
|
110
|
+
if (options.once) uiCallbacks.stopAutoMount();
|
|
111
|
+
}
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (abortController.signal.aborted && abortController.signal.reason === EXPLICIT_STOP_REASON) break;
|
|
114
|
+
else throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
observeElement(resolvedAnchor);
|
|
118
|
+
return { stopAutoMount: _stopAutoMount };
|
|
157
119
|
}
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
export { applyPosition, createMountFunctions, mountUi };
|