Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,22 @@ export interface BaseContentScriptEntrypointOptions extends BaseScriptEntrypoint
* @default 'manifest'
*/
registration?: PerBrowserOption<'manifest' | 'runtime'>;
/**
* Do not send the `wxt:content-script-started` message via
* `window.postMessage`.
*
* This has been replaced with custom events. The `postMessage` call is kept
* for backwards compatibility. For some websites the `postMessage` call is
* undesirable, such as those with poorly written message event listeners.
*
* Setting this to `true` opts into the behaviour that will become the default
* in a future version of WXT, where the `postMessage` call is removed
* entirely.
*
* See https://github.com/wxt-dev/wxt/pull/1938 and
* https://github.com/wxt-dev/wxt/pull/2035 for a detailed discussion.
*/
noScriptStartedPostMessage?: boolean;
}

export interface MainWorldContentScriptEntrypointOptions extends BaseContentScriptEntrypointOptions {
Expand Down
16 changes: 16 additions & 0 deletions packages/wxt/src/utils/__tests__/content-script-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ describe('Content Script Context', () => {
expect(ctx.isValid).toBe(true);
});

describe('noScriptStartedPostMessage', () => {
it('should send window.postMessage by default', async () => {
const postMessageSpy = vi.spyOn(window, 'postMessage');
new ContentScriptContext('test');
expect(postMessageSpy).toHaveBeenCalledOnce();
postMessageSpy.mockRestore();
});

it('should not send window.postMessage when noScriptStartedPostMessage is true', async () => {
const postMessageSpy = vi.spyOn(window, 'postMessage');
new ContentScriptContext('test', { noScriptStartedPostMessage: true });
expect(postMessageSpy).not.toHaveBeenCalled();
postMessageSpy.mockRestore();
});
});

describe('addEventListener', () => {
const context = new ContentScriptContext('test');
it('should infer types correctly for the window target', () => {
Expand Down
22 changes: 12 additions & 10 deletions packages/wxt/src/utils/content-script-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,18 @@ export class ContentScriptContext implements AbortController {
}),
);

// Send message using `window.postMessage` for backwards compatibility to invalidate old versions before WXT changed to `document.dispatchEvent`
// TODO: Remove this once WXT version using `document.dispatchEvent` has been released for a while
window.postMessage(
{
type: ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE,
contentScriptName: this.contentScriptName,
messageId: this.id,
},
'*',
);
if (!this.options?.noScriptStartedPostMessage) {
// Send message using `window.postMessage` for backwards compatibility to invalidate old versions before WXT changed to `document.dispatchEvent`
// TODO: Remove this once WXT version using `document.dispatchEvent` has been released for a while
window.postMessage(
{
type: ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE,
contentScriptName: this.contentScriptName,
messageId: this.id,
},
'*',
);
}
}

verifyScriptStartedEvent(event: CustomEvent) {
Expand Down