2017-05-11 07:06:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2018-05-31 23:53:51 +00:00
|
|
|
const { helper, assert } = require('./helper');
|
2018-08-06 18:31:33 +00:00
|
|
|
const {Target} = require('./Target');
|
2017-09-14 04:27:14 +00:00
|
|
|
const EventEmitter = require('events');
|
2018-08-06 18:31:33 +00:00
|
|
|
const {TaskQueue} = require('./TaskQueue');
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-09-14 04:27:14 +00:00
|
|
|
class Browser extends EventEmitter {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Connection} connection
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Array<string>} contextIds
|
2018-05-17 01:09:59 +00:00
|
|
|
* @param {boolean} ignoreHTTPSErrors
|
2018-08-01 23:23:03 +00:00
|
|
|
* @param {?Puppeteer.Viewport} defaultViewport
|
2017-12-11 20:11:12 +00:00
|
|
|
* @param {?Puppeteer.ChildProcess} process
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {(function():Promise)=} closeCallback
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2018-08-01 23:23:03 +00:00
|
|
|
constructor(connection, contextIds, ignoreHTTPSErrors, defaultViewport, process, closeCallback) {
|
2017-09-14 04:27:14 +00:00
|
|
|
super();
|
2018-05-17 01:09:59 +00:00
|
|
|
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
2018-08-01 23:23:03 +00:00
|
|
|
this._defaultViewport = defaultViewport;
|
2017-12-11 20:11:12 +00:00
|
|
|
this._process = process;
|
2017-07-19 05:10:38 +00:00
|
|
|
this._screenshotTaskQueue = new TaskQueue();
|
2017-08-15 01:08:06 +00:00
|
|
|
this._connection = connection;
|
|
|
|
this._closeCallback = closeCallback || new Function();
|
2018-05-10 20:26:08 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
this._defaultContext = new BrowserContext(this._connection, this, null);
|
2018-05-10 20:26:08 +00:00
|
|
|
/** @type {Map<string, BrowserContext>} */
|
|
|
|
this._contexts = new Map();
|
|
|
|
for (const contextId of contextIds)
|
2018-08-30 22:36:09 +00:00
|
|
|
this._contexts.set(contextId, new BrowserContext(this._connection, this, contextId));
|
2018-05-10 20:26:08 +00:00
|
|
|
|
2017-10-18 02:14:57 +00:00
|
|
|
/** @type {Map<string, Target>} */
|
|
|
|
this._targets = new Map();
|
2017-11-04 01:46:17 +00:00
|
|
|
this._connection.setClosedCallback(() => {
|
|
|
|
this.emit(Browser.Events.Disconnected);
|
|
|
|
});
|
2017-10-18 02:14:57 +00:00
|
|
|
this._connection.on('Target.targetCreated', this._targetCreated.bind(this));
|
|
|
|
this._connection.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
|
|
|
|
this._connection.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @return {?Puppeteer.ChildProcess}
|
|
|
|
*/
|
|
|
|
process() {
|
|
|
|
return this._process;
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!BrowserContext>}
|
|
|
|
*/
|
|
|
|
async createIncognitoBrowserContext() {
|
|
|
|
const {browserContextId} = await this._connection.send('Target.createBrowserContext');
|
2018-08-30 22:36:09 +00:00
|
|
|
const context = new BrowserContext(this._connection, this, browserContextId);
|
2018-05-10 20:26:08 +00:00
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Array<!BrowserContext>}
|
|
|
|
*/
|
|
|
|
browserContexts() {
|
|
|
|
return [this._defaultContext, ...Array.from(this._contexts.values())];
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
/**
|
|
|
|
* @return {!BrowserContext}
|
|
|
|
*/
|
|
|
|
defaultBrowserContext() {
|
|
|
|
return this._defaultContext;
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
/**
|
|
|
|
* @param {?string} contextId
|
|
|
|
*/
|
|
|
|
async _disposeContext(contextId) {
|
|
|
|
await this._connection.send('Target.disposeBrowserContext', {browserContextId: contextId || undefined});
|
|
|
|
this._contexts.delete(contextId);
|
|
|
|
}
|
|
|
|
|
2017-10-18 02:14:57 +00:00
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Connection} connection
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Array<string>} contextIds
|
2018-05-17 01:09:59 +00:00
|
|
|
* @param {boolean} ignoreHTTPSErrors
|
2018-08-01 23:23:03 +00:00
|
|
|
* @param {?Puppeteer.Viewport} defaultViewport
|
2017-12-11 20:11:12 +00:00
|
|
|
* @param {?Puppeteer.ChildProcess} process
|
2017-10-18 02:14:57 +00:00
|
|
|
* @param {function()=} closeCallback
|
|
|
|
*/
|
2018-08-01 23:23:03 +00:00
|
|
|
static async create(connection, contextIds, ignoreHTTPSErrors, defaultViewport, process, closeCallback) {
|
|
|
|
const browser = new Browser(connection, contextIds, ignoreHTTPSErrors, defaultViewport, process, closeCallback);
|
2017-10-18 02:14:57 +00:00
|
|
|
await connection.send('Target.setDiscoverTargets', {discover: true});
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Protocol.Target.targetCreatedPayload} event
|
2017-10-18 02:14:57 +00:00
|
|
|
*/
|
|
|
|
async _targetCreated(event) {
|
2018-02-26 20:10:06 +00:00
|
|
|
const targetInfo = event.targetInfo;
|
2018-05-10 20:26:08 +00:00
|
|
|
const {browserContextId} = targetInfo;
|
|
|
|
const context = (browserContextId && this._contexts.has(browserContextId)) ? this._contexts.get(browserContextId) : this._defaultContext;
|
|
|
|
|
2018-08-01 23:23:03 +00:00
|
|
|
const target = new Target(targetInfo, context, () => this._connection.createSession(targetInfo), this._ignoreHTTPSErrors, this._defaultViewport, this._screenshotTaskQueue);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
|
2017-10-18 02:14:57 +00:00
|
|
|
this._targets.set(event.targetInfo.targetId, target);
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
if (await target._initializedPromise) {
|
2017-10-18 02:14:57 +00:00
|
|
|
this.emit(Browser.Events.TargetCreated, target);
|
2018-05-10 20:26:08 +00:00
|
|
|
context.emit(BrowserContext.Events.TargetCreated, target);
|
|
|
|
}
|
2017-10-18 02:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {{targetId: string}} event
|
|
|
|
*/
|
|
|
|
async _targetDestroyed(event) {
|
|
|
|
const target = this._targets.get(event.targetId);
|
|
|
|
target._initializedCallback(false);
|
|
|
|
this._targets.delete(event.targetId);
|
2018-02-22 00:08:29 +00:00
|
|
|
target._closedCallback();
|
2018-05-10 20:26:08 +00:00
|
|
|
if (await target._initializedPromise) {
|
2017-10-18 02:14:57 +00:00
|
|
|
this.emit(Browser.Events.TargetDestroyed, target);
|
2018-05-10 20:26:08 +00:00
|
|
|
target.browserContext().emit(BrowserContext.Events.TargetDestroyed, target);
|
|
|
|
}
|
2017-10-18 02:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Protocol.Target.targetInfoChangedPayload} event
|
2017-10-18 02:14:57 +00:00
|
|
|
*/
|
|
|
|
_targetInfoChanged(event) {
|
|
|
|
const target = this._targets.get(event.targetInfo.targetId);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(target, 'target should exist before targetInfoChanged');
|
2018-02-26 20:10:06 +00:00
|
|
|
const previousURL = target.url();
|
|
|
|
const wasInitialized = target._isInitialized;
|
2017-10-18 02:14:57 +00:00
|
|
|
target._targetInfoChanged(event.targetInfo);
|
2018-05-10 20:26:08 +00:00
|
|
|
if (wasInitialized && previousURL !== target.url()) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this.emit(Browser.Events.TargetChanged, target);
|
2018-05-10 20:26:08 +00:00
|
|
|
target.browserContext().emit(BrowserContext.Events.TargetChanged, target);
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-08-15 21:29:42 +00:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-08-16 08:10:55 +00:00
|
|
|
wsEndpoint() {
|
2017-08-15 21:29:42 +00:00
|
|
|
return this._connection.url();
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-02-26 20:10:06 +00:00
|
|
|
* @return {!Promise<!Puppeteer.Page>}
|
2017-07-25 07:44:13 +00:00
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async newPage() {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._defaultContext.newPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-24 22:17:36 +00:00
|
|
|
* @param {?string} contextId
|
2018-05-10 20:26:08 +00:00
|
|
|
* @return {!Promise<!Puppeteer.Page>}
|
|
|
|
*/
|
|
|
|
async _createPageInContext(contextId) {
|
|
|
|
const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank', browserContextId: contextId || undefined});
|
2017-10-18 02:14:57 +00:00
|
|
|
const target = await this._targets.get(targetId);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(await target._initializedPromise, 'Failed to create target for page');
|
2017-10-18 02:14:57 +00:00
|
|
|
const page = await target.page();
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Array<!Target>}
|
|
|
|
*/
|
|
|
|
targets() {
|
|
|
|
return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-26 20:10:06 +00:00
|
|
|
* @return {!Promise<!Array<!Puppeteer.Page>>}
|
2017-10-18 02:14:57 +00:00
|
|
|
*/
|
|
|
|
async pages() {
|
2018-07-31 20:24:29 +00:00
|
|
|
const contextPages = await Promise.all(this.browserContexts().map(context => context.pages()));
|
|
|
|
// Flatten array.
|
|
|
|
return contextPages.reduce((acc, x) => acc.concat(x), []);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-26 03:39:50 +00:00
|
|
|
* @return {!Promise<string>}
|
2017-07-25 07:44:13 +00:00
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async version() {
|
2018-01-05 18:14:30 +00:00
|
|
|
const version = await this._getVersion();
|
2017-08-13 05:04:39 +00:00
|
|
|
return version.product;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2018-01-05 18:14:30 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async userAgent() {
|
|
|
|
const version = await this._getVersion();
|
|
|
|
return version.userAgent;
|
|
|
|
}
|
|
|
|
|
2017-09-14 04:27:14 +00:00
|
|
|
async close() {
|
|
|
|
await this._closeCallback.call(null);
|
2017-10-17 22:35:00 +00:00
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this._connection.dispose();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2018-01-05 18:14:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<!Object>}
|
|
|
|
*/
|
|
|
|
_getVersion() {
|
|
|
|
return this._connection.send('Browser.getVersion');
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 02:14:57 +00:00
|
|
|
/** @enum {string} */
|
|
|
|
Browser.Events = {
|
|
|
|
TargetCreated: 'targetcreated',
|
|
|
|
TargetDestroyed: 'targetdestroyed',
|
2017-11-04 01:46:17 +00:00
|
|
|
TargetChanged: 'targetchanged',
|
|
|
|
Disconnected: 'disconnected'
|
2017-10-18 02:14:57 +00:00
|
|
|
};
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
class BrowserContext extends EventEmitter {
|
|
|
|
/**
|
2018-08-30 22:36:09 +00:00
|
|
|
* @param {!Puppeteer.Connection} connection
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Browser} browser
|
|
|
|
* @param {?string} contextId
|
|
|
|
*/
|
2018-08-30 22:36:09 +00:00
|
|
|
constructor(connection, browser, contextId) {
|
2018-05-10 20:26:08 +00:00
|
|
|
super();
|
2018-08-30 22:36:09 +00:00
|
|
|
this._connection = connection;
|
2018-05-10 20:26:08 +00:00
|
|
|
this._browser = browser;
|
|
|
|
this._id = contextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Array<!Target>} target
|
|
|
|
*/
|
|
|
|
targets() {
|
|
|
|
return this._browser.targets().filter(target => target.browserContext() === this);
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:24:29 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Array<!Puppeteer.Page>>}
|
|
|
|
*/
|
|
|
|
async pages() {
|
|
|
|
const pages = await Promise.all(
|
|
|
|
this.targets()
|
|
|
|
.filter(target => target.type() === 'page')
|
|
|
|
.map(target => target.page())
|
|
|
|
);
|
|
|
|
return pages.filter(page => !!page);
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isIncognito() {
|
|
|
|
return !!this._id;
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
/**
|
|
|
|
* @param {string} origin
|
|
|
|
* @param {!Array<string>} permissions
|
|
|
|
*/
|
|
|
|
async overridePermissions(origin, permissions) {
|
|
|
|
const webPermissionToProtocol = new Map([
|
|
|
|
['geolocation', 'geolocation'],
|
|
|
|
['midi', 'midi'],
|
|
|
|
['notifications', 'notifications'],
|
|
|
|
['push', 'push'],
|
|
|
|
['camera', 'videoCapture'],
|
|
|
|
['microphone', 'audioCapture'],
|
|
|
|
['background-sync', 'backgroundSync'],
|
|
|
|
['ambient-light-sensor', 'sensors'],
|
|
|
|
['accelerometer', 'sensors'],
|
|
|
|
['gyroscope', 'sensors'],
|
|
|
|
['magnetometer', 'sensors'],
|
|
|
|
['accessibility-events', 'accessibilityEvents'],
|
|
|
|
['clipboard-read', 'clipboardRead'],
|
|
|
|
['clipboard-write', 'clipboardWrite'],
|
|
|
|
['payment-handler', 'paymentHandler'],
|
|
|
|
// chrome-specific permissions we have.
|
|
|
|
['midi-sysex', 'midiSysex'],
|
|
|
|
]);
|
|
|
|
permissions = permissions.map(permission => {
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
if (!protocolPermission)
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
return protocolPermission;
|
|
|
|
});
|
|
|
|
await this._connection.send('Browser.grantPermissions', {origin, browserContextId: this._id || undefined, permissions});
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissionOverrides() {
|
|
|
|
await this._connection.send('Browser.resetPermissions', {browserContextId: this._id || undefined});
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Puppeteer.Page>}
|
|
|
|
*/
|
|
|
|
newPage() {
|
|
|
|
return this._browser._createPageInContext(this._id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Browser}
|
|
|
|
*/
|
|
|
|
browser() {
|
|
|
|
return this._browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(this._id, 'Non-incognito profiles cannot be closed!');
|
2018-05-10 20:26:08 +00:00
|
|
|
await this._browser._disposeContext(this._id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @enum {string} */
|
|
|
|
BrowserContext.Events = {
|
|
|
|
TargetCreated: 'targetcreated',
|
|
|
|
TargetDestroyed: 'targetdestroyed',
|
|
|
|
TargetChanged: 'targetchanged',
|
|
|
|
};
|
|
|
|
|
|
|
|
helper.tracePublicAPI(BrowserContext);
|
2017-07-19 03:53:00 +00:00
|
|
|
helper.tracePublicAPI(Browser);
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
module.exports = {Browser, BrowserContext};
|