diff --git a/packages/puppeteer-core/src/common/ChromeTargetManager.ts b/packages/puppeteer-core/src/common/ChromeTargetManager.ts index 5a9280beb3b..f80cbb842b1 100644 --- a/packages/puppeteer-core/src/common/ChromeTargetManager.ts +++ b/packages/puppeteer-core/src/common/ChromeTargetManager.ts @@ -227,6 +227,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { return; } const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#attachedTargetsByTargetId.set(event.targetInfo.targetId, target); } }; @@ -324,18 +325,12 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { return; } const target = this.#targetFactory(targetInfo); + target._initialize(); this.#attachedTargetsByTargetId.set(targetInfo.targetId, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target); return; } - if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) { - this.#ignoredTargets.add(targetInfo.targetId); - this.#finishInitializationIfReady(targetInfo.targetId); - await silentDetach(); - return; - } - const existingTarget = this.#attachedTargetsByTargetId.has( targetInfo.targetId ); @@ -344,6 +339,17 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { ? this.#attachedTargetsByTargetId.get(targetInfo.targetId)! : this.#targetFactory(targetInfo, session); + if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) { + this.#ignoredTargets.add(targetInfo.targetId); + this.#finishInitializationIfReady(targetInfo.targetId); + await silentDetach(); + return; + } + + if (!existingTarget) { + target._initialize(); + } + this.#setupAttachmentListeners(session); if (existingTarget) { diff --git a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts index 1a095a8ff49..b798180c8e4 100644 --- a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts +++ b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts @@ -187,11 +187,13 @@ export class FirefoxTargetManager if (event.targetInfo.type === 'browser' && event.targetInfo.attached) { const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.#finishInitializationIfReady(target._targetId); return; } + const target = this.#targetFactory(event.targetInfo, undefined); if ( this.#targetFilterCallback && !this.#targetFilterCallback(event.targetInfo) @@ -200,8 +202,7 @@ export class FirefoxTargetManager this.#finishInitializationIfReady(event.targetInfo.targetId); return; } - - const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target); this.#finishInitializationIfReady(target._targetId); diff --git a/packages/puppeteer-core/src/common/Target.ts b/packages/puppeteer-core/src/common/Target.ts index 4a9745e6741..559cd1d95aa 100644 --- a/packages/puppeteer-core/src/common/Target.ts +++ b/packages/puppeteer-core/src/common/Target.ts @@ -66,6 +66,8 @@ export class Target { _targetId: string; /** + * To initialize the target for use, call initialize. + * * @internal */ constructor( @@ -81,7 +83,6 @@ export class Target { this.#browserContext = browserContext; this._targetId = targetInfo.targetId; this.#sessionFactory = sessionFactory; - this._initialize(); } /** @@ -197,7 +198,7 @@ export class Target { /** * @internal */ - protected _initialize(): void { + _initialize(): void { this._initializedDeferred.resolve(InitializationStatus.SUCCESS); } @@ -247,7 +248,7 @@ export class PageTarget extends Target { this.#screenshotTaskQueue = screenshotTaskQueue; } - protected override _initialize(): void { + override _initialize(): void { this._initializedDeferred .valueOrThrow() .then(async result => {