diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 478e03dd17b..7cb844909e7 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -610,7 +610,7 @@ export class CDPBrowserContext extends BrowserContext { * * @returns Promise which resolves to an array of all open pages. * Non visible pages, such as `"background_page"`, will not be listed here. - * You can find them using {@link CDPTarget.page | the target page}. + * You can find them using {@link Target.page | the target page}. */ override async pages(): Promise { const pages = await Promise.all( diff --git a/packages/puppeteer-core/src/common/Page.ts b/packages/puppeteer-core/src/common/Page.ts index ba94fac4ed3..54cfbb07e1b 100644 --- a/packages/puppeteer-core/src/common/Page.ts +++ b/packages/puppeteer-core/src/common/Page.ts @@ -152,54 +152,38 @@ export class CDPPage extends Page { #frameManagerHandlers = new Map>([ [ FrameManagerEmittedEvents.FrameAttached, - event => { - return this.emit(PageEmittedEvents.FrameAttached, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameAttached), ], [ FrameManagerEmittedEvents.FrameDetached, - event => { - return this.emit(PageEmittedEvents.FrameDetached, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameDetached), ], [ FrameManagerEmittedEvents.FrameNavigated, - event => { - return this.emit(PageEmittedEvents.FrameNavigated, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameNavigated), ], ]); #networkManagerHandlers = new Map>([ [ NetworkManagerEmittedEvents.Request, - event => { - return this.emit(PageEmittedEvents.Request, event); - }, + this.emit.bind(this, PageEmittedEvents.Request), ], [ NetworkManagerEmittedEvents.RequestServedFromCache, - event => { - return this.emit(PageEmittedEvents.RequestServedFromCache, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestServedFromCache), ], [ NetworkManagerEmittedEvents.Response, - event => { - return this.emit(PageEmittedEvents.Response, event); - }, + this.emit.bind(this, PageEmittedEvents.Response), ], [ NetworkManagerEmittedEvents.RequestFailed, - event => { - return this.emit(PageEmittedEvents.RequestFailed, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestFailed), ], [ NetworkManagerEmittedEvents.RequestFinished, - event => { - return this.emit(PageEmittedEvents.RequestFinished, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestFinished), ], ]); @@ -214,70 +198,17 @@ export class CDPPage extends Page { ], [ 'Page.domContentEventFired', - () => { - return this.emit(PageEmittedEvents.DOMContentLoaded); - }, - ], - [ - 'Page.loadEventFired', - () => { - return this.emit(PageEmittedEvents.Load); - }, - ], - [ - 'Page.loadEventFired', - () => { - return this.emit(PageEmittedEvents.Load); - }, - ], - [ - 'Runtime.consoleAPICalled', - event => { - return this.#onConsoleAPI(event); - }, - ], - [ - 'Runtime.bindingCalled', - event => { - return this.#onBindingCalled(event); - }, - ], - [ - 'Page.javascriptDialogOpening', - event => { - return this.#onDialog(event); - }, - ], - [ - 'Runtime.exceptionThrown', - exception => { - return this.#handleException(exception.exceptionDetails); - }, - ], - [ - 'Inspector.targetCrashed', - () => { - return this.#onTargetCrashed(); - }, - ], - [ - 'Performance.metrics', - event => { - return this.#emitMetrics(event); - }, - ], - [ - 'Log.entryAdded', - event => { - return this.#onLogEntryAdded(event); - }, - ], - [ - 'Page.fileChooserOpened', - event => { - return this.#onFileChooser(event); - }, + this.emit.bind(this, PageEmittedEvents.DOMContentLoaded), ], + ['Page.loadEventFired', this.emit.bind(this, PageEmittedEvents.Load)], + ['Runtime.consoleAPICalled', this.#onConsoleAPI.bind(this)], + ['Runtime.bindingCalled', this.#onBindingCalled.bind(this)], + ['Page.javascriptDialogOpening', this.#onDialog.bind(this)], + ['Runtime.exceptionThrown', this.#handleException.bind(this)], + ['Inspector.targetCrashed', this.#onTargetCrashed.bind(this)], + ['Performance.metrics', this.#emitMetrics.bind(this)], + ['Log.entryAdded', this.#onLogEntryAdded.bind(this)], + ['Page.fileChooserOpened', this.#onFileChooser.bind(this)], ]); /** @@ -802,8 +733,11 @@ export class CDPPage extends Page { return result; } - #handleException(exceptionDetails: Protocol.Runtime.ExceptionDetails): void { - this.emit(PageEmittedEvents.PageError, createClientError(exceptionDetails)); + #handleException(exception: Protocol.Runtime.ExceptionThrownEvent): void { + this.emit( + PageEmittedEvents.PageError, + createClientError(exception.exceptionDetails) + ); } async #onConsoleAPI( diff --git a/packages/puppeteer-core/src/common/WebWorker.ts b/packages/puppeteer-core/src/common/WebWorker.ts index ba7a9627498..cda9df91d0b 100644 --- a/packages/puppeteer-core/src/common/WebWorker.ts +++ b/packages/puppeteer-core/src/common/WebWorker.ts @@ -38,7 +38,7 @@ export type ConsoleAPICalledCallback = ( * @internal */ export type ExceptionThrownCallback = ( - details: Protocol.Runtime.ExceptionDetails + event: Protocol.Runtime.ExceptionThrownEvent ) => void; /** @@ -69,7 +69,6 @@ export type ExceptionThrownCallback = ( */ export class WebWorker extends EventEmitter { #executionContext = Deferred.create(); - #client: CDPSession; #url: string; @@ -104,9 +103,7 @@ export class WebWorker extends EventEmitter { debugError(err); } }); - this.#client.on('Runtime.exceptionThrown', exception => { - return exceptionThrown(exception.exceptionDetails); - }); + this.#client.on('Runtime.exceptionThrown', exceptionThrown); // This might fail if the target is closed before we receive all execution contexts. this.#client.send('Runtime.enable').catch(debugError); diff --git a/packages/puppeteer-core/src/common/common.ts b/packages/puppeteer-core/src/common/common.ts index 3cdabf995da..643773fbba4 100644 --- a/packages/puppeteer-core/src/common/common.ts +++ b/packages/puppeteer-core/src/common/common.ts @@ -16,6 +16,7 @@ export * from './Accessibility.js'; export * from './AriaQueryHandler.js'; +export * from './Binding.js'; export * from './Browser.js'; export * from './BrowserConnector.js'; export * from './BrowserWebSocketTransport.js';