@cloudflare/puppeteer
Version:
A high-level API to control headless Chrome over the DevTools Protocol
267 lines (261 loc) • 3.45 kB
text/typescript
/**
* @license
* Copyright 2025 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
export const DEFAULT_VIEWPORT = Object.freeze({width: 800, height: 600});
import {CdpBrowser} from '../cdp/Browser.js';
import {Connection} from '../cdp/Connection.js';
import type {ConnectionTransport} from '../common/ConnectionTransport.js';
import type {
BrowserConnectOptions,
ConnectOptions,
} from '../common/ConnectOptions.js';
/**
* Users should never call this directly; it's called when calling
* `puppeteer.connect` with `protocol: 'cdp'`.
*
*
*/
export async function connectToCDPBrowser(
connectionTransport: ConnectionTransport,
options: BrowserConnectOptions & ConnectOptions & {sessionId?: string}
): Promise<CdpBrowser> {
const {
ignoreHTTPSErrors = false,
defaultViewport = DEFAULT_VIEWPORT,
targetFilter,
_isPageTarget: isPageTarget,
slowMo = 0,
protocolTimeout,
sessionId = 'unknown',
} = options;
const connection = new Connection(
'',
connectionTransport,
slowMo,
protocolTimeout
);
const version = await connection.send('Browser.getVersion');
const product = version.product.toLowerCase().includes('firefox')
? 'firefox'
: 'chrome';
const {browserContextIds} = await connection.send(
'Target.getBrowserContexts'
);
const browser = await CdpBrowser._create(
product || 'chrome',
connection,
browserContextIds,
ignoreHTTPSErrors,
defaultViewport,
undefined,
() => {
return connection.send('Browser.close').catch(console.log);
},
targetFilter,
isPageTarget,
true,
sessionId
);
return browser;
}
/**
* @public
*/
export type Locations =
| 'AF'
| 'AL'
| 'DZ'
| 'AD'
| 'AO'
| 'AG'
| 'AR'
| 'AM'
| 'AU'
| 'AT'
| 'AZ'
| 'BH'
| 'BD'
| 'BB'
| 'BY'
| 'BE'
| 'BZ'
| 'BJ'
| 'BM'
| 'BT'
| 'BO'
| 'BA'
| 'BW'
| 'BR'
| 'BN'
| 'BG'
| 'BF'
| 'BI'
| 'KH'
| 'CM'
| 'CA'
| 'CV'
| 'KY'
| 'CF'
| 'TD'
| 'CL'
| 'CN'
|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