@cloudflare/puppeteer
Version:
A high-level API to control headless Chrome over the DevTools Protocol
106 lines (92 loc) • 2.74 kB
text/typescript
/**
* @license
* Copyright 2024 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import {
WEB_PERMISSION_TO_PROTOCOL_PERMISSION,
type Permission,
} from '../api/Browser.js';
import {BrowserContext} from '../api/BrowserContext.js';
import type {Page} from '../api/Page.js';
import {assert} from '../util/assert.js';
import type {CdpBrowser} from './Browser.js';
import type {Connection} from './Connection.js';
import type {CdpTarget} from './Target.js';
/**
* @internal
*/
export class CdpBrowserContext extends BrowserContext {
constructor(connection: Connection, browser: CdpBrowser, contextId?: string) {
super();
this.
this.
this.
}
override get id(): string | undefined {
return this.
}
override targets(): CdpTarget[] {
return this.
return target.browserContext() === this;
});
}
override async pages(): Promise<Page[]> {
const pages = await Promise.all(
this.targets()
.filter(target => {
return (
target.type() === 'page' ||
(target.type() === 'other' &&
this.
);
})
.map(target => {
return target.page();
})
);
return pages.filter((page): page is Page => {
return !!page;
});
}
override isIncognito(): boolean {
return !!this.
}
override async overridePermissions(
origin: string,
permissions: Permission[]
): Promise<void> {
const protocolPermissions = permissions.map(permission => {
const protocolPermission =
WEB_PERMISSION_TO_PROTOCOL_PERMISSION.get(permission);
if (!protocolPermission) {
throw new Error('Unknown permission: ' + permission);
}
return protocolPermission;
});
await this.
origin,
browserContextId: this.
permissions: protocolPermissions,
});
}
override async clearPermissionOverrides(): Promise<void> {
await this.
browserContextId: this.
});
}
override async newPage(): Promise<Page> {
using _guard = await this.waitForScreenshotOperations();
return await this.
}
override browser(): CdpBrowser {
return this.
}
override async close(): Promise<void> {
assert(this.
await this.
}
}