diff --git a/new-docs/puppeteer.browsercontext.md b/new-docs/puppeteer.browsercontext.md index 7cf0d65392a23..d3df508ba9da3 100644 --- a/new-docs/puppeteer.browsercontext.md +++ b/new-docs/puppeteer.browsercontext.md @@ -15,6 +15,8 @@ export declare class BrowserContext extends EventEmitter ## Remarks +The Browser class extends from Puppeteer's [EventEmitter](./puppeteer.eventemitter.md) class and will emit various events which are documented in the [BrowserContextEmittedEvents](./puppeteer.browsercontextemittedevents.md) enum. + If a page opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's browser context. Puppeteer allows creation of "incognito" browser contexts with [Browser.createIncognitoBrowserContext](./puppeteer.browser.createincognitobrowsercontext.md) method. "Incognito" browser contexts don't write any browsing data to disk. diff --git a/new-docs/puppeteer.browsercontextemittedevents.md b/new-docs/puppeteer.browsercontextemittedevents.md new file mode 100644 index 0000000000000..3929ad587b7ae --- /dev/null +++ b/new-docs/puppeteer.browsercontextemittedevents.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [puppeteer](./puppeteer.md) > [BrowserContextEmittedEvents](./puppeteer.browsercontextemittedevents.md) + +## BrowserContextEmittedEvents enum + +Signature: + +```typescript +export declare const enum BrowserContextEmittedEvents +``` + +## Enumeration Members + +| Member | Value | Description | +| --- | --- | --- | +| TargetChanged | "targetchanged" | Emitted when the url of a target inside the browser context changes. Contains a [Target](./puppeteer.target.md) instance. | +| TargetCreated | "targetcreated" | Emitted when a target is created within the browser context, for example when a new page is opened by [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) or by [browserContext.newPage](./puppeteer.browsercontext.newpage.md)Contains a [Target](./puppeteer.target.md) instance. | +| TargetDestroyed | "targetdestroyed" | Emitted when a target is destroyed within the browser context, for example when a page is closed. Contains a [Target](./puppeteer.target.md) instance. | + diff --git a/new-docs/puppeteer.md b/new-docs/puppeteer.md index c296a75ea1099..ff7dc9b072525 100644 --- a/new-docs/puppeteer.md +++ b/new-docs/puppeteer.md @@ -40,6 +40,7 @@ | Enumeration | Description | | --- | --- | +| [BrowserContextEmittedEvents](./puppeteer.browsercontextemittedevents.md) | | | [BrowserEmittedEvents](./puppeteer.browseremittedevents.md) | All the events a [browser instance](./puppeteer.browser.md) may emit. | | [PageEmittedEvents](./puppeteer.pageemittedevents.md) | All the events that a page instance may emit. | diff --git a/src/common/Browser.ts b/src/common/Browser.ts index f82ea91af5b1c..52d13e7c209e0 100644 --- a/src/common/Browser.ts +++ b/src/common/Browser.ts @@ -297,7 +297,7 @@ export class Browser extends EventEmitter { if (await target._initializedPromise) { this.emit(BrowserEmittedEvents.TargetCreated, target); - context.emit(Events.BrowserContext.TargetCreated, target); + context.emit(BrowserContextEmittedEvents.TargetCreated, target); } } @@ -310,7 +310,7 @@ export class Browser extends EventEmitter { this.emit(BrowserEmittedEvents.TargetDestroyed, target); target .browserContext() - .emit(Events.BrowserContext.TargetDestroyed, target); + .emit(BrowserContextEmittedEvents.TargetDestroyed, target); } } @@ -324,7 +324,9 @@ export class Browser extends EventEmitter { target._targetInfoChanged(event.targetInfo); if (wasInitialized && previousURL !== target.url()) { this.emit(BrowserEmittedEvents.TargetChanged, target); - target.browserContext().emit(Events.BrowserContext.TargetChanged, target); + target + .browserContext() + .emit(BrowserContextEmittedEvents.TargetChanged, target); } } @@ -504,16 +506,43 @@ export class Browser extends EventEmitter { } } +export const enum BrowserContextEmittedEvents { + /** + * Emitted when the url of a target inside the browser context changes. + * Contains a {@link Target} instance. + */ + TargetChanged = 'targetchanged', + + /** + * Emitted when a target is created within the browser context, for example + * when a new page is opened by + * {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/open | window.open} + * or by {@link BrowserContext.newPage | browserContext.newPage} + * + * Contains a {@link Target} instance. + */ + TargetCreated = 'targetcreated', + /** + * Emitted when a target is destroyed within the browser context, for example + * when a page is closed. Contains a {@link Target} instance. + */ + TargetDestroyed = 'targetdestroyed', +} + /** - * BrowserContexts provide a way to operate multiple independent browser sessions. - * When a browser is launched, it has a single BrowserContext used by default. - * The method {@link Browser.newPage | Browser.newPage} creates a page + * BrowserContexts provide a way to operate multiple independent browser + * sessions. When a browser is launched, it has a single BrowserContext used by + * default. The method {@link Browser.newPage | Browser.newPage} creates a page * in the default browser context. * * @remarks * - * If a page opens another page, e.g. with a `window.open` call, - * the popup will belong to the parent page's browser context. + * The Browser class extends from Puppeteer's {@link EventEmitter} class and + * will emit various events which are documented in the + * {@link BrowserContextEmittedEvents} enum. + * + * If a page opens another page, e.g. with a `window.open` call, the popup will + * belong to the parent page's browser context. * * Puppeteer allows creation of "incognito" browser contexts with * {@link Browser.createIncognitoBrowserContext | Browser.createIncognitoBrowserContext}