Skip to content

Commit f771e6a

Browse files
jvineyaklinker1
andauthored
feat: Add content script noScriptStartedPostMessage option (#2265)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
1 parent a0a2394 commit f771e6a

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

packages/wxt/src/types.ts

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

716732
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)