wxt 0.11.0 → 0.11.1
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/{chunk-6V4H2CZ4.js → chunk-65FJ5ESC.js} +1 -1
- package/dist/cli.cjs +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.js +59 -5
- package/dist/{external-6YqvLCcd.d.ts → external-PmmO6xnl.d.ts} +19 -3
- package/dist/{external-n9SucYhJ.d.cts → external-irU6kFSB.d.cts} +19 -3
- package/dist/{external-n9SucYhJ.d.ts → external-irU6kFSB.d.ts} +19 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/sandbox.d.ts +1 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +1 -1
- package/dist/virtual/content-script-entrypoint.js +59 -5
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -2415,7 +2415,7 @@ var init_execa = __esm({
|
|
|
2415
2415
|
var import_cac = __toESM(require("cac"), 1);
|
|
2416
2416
|
|
|
2417
2417
|
// package.json
|
|
2418
|
-
var version = "0.11.
|
|
2418
|
+
var version = "0.11.1";
|
|
2419
2419
|
|
|
2420
2420
|
// src/core/utils/fs.ts
|
|
2421
2421
|
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as BackgroundDefinition, C as ContentScriptContext, a as ContentScriptDefinition } from './external-
|
|
1
|
+
import { B as BackgroundDefinition, C as ContentScriptContext, a as ContentScriptDefinition } from './external-PmmO6xnl.js';
|
|
2
2
|
import * as wxt_browser from 'wxt/browser';
|
|
3
3
|
import 'webextension-polyfill';
|
|
4
4
|
|
package/dist/client.js
CHANGED
|
@@ -27,6 +27,44 @@ var logger = {
|
|
|
27
27
|
error: (...args) => print(console.error, ...args)
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
+
// src/client/content-scripts/custom-events.ts
|
|
31
|
+
var WxtLocationChangeEvent = class _WxtLocationChangeEvent extends Event {
|
|
32
|
+
constructor(newUrl, oldUrl) {
|
|
33
|
+
super(_WxtLocationChangeEvent.EVENT_NAME, {});
|
|
34
|
+
this.newUrl = newUrl;
|
|
35
|
+
this.oldUrl = oldUrl;
|
|
36
|
+
}
|
|
37
|
+
static EVENT_NAME = getUniqueEventName("wxt:locationchange");
|
|
38
|
+
};
|
|
39
|
+
function getUniqueEventName(eventName) {
|
|
40
|
+
const entrypointName = typeof __ENTRYPOINT__ === "undefined" ? "build" : __ENTRYPOINT__;
|
|
41
|
+
return `${browser.runtime.id}:${entrypointName}:${eventName}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/client/content-scripts/location-watcher.ts
|
|
45
|
+
function createLocationWatcher(ctx) {
|
|
46
|
+
let interval;
|
|
47
|
+
let oldUrl;
|
|
48
|
+
return {
|
|
49
|
+
/**
|
|
50
|
+
* Ensure the location watcher is actively looking for URL changes. If it's already watching,
|
|
51
|
+
* this is a noop.
|
|
52
|
+
*/
|
|
53
|
+
run() {
|
|
54
|
+
if (interval != null)
|
|
55
|
+
return;
|
|
56
|
+
oldUrl = new URL(location.href);
|
|
57
|
+
interval = ctx.setInterval(() => {
|
|
58
|
+
let newUrl = new URL(location.href);
|
|
59
|
+
if (newUrl.href !== oldUrl.href) {
|
|
60
|
+
window.dispatchEvent(new WxtLocationChangeEvent(newUrl, oldUrl));
|
|
61
|
+
oldUrl = newUrl;
|
|
62
|
+
}
|
|
63
|
+
}, 1e3);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
30
68
|
// src/client/content-scripts/content-script-context.ts
|
|
31
69
|
var ContentScriptContext = class _ContentScriptContext {
|
|
32
70
|
constructor(contentScriptName, options) {
|
|
@@ -43,6 +81,7 @@ var ContentScriptContext = class _ContentScriptContext {
|
|
|
43
81
|
static SCRIPT_STARTED_MESSAGE_TYPE = "wxt:content-script-started";
|
|
44
82
|
#isTopFrame = window.self === window.top;
|
|
45
83
|
#abortController;
|
|
84
|
+
#locationWatcher = createLocationWatcher(this);
|
|
46
85
|
get signal() {
|
|
47
86
|
return this.#abortController.signal;
|
|
48
87
|
}
|
|
@@ -139,18 +178,33 @@ var ContentScriptContext = class _ContentScriptContext {
|
|
|
139
178
|
/**
|
|
140
179
|
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
|
|
141
180
|
*
|
|
181
|
+
* Includes additional events useful for content scripts:
|
|
182
|
+
*
|
|
183
|
+
* - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
|
|
184
|
+
* scripts are not reloaded when navigating this way, so this can be used to reset the content
|
|
185
|
+
* script state on URL change, or run custom code.
|
|
186
|
+
*
|
|
142
187
|
* @example
|
|
143
|
-
* ctx.addEventListener(
|
|
188
|
+
* ctx.addEventListener(document, "visibilitychange", () => {
|
|
144
189
|
* // ...
|
|
145
190
|
* });
|
|
146
|
-
* ctx.addEventListener(document, "
|
|
191
|
+
* ctx.addEventListener(document, "wxt:locationchange", () => {
|
|
147
192
|
* // ...
|
|
148
193
|
* });
|
|
149
194
|
*/
|
|
150
195
|
addEventListener(target, type, handler, options) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
196
|
+
if (type === "wxt:locationchange") {
|
|
197
|
+
if (this.isValid)
|
|
198
|
+
this.#locationWatcher.run();
|
|
199
|
+
}
|
|
200
|
+
target.addEventListener?.(
|
|
201
|
+
type.startsWith("wxt:") ? getUniqueEventName(type) : type,
|
|
202
|
+
// @ts-expect-error: Event don't match, but that's OK, EventTarget doesn't allow custom types in the callback
|
|
203
|
+
handler,
|
|
204
|
+
{
|
|
205
|
+
...options,
|
|
206
|
+
signal: this.signal
|
|
207
|
+
}
|
|
154
208
|
);
|
|
155
209
|
}
|
|
156
210
|
/**
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Manifest } from 'webextension-polyfill';
|
|
2
2
|
|
|
3
|
+
declare class WxtLocationChangeEvent extends Event {
|
|
4
|
+
readonly newUrl: URL;
|
|
5
|
+
readonly oldUrl: URL;
|
|
6
|
+
static EVENT_NAME: string;
|
|
7
|
+
constructor(newUrl: URL, oldUrl: URL);
|
|
8
|
+
}
|
|
9
|
+
|
|
3
10
|
/**
|
|
4
11
|
* Implements [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
5
12
|
* Used to detect and stop content script code when the script is invalidated.
|
|
@@ -64,21 +71,30 @@ declare class ContentScriptContext implements AbortController {
|
|
|
64
71
|
/**
|
|
65
72
|
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
|
|
66
73
|
*
|
|
74
|
+
* Includes additional events useful for content scripts:
|
|
75
|
+
*
|
|
76
|
+
* - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
|
|
77
|
+
* scripts are not reloaded when navigating this way, so this can be used to reset the content
|
|
78
|
+
* script state on URL change, or run custom code.
|
|
79
|
+
*
|
|
67
80
|
* @example
|
|
68
|
-
* ctx.addEventListener(
|
|
81
|
+
* ctx.addEventListener(document, "visibilitychange", () => {
|
|
69
82
|
* // ...
|
|
70
83
|
* });
|
|
71
|
-
* ctx.addEventListener(document, "
|
|
84
|
+
* ctx.addEventListener(document, "wxt:locationchange", () => {
|
|
72
85
|
* // ...
|
|
73
86
|
* });
|
|
74
87
|
*/
|
|
75
|
-
addEventListener(target:
|
|
88
|
+
addEventListener<TTarget extends EventTarget, TType extends keyof WxtContentScriptEventMap>(target: TTarget, type: TType, handler: (event: WxtContentScriptEventMap[TType]) => void, options?: AddEventListenerOptions): void;
|
|
76
89
|
/**
|
|
77
90
|
* @internal
|
|
78
91
|
* Abort the abort controller and execute all `onInvalidated` listeners.
|
|
79
92
|
*/
|
|
80
93
|
notifyInvalidated(): void;
|
|
81
94
|
}
|
|
95
|
+
interface WxtContentScriptEventMap extends WindowEventMap {
|
|
96
|
+
'wxt:locationchange': WxtLocationChangeEvent;
|
|
97
|
+
}
|
|
82
98
|
|
|
83
99
|
type TargetBrowser = string;
|
|
84
100
|
interface ContentScriptDefinition extends ExcludableEntrypoint {
|
|
@@ -5,6 +5,13 @@ import { LogLevel } from 'consola';
|
|
|
5
5
|
import { PluginVisualizerOptions } from 'rollup-plugin-visualizer';
|
|
6
6
|
import { FSWatcher } from 'chokidar';
|
|
7
7
|
|
|
8
|
+
declare class WxtLocationChangeEvent extends Event {
|
|
9
|
+
readonly newUrl: URL;
|
|
10
|
+
readonly oldUrl: URL;
|
|
11
|
+
static EVENT_NAME: string;
|
|
12
|
+
constructor(newUrl: URL, oldUrl: URL);
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
/**
|
|
9
16
|
* Implements [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
10
17
|
* Used to detect and stop content script code when the script is invalidated.
|
|
@@ -69,21 +76,30 @@ declare class ContentScriptContext implements AbortController {
|
|
|
69
76
|
/**
|
|
70
77
|
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
|
|
71
78
|
*
|
|
79
|
+
* Includes additional events useful for content scripts:
|
|
80
|
+
*
|
|
81
|
+
* - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
|
|
82
|
+
* scripts are not reloaded when navigating this way, so this can be used to reset the content
|
|
83
|
+
* script state on URL change, or run custom code.
|
|
84
|
+
*
|
|
72
85
|
* @example
|
|
73
|
-
* ctx.addEventListener(
|
|
86
|
+
* ctx.addEventListener(document, "visibilitychange", () => {
|
|
74
87
|
* // ...
|
|
75
88
|
* });
|
|
76
|
-
* ctx.addEventListener(document, "
|
|
89
|
+
* ctx.addEventListener(document, "wxt:locationchange", () => {
|
|
77
90
|
* // ...
|
|
78
91
|
* });
|
|
79
92
|
*/
|
|
80
|
-
addEventListener(target:
|
|
93
|
+
addEventListener<TTarget extends EventTarget, TType extends keyof WxtContentScriptEventMap>(target: TTarget, type: TType, handler: (event: WxtContentScriptEventMap[TType]) => void, options?: AddEventListenerOptions): void;
|
|
81
94
|
/**
|
|
82
95
|
* @internal
|
|
83
96
|
* Abort the abort controller and execute all `onInvalidated` listeners.
|
|
84
97
|
*/
|
|
85
98
|
notifyInvalidated(): void;
|
|
86
99
|
}
|
|
100
|
+
interface WxtContentScriptEventMap extends WindowEventMap {
|
|
101
|
+
'wxt:locationchange': WxtLocationChangeEvent;
|
|
102
|
+
}
|
|
87
103
|
|
|
88
104
|
interface InlineConfig {
|
|
89
105
|
/**
|
|
@@ -5,6 +5,13 @@ import { LogLevel } from 'consola';
|
|
|
5
5
|
import { PluginVisualizerOptions } from 'rollup-plugin-visualizer';
|
|
6
6
|
import { FSWatcher } from 'chokidar';
|
|
7
7
|
|
|
8
|
+
declare class WxtLocationChangeEvent extends Event {
|
|
9
|
+
readonly newUrl: URL;
|
|
10
|
+
readonly oldUrl: URL;
|
|
11
|
+
static EVENT_NAME: string;
|
|
12
|
+
constructor(newUrl: URL, oldUrl: URL);
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
/**
|
|
9
16
|
* Implements [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
10
17
|
* Used to detect and stop content script code when the script is invalidated.
|
|
@@ -69,21 +76,30 @@ declare class ContentScriptContext implements AbortController {
|
|
|
69
76
|
/**
|
|
70
77
|
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
|
|
71
78
|
*
|
|
79
|
+
* Includes additional events useful for content scripts:
|
|
80
|
+
*
|
|
81
|
+
* - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
|
|
82
|
+
* scripts are not reloaded when navigating this way, so this can be used to reset the content
|
|
83
|
+
* script state on URL change, or run custom code.
|
|
84
|
+
*
|
|
72
85
|
* @example
|
|
73
|
-
* ctx.addEventListener(
|
|
86
|
+
* ctx.addEventListener(document, "visibilitychange", () => {
|
|
74
87
|
* // ...
|
|
75
88
|
* });
|
|
76
|
-
* ctx.addEventListener(document, "
|
|
89
|
+
* ctx.addEventListener(document, "wxt:locationchange", () => {
|
|
77
90
|
* // ...
|
|
78
91
|
* });
|
|
79
92
|
*/
|
|
80
|
-
addEventListener(target:
|
|
93
|
+
addEventListener<TTarget extends EventTarget, TType extends keyof WxtContentScriptEventMap>(target: TTarget, type: TType, handler: (event: WxtContentScriptEventMap[TType]) => void, options?: AddEventListenerOptions): void;
|
|
81
94
|
/**
|
|
82
95
|
* @internal
|
|
83
96
|
* Abort the abort controller and execute all `onInvalidated` listeners.
|
|
84
97
|
*/
|
|
85
98
|
notifyInvalidated(): void;
|
|
86
99
|
}
|
|
100
|
+
interface WxtContentScriptEventMap extends WindowEventMap {
|
|
101
|
+
'wxt:locationchange': WxtLocationChangeEvent;
|
|
102
|
+
}
|
|
87
103
|
|
|
88
104
|
interface InlineConfig {
|
|
89
105
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -4299,7 +4299,7 @@ function getChunkSortWeight(filename) {
|
|
|
4299
4299
|
var import_picocolors3 = __toESM(require("picocolors"), 1);
|
|
4300
4300
|
|
|
4301
4301
|
// package.json
|
|
4302
|
-
var version = "0.11.
|
|
4302
|
+
var version = "0.11.1";
|
|
4303
4303
|
|
|
4304
4304
|
// src/core/utils/log/printHeader.ts
|
|
4305
4305
|
var import_consola2 = require("consola");
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as InlineConfig, B as BuildOutput, U as UserConfig, E as ExtensionRunnerConfig, W as WxtDevServer } from './external-
|
|
2
|
-
export { n as BackgroundDefinition, h as BackgroundEntrypoint, g as BaseEntrypoint, f as BaseEntrypointOptions, d as BuildStepOutput, t as ConfigEnv, m as ContentScriptDefinition, C as ContentScriptEntrypoint, j as Entrypoint, k as EntrypointGroup, q as ExcludableEntrypoint, G as GenericEntrypoint, L as Logger, l as OnContentScriptStopped, i as OptionsEntrypoint, c as OutputAsset, b as OutputChunk, O as OutputFile, p as PerBrowserOption, P as PopupEntrypoint, S as ServerInfo, T as TargetBrowser, e as TargetManifestVersion, o as UnlistedScriptDefinition, r as UserManifest, s as UserManifestFn, u as WxtBuilder, v as WxtBuilderServer, a as WxtViteConfig } from './external-
|
|
1
|
+
import { I as InlineConfig, B as BuildOutput, U as UserConfig, E as ExtensionRunnerConfig, W as WxtDevServer } from './external-irU6kFSB.cjs';
|
|
2
|
+
export { n as BackgroundDefinition, h as BackgroundEntrypoint, g as BaseEntrypoint, f as BaseEntrypointOptions, d as BuildStepOutput, t as ConfigEnv, m as ContentScriptDefinition, C as ContentScriptEntrypoint, j as Entrypoint, k as EntrypointGroup, q as ExcludableEntrypoint, G as GenericEntrypoint, L as Logger, l as OnContentScriptStopped, i as OptionsEntrypoint, c as OutputAsset, b as OutputChunk, O as OutputFile, p as PerBrowserOption, P as PopupEntrypoint, S as ServerInfo, T as TargetBrowser, e as TargetManifestVersion, o as UnlistedScriptDefinition, r as UserManifest, s as UserManifestFn, u as WxtBuilder, v as WxtBuilderServer, a as WxtViteConfig } from './external-irU6kFSB.cjs';
|
|
3
3
|
import 'vite';
|
|
4
4
|
import 'webextension-polyfill';
|
|
5
5
|
import 'unimport';
|
|
@@ -62,6 +62,6 @@ declare function prepare(config: InlineConfig): Promise<void>;
|
|
|
62
62
|
*/
|
|
63
63
|
declare function zip(config?: InlineConfig): Promise<string[]>;
|
|
64
64
|
|
|
65
|
-
var version = "0.11.
|
|
65
|
+
var version = "0.11.1";
|
|
66
66
|
|
|
67
67
|
export { BuildOutput, ExtensionRunnerConfig, InlineConfig, UserConfig, WxtDevServer, build, clean, createServer, defineConfig, defineRunnerConfig, initialize, prepare, version, zip };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as InlineConfig, B as BuildOutput, U as UserConfig, E as ExtensionRunnerConfig, W as WxtDevServer } from './external-
|
|
2
|
-
export { n as BackgroundDefinition, h as BackgroundEntrypoint, g as BaseEntrypoint, f as BaseEntrypointOptions, d as BuildStepOutput, t as ConfigEnv, m as ContentScriptDefinition, C as ContentScriptEntrypoint, j as Entrypoint, k as EntrypointGroup, q as ExcludableEntrypoint, G as GenericEntrypoint, L as Logger, l as OnContentScriptStopped, i as OptionsEntrypoint, c as OutputAsset, b as OutputChunk, O as OutputFile, p as PerBrowserOption, P as PopupEntrypoint, S as ServerInfo, T as TargetBrowser, e as TargetManifestVersion, o as UnlistedScriptDefinition, r as UserManifest, s as UserManifestFn, u as WxtBuilder, v as WxtBuilderServer, a as WxtViteConfig } from './external-
|
|
1
|
+
import { I as InlineConfig, B as BuildOutput, U as UserConfig, E as ExtensionRunnerConfig, W as WxtDevServer } from './external-irU6kFSB.js';
|
|
2
|
+
export { n as BackgroundDefinition, h as BackgroundEntrypoint, g as BaseEntrypoint, f as BaseEntrypointOptions, d as BuildStepOutput, t as ConfigEnv, m as ContentScriptDefinition, C as ContentScriptEntrypoint, j as Entrypoint, k as EntrypointGroup, q as ExcludableEntrypoint, G as GenericEntrypoint, L as Logger, l as OnContentScriptStopped, i as OptionsEntrypoint, c as OutputAsset, b as OutputChunk, O as OutputFile, p as PerBrowserOption, P as PopupEntrypoint, S as ServerInfo, T as TargetBrowser, e as TargetManifestVersion, o as UnlistedScriptDefinition, r as UserManifest, s as UserManifestFn, u as WxtBuilder, v as WxtBuilderServer, a as WxtViteConfig } from './external-irU6kFSB.js';
|
|
3
3
|
import 'vite';
|
|
4
4
|
import 'webextension-polyfill';
|
|
5
5
|
import 'unimport';
|
|
@@ -62,6 +62,6 @@ declare function prepare(config: InlineConfig): Promise<void>;
|
|
|
62
62
|
*/
|
|
63
63
|
declare function zip(config?: InlineConfig): Promise<string[]>;
|
|
64
64
|
|
|
65
|
-
var version = "0.11.
|
|
65
|
+
var version = "0.11.1";
|
|
66
66
|
|
|
67
67
|
export { BuildOutput, ExtensionRunnerConfig, InlineConfig, UserConfig, WxtDevServer, build, clean, createServer, defineConfig, defineRunnerConfig, initialize, prepare, version, zip };
|
package/dist/index.js
CHANGED
package/dist/sandbox.d.ts
CHANGED
package/dist/testing.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { FakeBrowser, fakeBrowser } from '@webext-core/fake-browser';
|
|
2
2
|
import * as vite from 'vite';
|
|
3
|
-
import { I as InlineConfig } from './external-
|
|
3
|
+
import { I as InlineConfig } from './external-irU6kFSB.cjs';
|
|
4
4
|
import 'webextension-polyfill';
|
|
5
5
|
import 'unimport';
|
|
6
6
|
import 'consola';
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { FakeBrowser, fakeBrowser } from '@webext-core/fake-browser';
|
|
2
2
|
import * as vite from 'vite';
|
|
3
|
-
import { I as InlineConfig } from './external-
|
|
3
|
+
import { I as InlineConfig } from './external-irU6kFSB.js';
|
|
4
4
|
import 'webextension-polyfill';
|
|
5
5
|
import 'unimport';
|
|
6
6
|
import 'consola';
|
package/dist/testing.js
CHANGED
|
@@ -23,6 +23,44 @@ var logger = {
|
|
|
23
23
|
import originalBrowser from "webextension-polyfill";
|
|
24
24
|
var browser = originalBrowser;
|
|
25
25
|
|
|
26
|
+
// src/client/content-scripts/custom-events.ts
|
|
27
|
+
var WxtLocationChangeEvent = class _WxtLocationChangeEvent extends Event {
|
|
28
|
+
constructor(newUrl, oldUrl) {
|
|
29
|
+
super(_WxtLocationChangeEvent.EVENT_NAME, {});
|
|
30
|
+
this.newUrl = newUrl;
|
|
31
|
+
this.oldUrl = oldUrl;
|
|
32
|
+
}
|
|
33
|
+
static EVENT_NAME = getUniqueEventName("wxt:locationchange");
|
|
34
|
+
};
|
|
35
|
+
function getUniqueEventName(eventName) {
|
|
36
|
+
const entrypointName = typeof __ENTRYPOINT__ === "undefined" ? "build" : __ENTRYPOINT__;
|
|
37
|
+
return `${browser.runtime.id}:${entrypointName}:${eventName}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/client/content-scripts/location-watcher.ts
|
|
41
|
+
function createLocationWatcher(ctx) {
|
|
42
|
+
let interval;
|
|
43
|
+
let oldUrl;
|
|
44
|
+
return {
|
|
45
|
+
/**
|
|
46
|
+
* Ensure the location watcher is actively looking for URL changes. If it's already watching,
|
|
47
|
+
* this is a noop.
|
|
48
|
+
*/
|
|
49
|
+
run() {
|
|
50
|
+
if (interval != null)
|
|
51
|
+
return;
|
|
52
|
+
oldUrl = new URL(location.href);
|
|
53
|
+
interval = ctx.setInterval(() => {
|
|
54
|
+
let newUrl = new URL(location.href);
|
|
55
|
+
if (newUrl.href !== oldUrl.href) {
|
|
56
|
+
window.dispatchEvent(new WxtLocationChangeEvent(newUrl, oldUrl));
|
|
57
|
+
oldUrl = newUrl;
|
|
58
|
+
}
|
|
59
|
+
}, 1e3);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
26
64
|
// src/client/content-scripts/content-script-context.ts
|
|
27
65
|
var ContentScriptContext = class _ContentScriptContext {
|
|
28
66
|
constructor(contentScriptName, options) {
|
|
@@ -39,6 +77,7 @@ var ContentScriptContext = class _ContentScriptContext {
|
|
|
39
77
|
static SCRIPT_STARTED_MESSAGE_TYPE = "wxt:content-script-started";
|
|
40
78
|
#isTopFrame = window.self === window.top;
|
|
41
79
|
#abortController;
|
|
80
|
+
#locationWatcher = createLocationWatcher(this);
|
|
42
81
|
get signal() {
|
|
43
82
|
return this.#abortController.signal;
|
|
44
83
|
}
|
|
@@ -135,18 +174,33 @@ var ContentScriptContext = class _ContentScriptContext {
|
|
|
135
174
|
/**
|
|
136
175
|
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
|
|
137
176
|
*
|
|
177
|
+
* Includes additional events useful for content scripts:
|
|
178
|
+
*
|
|
179
|
+
* - `"wxt:locationchange"` - Triggered when HTML5 history mode is used to change URL. Content
|
|
180
|
+
* scripts are not reloaded when navigating this way, so this can be used to reset the content
|
|
181
|
+
* script state on URL change, or run custom code.
|
|
182
|
+
*
|
|
138
183
|
* @example
|
|
139
|
-
* ctx.addEventListener(
|
|
184
|
+
* ctx.addEventListener(document, "visibilitychange", () => {
|
|
140
185
|
* // ...
|
|
141
186
|
* });
|
|
142
|
-
* ctx.addEventListener(document, "
|
|
187
|
+
* ctx.addEventListener(document, "wxt:locationchange", () => {
|
|
143
188
|
* // ...
|
|
144
189
|
* });
|
|
145
190
|
*/
|
|
146
191
|
addEventListener(target, type, handler, options) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
192
|
+
if (type === "wxt:locationchange") {
|
|
193
|
+
if (this.isValid)
|
|
194
|
+
this.#locationWatcher.run();
|
|
195
|
+
}
|
|
196
|
+
target.addEventListener?.(
|
|
197
|
+
type.startsWith("wxt:") ? getUniqueEventName(type) : type,
|
|
198
|
+
// @ts-expect-error: Event don't match, but that's OK, EventTarget doesn't allow custom types in the callback
|
|
199
|
+
handler,
|
|
200
|
+
{
|
|
201
|
+
...options,
|
|
202
|
+
signal: this.signal
|
|
203
|
+
}
|
|
150
204
|
);
|
|
151
205
|
}
|
|
152
206
|
/**
|