@@ -115,6 +115,7 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
115115 _clientCertificatesProxy : ClientCertificatesProxy | undefined ;
116116 private _playwrightBindingExposed ?: Promise < void > ;
117117 readonly dialogManager : DialogManager ;
118+ private _consoleApiExposed = false ;
118119
119120 constructor ( browser : Browser , options : types . BrowserContextOptions , browserContextId : string | undefined ) {
120121 super ( browser , 'browser-context' ) ;
@@ -159,12 +160,9 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
159160 RecorderApp . showInspectorNoReply ( this ) ;
160161 } ) ;
161162
162- if ( debugMode ( ) === 'console' ) {
163- await this . extendInjectedScript ( `
164- function installConsoleApi(injectedScript) { injectedScript.consoleApi.install(); }
165- module.exports = { default: () => installConsoleApi };
166- ` ) ;
167- }
163+ if ( debugMode ( ) === 'console' )
164+ await this . exposeConsoleApi ( ) ;
165+
168166 if ( this . _options . serviceWorkers === 'block' )
169167 await this . addInitScript ( undefined , `\nif (navigator.serviceWorker) navigator.serviceWorker.register = async () => { console.warn('Service Worker registration blocked by Playwright'); };\n` ) ;
170168
@@ -176,6 +174,16 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
176174 return this . _debugger ;
177175 }
178176
177+ async exposeConsoleApi ( ) {
178+ if ( this . _consoleApiExposed )
179+ return ;
180+ this . _consoleApiExposed = true ;
181+ await this . extendInjectedScript ( `
182+ function installConsoleApi(injectedScript) { injectedScript.consoleApi.install(); }
183+ module.exports = { default: () => installConsoleApi };
184+ ` ) ;
185+ }
186+
179187 async _ensureVideosPath ( ) {
180188 if ( this . _options . recordVideo )
181189 await mkdirIfNeeded ( path . join ( this . _options . recordVideo . dir , 'dummy' ) ) ;
0 commit comments