@@ -242,8 +242,29 @@ export class Frame extends EventEmitter {
242242 const maxAttempts = 3 ;
243243 let attempts = 0 ;
244244 let lastError : any ;
245+ let useDefaultContext = false ;
246+
245247 while ( attempts < maxAttempts ) {
246- const ctx = await this . getCurrentExecutionContext ( ) ;
248+ let ctx : ExecutionContext ;
249+
250+ try {
251+ // Try isolated world first, unless we're already using default context
252+ if ( ! useDefaultContext ) {
253+ ctx = await this . getCurrentExecutionContext ( ) ;
254+ } else {
255+ ctx = await this . getDefaultExecutionContext ( ) ;
256+ }
257+ } catch ( err : any ) {
258+ // If isolated world fails, try default context as fallback
259+ if ( ! useDefaultContext ) {
260+ this . logger . warn ( 'Isolated world failed, falling back to default context' , { error : err . message } ) ;
261+ useDefaultContext = true ;
262+ ctx = await this . getDefaultExecutionContext ( ) ;
263+ } else {
264+ throw err ;
265+ }
266+ }
267+
247268 try {
248269 return await fn ( ctx ) ;
249270 } catch ( err : any ) {
@@ -259,6 +280,13 @@ export class Frame extends EventEmitter {
259280 if ( isContextError || isToolkitError ) {
260281 this . _isolatedWorld = null ;
261282 attempts += 1 ;
283+
284+ // If isolated world failed and we haven't tried default context yet, switch to it
285+ if ( ! useDefaultContext && attempts < maxAttempts ) {
286+ useDefaultContext = true ;
287+ this . logger . warn ( 'Switching to default context due to isolated world failure' , { attempt : attempts } ) ;
288+ }
289+
262290 if ( attempts < maxAttempts ) {
263291 await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
264292 continue ;
0 commit comments