@@ -98,11 +98,14 @@ export default defineContentScript({
9898 if ( manager . isActive ) {
9999 manager . stop ( )
100100 }
101- const { detectedCodeOrUnd } = await getDocumentInfo ( )
102- const detectedCode : LangCodeISO6393 = detectedCodeOrUnd === 'und' ? 'eng' : detectedCodeOrUnd
103- await storage . setItem < LangCodeISO6393 > ( `local:${ DETECTED_CODE_STORAGE_KEY } ` , detectedCode )
104- // Notify background script that URL has changed, let it decide whether to automatically enable translation
105- void sendMessage ( 'checkAndAskAutoPageTranslation' , { url : to , detectedCodeOrUnd } )
101+ // Only the top frame should detect and set language to avoid race conditions from iframes
102+ if ( window === window . top ) {
103+ const { detectedCodeOrUnd } = await getDocumentInfo ( )
104+ const detectedCode : LangCodeISO6393 = detectedCodeOrUnd === 'und' ? 'eng' : detectedCodeOrUnd
105+ await storage . setItem < LangCodeISO6393 > ( `local:${ DETECTED_CODE_STORAGE_KEY } ` , detectedCode )
106+ // Notify background script that URL has changed, let it decide whether to automatically enable translation
107+ void sendMessage ( 'checkAndAskAutoPageTranslation' , { url : to , detectedCodeOrUnd } )
108+ }
106109 }
107110 }
108111
@@ -129,11 +132,14 @@ export default defineContentScript({
129132 enabled ? void manager . start ( ) : manager . stop ( )
130133 } )
131134
132- const { detectedCodeOrUnd } = await getDocumentInfo ( )
133- const initialDetectedCode : LangCodeISO6393 = detectedCodeOrUnd === 'und' ? 'eng' : detectedCodeOrUnd
134- await storage . setItem < LangCodeISO6393 > ( `local:${ DETECTED_CODE_STORAGE_KEY } ` , initialDetectedCode )
135+ // Only the top frame should detect and set language to avoid race conditions from iframes
136+ if ( window === window . top ) {
137+ const { detectedCodeOrUnd } = await getDocumentInfo ( )
138+ const initialDetectedCode : LangCodeISO6393 = detectedCodeOrUnd === 'und' ? 'eng' : detectedCodeOrUnd
139+ await storage . setItem < LangCodeISO6393 > ( `local:${ DETECTED_CODE_STORAGE_KEY } ` , initialDetectedCode )
135140
136- // Check if auto-translation should be enabled for initial page load
137- void sendMessage ( 'checkAndAskAutoPageTranslation' , { url : window . location . href , detectedCodeOrUnd } )
141+ // Check if auto-translation should be enabled for initial page load
142+ void sendMessage ( 'checkAndAskAutoPageTranslation' , { url : window . location . href , detectedCodeOrUnd } )
143+ }
138144 } ,
139145} )
0 commit comments