Skip to content

Commit 4319253

Browse files
committed
Add content script noScriptStartedPostMessage option.
The deprecated `window.postMessage` path for the `wxt:content-script-started` message causes bad interactions with sites that have a poorly implemented event listener. This PR adds an option to exclude the `window.postMessage` event entirely, until such time as WXT completely removes it, which is a breaking change. Related PRs #1938 #2035
1 parent b6059cf commit 4319253

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

packages/wxt/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,22 @@ export interface BaseContentScriptEntrypointOptions extends BaseScriptEntrypoint
713713
* @default 'manifest'
714714
*/
715715
registration?: PerBrowserOption<'manifest' | 'runtime'>;
716+
/**
717+
* Do not send the `wxt:content-script-started` message via
718+
* `window.postMessage`.
719+
*
720+
* This has been replaced with custom events. The `postMessage` call is kept
721+
* for backwards compatibility. For some websites the `postMessage` call is
722+
* undesirable, such as those with poorly written message event listeners.
723+
*
724+
* Setting this to `true` opts into the behaviour that will become the default
725+
* in a future version of WXT, where the `postMessage` call is removed
726+
* entirely.
727+
*
728+
* See https://github.com/wxt-dev/wxt/pull/1938 and
729+
* https://github.com/wxt-dev/wxt/pull/2035 for a detailed discussion.
730+
*/
731+
noScriptStartedPostMessage?: boolean;
716732
}
717733

718734
export interface MainWorldContentScriptEntrypointOptions extends BaseContentScriptEntrypointOptions {

packages/wxt/src/utils/__tests__/content-script-context.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ describe('Content Script Context', () => {
8484
expect(ctx.isValid).toBe(true);
8585
});
8686

87+
describe('noScriptStartedPostMessage', () => {
88+
it('should send window.postMessage by default', async () => {
89+
const postMessageSpy = vi.spyOn(window, 'postMessage');
90+
new ContentScriptContext('test');
91+
expect(postMessageSpy).toHaveBeenCalledOnce();
92+
postMessageSpy.mockRestore();
93+
});
94+
95+
it('should not send window.postMessage when noScriptStartedPostMessage is true', async () => {
96+
const postMessageSpy = vi.spyOn(window, 'postMessage');
97+
new ContentScriptContext('test', { noScriptStartedPostMessage: true });
98+
expect(postMessageSpy).not.toHaveBeenCalled();
99+
postMessageSpy.mockRestore();
100+
});
101+
});
102+
87103
describe('addEventListener', () => {
88104
const context = new ContentScriptContext('test');
89105
it('should infer types correctly for the window target', () => {

packages/wxt/src/utils/content-script-context.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,18 @@ export class ContentScriptContext implements AbortController {
258258
}),
259259
);
260260

261-
// Send message using `window.postMessage` for backwards compatibility to invalidate old versions before WXT changed to `document.dispatchEvent`
262-
// TODO: Remove this once WXT version using `document.dispatchEvent` has been released for a while
263-
window.postMessage(
264-
{
265-
type: ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE,
266-
contentScriptName: this.contentScriptName,
267-
messageId: this.id,
268-
},
269-
'*',
270-
);
261+
if (!this.options?.noScriptStartedPostMessage) {
262+
// Send message using `window.postMessage` for backwards compatibility to invalidate old versions before WXT changed to `document.dispatchEvent`
263+
// TODO: Remove this once WXT version using `document.dispatchEvent` has been released for a while
264+
window.postMessage(
265+
{
266+
type: ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE,
267+
contentScriptName: this.contentScriptName,
268+
messageId: this.id,
269+
},
270+
'*',
271+
);
272+
}
271273
}
272274

273275
verifyScriptStartedEvent(event: CustomEvent) {

0 commit comments

Comments
 (0)