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.
|
|
|
|
*/
|
|
|
|
|
2017-09-11 23:21:51 +00:00
|
|
|
const {helper} = require('./helper');
|
2017-08-02 19:06:47 +00:00
|
|
|
const Page = require('./Page');
|
2017-09-14 04:27:14 +00:00
|
|
|
const EventEmitter = require('events');
|
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
|
2017-09-29 22:16:32 +00:00
|
|
|
* @param {!Object=} options
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {(function():Promise)=} closeCallback
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-09-29 22:16:32 +00:00
|
|
|
constructor(connection, options = {}, closeCallback) {
|
2017-09-14 04:27:14 +00:00
|
|
|
super();
|
2017-09-29 22:16:32 +00:00
|
|
|
this._ignoreHTTPSErrors = !!options.ignoreHTTPSErrors;
|
|
|
|
this._appMode = !!options.appMode;
|
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();
|
2017-10-18 02:14:57 +00:00
|
|
|
/** @type {Map<string, Target>} */
|
|
|
|
this._targets = new Map();
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Connection} connection
|
|
|
|
* @param {boolean} ignoreHTTPSErrors
|
|
|
|
* @param {function()=} closeCallback
|
|
|
|
*/
|
|
|
|
static async create(connection, ignoreHTTPSErrors, closeCallback) {
|
|
|
|
const browser = new Browser(connection, ignoreHTTPSErrors, closeCallback);
|
|
|
|
await connection.send('Target.setDiscoverTargets', {discover: true});
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {{targetInfo: !Target.TargetInfo}} event
|
|
|
|
*/
|
|
|
|
async _targetCreated(event) {
|
|
|
|
const target = new Target(this, event.targetInfo);
|
|
|
|
console.assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
|
|
|
|
this._targets.set(event.targetInfo.targetId, target);
|
|
|
|
|
|
|
|
if (await target._initializedPromise)
|
|
|
|
this.emit(Browser.Events.TargetCreated, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {{targetId: string}} event
|
|
|
|
*/
|
|
|
|
async _targetDestroyed(event) {
|
|
|
|
const target = this._targets.get(event.targetId);
|
|
|
|
target._initializedCallback(false);
|
|
|
|
this._targets.delete(event.targetId);
|
|
|
|
if (await target._initializedPromise)
|
|
|
|
this.emit(Browser.Events.TargetDestroyed, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {{targetInfo: !Target.TargetInfo}} event
|
|
|
|
*/
|
|
|
|
_targetInfoChanged(event) {
|
|
|
|
const target = this._targets.get(event.targetInfo.targetId);
|
|
|
|
console.assert(target, 'target should exist before targetInfoChanged');
|
|
|
|
target._targetInfoChanged(event.targetInfo);
|
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
|
|
|
/**
|
2017-07-25 07:44:13 +00:00
|
|
|
* @return {!Promise<!Page>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async newPage() {
|
2017-08-09 23:14:00 +00:00
|
|
|
const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank'});
|
2017-10-18 02:14:57 +00:00
|
|
|
const target = await this._targets.get(targetId);
|
|
|
|
console.assert(await target._initializedPromise, 'Failed to create target for page');
|
|
|
|
const page = await target.page();
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Array<!Target>}
|
|
|
|
*/
|
|
|
|
targets() {
|
|
|
|
return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<!Array<!Page>>}
|
|
|
|
*/
|
|
|
|
async pages() {
|
|
|
|
const pages = await Promise.all(this.targets().map(target => target.page()));
|
|
|
|
return pages.filter(page => !!page);
|
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() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const version = await this._connection.send('Browser.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
|
|
|
|
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
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 02:14:57 +00:00
|
|
|
/** @enum {string} */
|
|
|
|
Browser.Events = {
|
|
|
|
TargetCreated: 'targetcreated',
|
|
|
|
TargetDestroyed: 'targetdestroyed',
|
|
|
|
TargetChanged: 'targetchanged'
|
|
|
|
};
|
|
|
|
|
2017-07-19 03:53:00 +00:00
|
|
|
helper.tracePublicAPI(Browser);
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-07-19 05:10:38 +00:00
|
|
|
class TaskQueue {
|
|
|
|
constructor() {
|
|
|
|
this._chain = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-26 03:39:50 +00:00
|
|
|
* @param {function()} task
|
2017-07-19 05:10:38 +00:00
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
postTask(task) {
|
2017-07-26 03:39:50 +00:00
|
|
|
const result = this._chain.then(task);
|
2017-07-19 05:10:38 +00:00
|
|
|
this._chain = result.catch(() => {});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
class Target {
|
|
|
|
/**
|
|
|
|
* @param {!Browser} browser
|
|
|
|
* @param {!Target.TargetInfo} targetInfo
|
|
|
|
*/
|
|
|
|
constructor(browser, targetInfo) {
|
|
|
|
this._browser = browser;
|
|
|
|
this._targetInfo = targetInfo;
|
|
|
|
/** @type {?Promise<!Page>} */
|
|
|
|
this._pagePromise = null;
|
|
|
|
this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill);
|
|
|
|
this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
|
|
|
|
if (this._isInitialized)
|
|
|
|
this._initializedCallback(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<?Page>}
|
|
|
|
*/
|
|
|
|
async page() {
|
|
|
|
if (this._targetInfo.type === 'page' && !this._pagePromise) {
|
|
|
|
this._pagePromise = this._browser._connection.createSession(this._targetInfo.targetId)
|
|
|
|
.then(client => Page.create(client, this._browser._ignoreHTTPSErrors, this._browser._appMode, this._browser._screenshotTaskQueue));
|
|
|
|
}
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._targetInfo.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {"page"|"service_worker"|"other"}
|
|
|
|
*/
|
|
|
|
type() {
|
|
|
|
const type = this._targetInfo.type;
|
|
|
|
if (type === 'page' || type === 'service_worker')
|
|
|
|
return type;
|
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Target.TargetInfo} targetInfo
|
|
|
|
*/
|
|
|
|
_targetInfoChanged(targetInfo) {
|
|
|
|
const previousURL = this._targetInfo.url;
|
|
|
|
this._targetInfo = targetInfo;
|
|
|
|
|
|
|
|
if (!this._isInitialized && (this._targetInfo.type !== 'page' || this._targetInfo.url !== '')) {
|
|
|
|
this._isInitialized = true;
|
|
|
|
this._initializedCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousURL !== targetInfo.url)
|
|
|
|
this._browser.emit(Browser.Events.TargetChanged, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
helper.tracePublicAPI(Target);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} Target.TargetInfo
|
|
|
|
* @property {string} type
|
|
|
|
* @property {string} targetId
|
|
|
|
* @property {string} title
|
|
|
|
* @property {string} url
|
|
|
|
* @property {boolean} attached
|
|
|
|
*/
|
|
|
|
|
2017-10-10 05:31:40 +00:00
|
|
|
module.exports = { Browser, TaskQueue };
|