File tree Expand file tree Collapse file tree 3 files changed +44
-10
lines changed
Expand file tree Collapse file tree 3 files changed +44
-10
lines changed Original file line number Diff line number Diff 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
718734export interface MainWorldContentScriptEntrypointOptions extends BaseContentScriptEntrypointOptions {
Original file line number Diff line number Diff 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' , ( ) => {
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments