diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 412bb2ea..2e18acd8 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -39,7 +39,7 @@ import {ChromeTargetManager} from './ChromeTargetManager.js'; import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js'; import {FirefoxTargetManager} from './FirefoxTargetManager.js'; import {Viewport} from './PuppeteerViewport.js'; -import {PageTarget, Target} from './Target.js'; +import {OtherTarget, PageTarget, Target, WorkerTarget} from './Target.js'; import {TargetManager, TargetManagerEmittedEvents} from './TargetManager.js'; import {TaskQueue} from './TaskQueue.js'; import {waitWithTimeout} from './util.js'; @@ -318,34 +318,39 @@ export class CDPBrowser extends BrowserBase { throw new Error('Missing browser context'); } + const createSession = (isAutoAttachEmulated: boolean) => { + return this.#connection._createSession(targetInfo, isAutoAttachEmulated); + }; if (this.#isPageTargetCallback(targetInfo)) { return new PageTarget( targetInfo, session, context, this.#targetManager, - (isAutoAttachEmulated: boolean) => { - return this.#connection._createSession( - targetInfo, - isAutoAttachEmulated - ); - }, + createSession, this.#ignoreHTTPSErrors, this.#defaultViewport ?? null, this.#screenshotTaskQueue ); } - return new Target( + if ( + targetInfo.type === 'service_worker' || + targetInfo.type === 'shared_worker' + ) { + return new WorkerTarget( + targetInfo, + session, + context, + this.#targetManager, + createSession + ); + } + return new OtherTarget( targetInfo, session, context, this.#targetManager, - (isAutoAttachEmulated: boolean) => { - return this.#connection._createSession( - targetInfo, - isAutoAttachEmulated - ); - } + createSession ); }; diff --git a/packages/puppeteer-core/src/common/Target.ts b/packages/puppeteer-core/src/common/Target.ts index 103cea57..c997adf0 100644 --- a/packages/puppeteer-core/src/common/Target.ts +++ b/packages/puppeteer-core/src/common/Target.ts @@ -41,7 +41,6 @@ export class Target { #session?: CDPSession; #targetInfo: Protocol.Target.TargetInfo; #sessionFactory: (isAutoAttachEmulated: boolean) => Promise; - #workerPromise?: Promise; /** * @internal @@ -136,28 +135,7 @@ export class Target { * If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`. */ async worker(): Promise { - if ( - this.#targetInfo.type !== 'service_worker' && - this.#targetInfo.type !== 'shared_worker' - ) { - return null; - } - if (!this.#workerPromise) { - // TODO(einbinder): Make workers send their console logs. - this.#workerPromise = ( - this.#session - ? Promise.resolve(this.#session) - : this.#sessionFactory(false) - ).then(client => { - return new WebWorker( - client, - this.#targetInfo.url, - () => {} /* consoleAPICalled */, - () => {} /* exceptionThrown */ - ); - }); - } - return this.#workerPromise; + return null; } url(): string { @@ -314,7 +292,9 @@ export class PageTarget extends Target { if (!this.pagePromise) { const session = this._session(); this.pagePromise = ( - session ? Promise.resolve(session) : this._sessionFactory()(true) + session + ? Promise.resolve(session) + : this._sessionFactory()(/* isAutoAttachEmulated=*/ false) ).then(client => { return CDPPage._create( client, @@ -338,3 +318,35 @@ export class PageTarget extends Target { } } } + +/** + * @internal + */ +export class WorkerTarget extends Target { + #workerPromise?: Promise; + + override async worker(): Promise { + if (!this.#workerPromise) { + const session = this._session(); + // TODO(einbinder): Make workers send their console logs. + this.#workerPromise = ( + session + ? Promise.resolve(session) + : this._sessionFactory()(/* isAutoAttachEmulated=*/ false) + ).then(client => { + return new WebWorker( + client, + this._getTargetInfo().url, + () => {} /* consoleAPICalled */, + () => {} /* exceptionThrown */ + ); + }); + } + return this.#workerPromise; + } +} + +/** + * @internal + */ +export class OtherTarget extends Target {}