2020-10-12 09:08:57 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { ConnectionTransport } from './ConnectionTransport.js';
|
2022-05-13 06:04:46 +00:00
|
|
|
import {
|
|
|
|
Browser,
|
|
|
|
TargetFilterCallback,
|
|
|
|
IsPageTargetCallback,
|
|
|
|
} from './Browser.js';
|
2020-10-12 09:08:57 +00:00
|
|
|
import { assert } from './assert.js';
|
|
|
|
import { debugError } from '../common/helper.js';
|
|
|
|
import { Connection } from './Connection.js';
|
|
|
|
import { Viewport } from './PuppeteerViewport.js';
|
2020-10-19 09:32:41 +00:00
|
|
|
import { isNode } from '../environment.js';
|
2022-02-09 14:49:25 +00:00
|
|
|
import { getFetch } from './fetch.js';
|
2020-10-12 09:08:57 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-16 09:39:31 +00:00
|
|
|
* Generic browser options that can be passed when launching any browser or when
|
|
|
|
* connecting to an existing browser instance.
|
2020-10-12 09:08:57 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2021-02-16 09:39:31 +00:00
|
|
|
export interface BrowserConnectOptions {
|
|
|
|
/**
|
|
|
|
* Whether to ignore HTTPS errors during navigation.
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
2020-10-12 09:08:57 +00:00
|
|
|
ignoreHTTPSErrors?: boolean;
|
2021-02-16 09:39:31 +00:00
|
|
|
/**
|
|
|
|
* Sets the viewport for each page.
|
|
|
|
*/
|
2021-03-24 11:14:06 +00:00
|
|
|
defaultViewport?: Viewport | null;
|
2021-02-16 09:39:31 +00:00
|
|
|
/**
|
|
|
|
* Slows down Puppeteer operations by the specified amount of milliseconds to
|
|
|
|
* aid debugging.
|
|
|
|
*/
|
2020-10-12 09:08:57 +00:00
|
|
|
slowMo?: number;
|
2021-05-03 11:48:31 +00:00
|
|
|
/**
|
|
|
|
* Callback to decide if Puppeteer should connect to a given target or not.
|
|
|
|
*/
|
|
|
|
targetFilter?: TargetFilterCallback;
|
2022-05-13 06:04:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
isPageTarget?: IsPageTargetCallback;
|
2020-10-12 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 09:32:41 +00:00
|
|
|
const getWebSocketTransportClass = async () => {
|
|
|
|
return isNode
|
|
|
|
? (await import('../node/NodeWebSocketTransport.js')).NodeWebSocketTransport
|
|
|
|
: (await import('./BrowserWebSocketTransport.js'))
|
|
|
|
.BrowserWebSocketTransport;
|
|
|
|
};
|
|
|
|
|
2020-10-12 09:08:57 +00:00
|
|
|
/**
|
|
|
|
* Users should never call this directly; it's called when calling
|
|
|
|
* `puppeteer.connect`.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export const connectToBrowser = async (
|
2021-02-16 09:39:31 +00:00
|
|
|
options: BrowserConnectOptions & {
|
2020-10-12 09:08:57 +00:00
|
|
|
browserWSEndpoint?: string;
|
|
|
|
browserURL?: string;
|
|
|
|
transport?: ConnectionTransport;
|
|
|
|
}
|
2020-10-19 09:32:41 +00:00
|
|
|
): Promise<Browser> => {
|
2020-10-12 09:08:57 +00:00
|
|
|
const {
|
|
|
|
browserWSEndpoint,
|
|
|
|
browserURL,
|
|
|
|
ignoreHTTPSErrors = false,
|
|
|
|
defaultViewport = { width: 800, height: 600 },
|
|
|
|
transport,
|
|
|
|
slowMo = 0,
|
2021-05-03 11:48:31 +00:00
|
|
|
targetFilter,
|
2022-05-13 06:04:46 +00:00
|
|
|
isPageTarget,
|
2020-10-12 09:08:57 +00:00
|
|
|
} = options;
|
|
|
|
|
|
|
|
assert(
|
|
|
|
Number(!!browserWSEndpoint) + Number(!!browserURL) + Number(!!transport) ===
|
|
|
|
1,
|
|
|
|
'Exactly one of browserWSEndpoint, browserURL or transport must be passed to puppeteer.connect'
|
|
|
|
);
|
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
let connection!: Connection;
|
2020-10-12 09:08:57 +00:00
|
|
|
if (transport) {
|
|
|
|
connection = new Connection('', transport, slowMo);
|
|
|
|
} else if (browserWSEndpoint) {
|
2020-10-19 09:32:41 +00:00
|
|
|
const WebSocketClass = await getWebSocketTransportClass();
|
2021-05-12 14:48:30 +00:00
|
|
|
const connectionTransport: ConnectionTransport =
|
|
|
|
await WebSocketClass.create(browserWSEndpoint);
|
2020-10-12 09:08:57 +00:00
|
|
|
connection = new Connection(browserWSEndpoint, connectionTransport, slowMo);
|
|
|
|
} else if (browserURL) {
|
|
|
|
const connectionURL = await getWSEndpoint(browserURL);
|
2020-10-19 09:32:41 +00:00
|
|
|
const WebSocketClass = await getWebSocketTransportClass();
|
2021-05-12 14:48:30 +00:00
|
|
|
const connectionTransport: ConnectionTransport =
|
|
|
|
await WebSocketClass.create(connectionURL);
|
2020-10-12 09:08:57 +00:00
|
|
|
connection = new Connection(connectionURL, connectionTransport, slowMo);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { browserContextIds } = await connection.send(
|
|
|
|
'Target.getBrowserContexts'
|
|
|
|
);
|
|
|
|
return Browser.create(
|
|
|
|
connection,
|
|
|
|
browserContextIds,
|
|
|
|
ignoreHTTPSErrors,
|
|
|
|
defaultViewport,
|
2022-05-31 14:34:16 +00:00
|
|
|
undefined,
|
2021-05-03 11:48:31 +00:00
|
|
|
() => connection.send('Browser.close').catch(debugError),
|
2022-05-13 06:04:46 +00:00
|
|
|
targetFilter,
|
|
|
|
isPageTarget
|
2020-10-12 09:08:57 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getWSEndpoint(browserURL: string): Promise<string> {
|
|
|
|
const endpointURL = new URL('/json/version', browserURL);
|
|
|
|
|
|
|
|
const fetch = await getFetch();
|
|
|
|
try {
|
|
|
|
const result = await fetch(endpointURL.toString(), {
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
if (!result.ok) {
|
|
|
|
throw new Error(`HTTP ${result.statusText}`);
|
|
|
|
}
|
|
|
|
const data = await result.json();
|
|
|
|
return data.webSocketDebuggerUrl;
|
|
|
|
} catch (error) {
|
2022-05-31 14:34:16 +00:00
|
|
|
if (error instanceof Error) {
|
|
|
|
error.message =
|
|
|
|
`Failed to fetch browser webSocket URL from ${endpointURL}: ` +
|
|
|
|
error.message;
|
|
|
|
}
|
2020-10-12 09:08:57 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|