diff --git a/src/common/FrameManager.ts b/src/common/FrameManager.ts index a50ac3b4..bf943afe 100644 --- a/src/common/FrameManager.ts +++ b/src/common/FrameManager.ts @@ -17,7 +17,6 @@ import { EventEmitter } from './EventEmitter'; import { assert } from './assert'; import { helper, debugError } from './helper'; -import { Events } from './Events'; import { ExecutionContext, EVALUATION_SCRIPT_URL } from './ExecutionContext'; import { LifecycleWatcher, PuppeteerLifeCycleEvent } from './LifecycleWatcher'; import { DOMWorld, WaitForSelectorOptions } from './DOMWorld'; @@ -37,6 +36,24 @@ import { const UTILITY_WORLD_NAME = '__puppeteer_utility_world__'; +/** + * We use symbols to prevent external parties listening to these events. + * They are internal to Puppeteer. + * + * @internal + */ +export const FrameManagerEmittedEvents = { + FrameAttached: Symbol('FrameManager.FrameAttached'), + FrameNavigated: Symbol('FrameManager.FrameNavigated'), + FrameDetached: Symbol('FrameManager.FrameDetached'), + LifecycleEvent: Symbol('FrameManager.LifecycleEvent'), + FrameNavigatedWithinDocument: Symbol( + 'FrameManager.FrameNavigatedWithinDocument' + ), + ExecutionContextCreated: Symbol('FrameManager.ExecutionContextCreated'), + ExecutionContextDestroyed: Symbol('FrameManager.ExecutionContextDestroyed'), +}; + /** * @internal */ @@ -197,14 +214,14 @@ export class FrameManager extends EventEmitter { const frame = this._frames.get(event.frameId); if (!frame) return; frame._onLifecycleEvent(event.loaderId, event.name); - this.emit(Events.FrameManager.LifecycleEvent, frame); + this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame); } _onFrameStoppedLoading(frameId: string): void { const frame = this._frames.get(frameId); if (!frame) return; frame._onLoadingStopped(); - this.emit(Events.FrameManager.LifecycleEvent, frame); + this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame); } _handleFrameTree(frameTree: Protocol.Page.FrameTree): void { @@ -238,7 +255,7 @@ export class FrameManager extends EventEmitter { const parentFrame = this._frames.get(parentFrameId); const frame = new Frame(this, parentFrame, frameId); this._frames.set(frame._id, frame); - this.emit(Events.FrameManager.FrameAttached, frame); + this.emit(FrameManagerEmittedEvents.FrameAttached, frame); } _onFrameNavigated(framePayload: Protocol.Page.Frame): void { @@ -274,7 +291,7 @@ export class FrameManager extends EventEmitter { // Update frame payload. frame._navigated(framePayload); - this.emit(Events.FrameManager.FrameNavigated, frame); + this.emit(FrameManagerEmittedEvents.FrameNavigated, frame); } async _ensureIsolatedWorld(name: string): Promise { @@ -301,8 +318,8 @@ export class FrameManager extends EventEmitter { const frame = this._frames.get(frameId); if (!frame) return; frame._navigatedWithinDocument(url); - this.emit(Events.FrameManager.FrameNavigatedWithinDocument, frame); - this.emit(Events.FrameManager.FrameNavigated, frame); + this.emit(FrameManagerEmittedEvents.FrameNavigatedWithinDocument, frame); + this.emit(FrameManagerEmittedEvents.FrameNavigated, frame); } _onFrameDetached(frameId: string): void { @@ -362,7 +379,7 @@ export class FrameManager extends EventEmitter { this._removeFramesRecursively(child); frame._detach(); this._frames.delete(frame._id); - this.emit(Events.FrameManager.FrameDetached, frame); + this.emit(FrameManagerEmittedEvents.FrameDetached, frame); } } diff --git a/src/common/LifecycleWatcher.ts b/src/common/LifecycleWatcher.ts index a1a6bb4a..32787c60 100644 --- a/src/common/LifecycleWatcher.ts +++ b/src/common/LifecycleWatcher.ts @@ -18,7 +18,7 @@ import { assert } from './assert'; import { helper, PuppeteerEventListener } from './helper'; import { Events } from './Events'; import { TimeoutError } from './Errors'; -import { FrameManager, Frame } from './FrameManager'; +import { FrameManager, Frame, FrameManagerEmittedEvents } from './FrameManager'; import { HTTPRequest } from './HTTPRequest'; import { HTTPResponse } from './HTTPResponse'; import { NetworkManagerEmittedEvents } from './NetworkManager'; @@ -103,17 +103,17 @@ export class LifecycleWatcher { ), helper.addEventListener( this._frameManager, - Events.FrameManager.LifecycleEvent, + FrameManagerEmittedEvents.LifecycleEvent, this._checkLifecycleComplete.bind(this) ), helper.addEventListener( this._frameManager, - Events.FrameManager.FrameNavigatedWithinDocument, + FrameManagerEmittedEvents.FrameNavigatedWithinDocument, this._navigatedWithinDocument.bind(this) ), helper.addEventListener( this._frameManager, - Events.FrameManager.FrameDetached, + FrameManagerEmittedEvents.FrameDetached, this._onFrameDetached.bind(this) ), helper.addEventListener( diff --git a/src/common/Page.ts b/src/common/Page.ts index a927308c..51598153 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -22,7 +22,7 @@ import { Events } from './Events'; import { Connection, CDPSession } from './Connection'; import { Dialog } from './Dialog'; import { EmulationManager } from './EmulationManager'; -import { Frame, FrameManager } from './FrameManager'; +import { Frame, FrameManager, FrameManagerEmittedEvents } from './FrameManager'; import { Keyboard, Mouse, Touchscreen, MouseButton } from './Input'; import { Tracing } from './Tracing'; import { assert } from './assert'; @@ -475,13 +475,13 @@ export class Page extends EventEmitter { this._workers.delete(event.sessionId); }); - this._frameManager.on(Events.FrameManager.FrameAttached, (event) => + this._frameManager.on(FrameManagerEmittedEvents.FrameAttached, (event) => this.emit(PageEmittedEvents.FrameAttached, event) ); - this._frameManager.on(Events.FrameManager.FrameDetached, (event) => + this._frameManager.on(FrameManagerEmittedEvents.FrameDetached, (event) => this.emit(PageEmittedEvents.FrameDetached, event) ); - this._frameManager.on(Events.FrameManager.FrameNavigated, (event) => + this._frameManager.on(FrameManagerEmittedEvents.FrameNavigated, (event) => this.emit(PageEmittedEvents.FrameNavigated, event) );