diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 6a0df7095ce..b2d0f2af16a 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -64,7 +64,8 @@ export class CDPBrowser extends BrowserBase { process?: ChildProcess, closeCallback?: BrowserCloseCallback, targetFilterCallback?: TargetFilterCallback, - isPageTargetCallback?: IsPageTargetCallback + isPageTargetCallback?: IsPageTargetCallback, + waitForInitiallyDiscoveredTargets = true ): Promise { const browser = new CDPBrowser( product, @@ -75,7 +76,8 @@ export class CDPBrowser extends BrowserBase { process, closeCallback, targetFilterCallback, - isPageTargetCallback + isPageTargetCallback, + waitForInitiallyDiscoveredTargets ); await browser._attach(); return browser; @@ -111,7 +113,8 @@ export class CDPBrowser extends BrowserBase { process?: ChildProcess, closeCallback?: BrowserCloseCallback, targetFilterCallback?: TargetFilterCallback, - isPageTargetCallback?: IsPageTargetCallback + isPageTargetCallback?: IsPageTargetCallback, + waitForInitiallyDiscoveredTargets = true ) { super(); product = product || 'chrome'; @@ -137,7 +140,8 @@ export class CDPBrowser extends BrowserBase { this.#targetManager = new ChromeTargetManager( connection, this.#createTarget, - this.#targetFilterCallback + this.#targetFilterCallback, + waitForInitiallyDiscoveredTargets ); } this.#defaultContext = new CDPBrowserContext(this.#connection, this); diff --git a/packages/puppeteer-core/src/common/ChromeTargetManager.ts b/packages/puppeteer-core/src/common/ChromeTargetManager.ts index e31e14f24f2..edd29b39bda 100644 --- a/packages/puppeteer-core/src/common/ChromeTargetManager.ts +++ b/packages/puppeteer-core/src/common/ChromeTargetManager.ts @@ -84,16 +84,19 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { #initializeDeferred = Deferred.create(); #targetsIdsForInit = new Set(); + #waitForInitiallyDiscoveredTargets = true; constructor( connection: Connection, targetFactory: TargetFactory, - targetFilterCallback?: TargetFilterCallback + targetFilterCallback?: TargetFilterCallback, + waitForInitiallyDiscoveredTargets = true ) { super(); this.#connection = connection; this.#targetFilterCallback = targetFilterCallback; this.#targetFactory = targetFactory; + this.#waitForInitiallyDiscoveredTargets = waitForInitiallyDiscoveredTargets; this.#connection.on('Target.targetCreated', this.#onTargetCreated); this.#connection.on('Target.targetDestroyed', this.#onTargetDestroyed); @@ -111,6 +114,9 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { } #storeExistingTargetsForInit = () => { + if (!this.#waitForInitiallyDiscoveredTargets) { + return; + } for (const [ targetId, targetInfo,