2019-02-01 22:09:18 +00:00
|
|
|
const {helper, assert} = require('./helper');
|
2018-12-06 19:24:00 +00:00
|
|
|
const {Page} = require('./Page');
|
2019-02-08 02:03:48 +00:00
|
|
|
const {Events} = require('./Events');
|
2018-12-06 19:24:00 +00:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
|
|
|
class Browser extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Connection} connection
|
|
|
|
* @param {?Puppeteer.Viewport} defaultViewport
|
|
|
|
* @param {?Puppeteer.ChildProcess} process
|
|
|
|
* @param {function():void} closeCallback
|
|
|
|
*/
|
2019-02-01 22:09:18 +00:00
|
|
|
static async create(connection, defaultViewport, process, closeCallback) {
|
|
|
|
const {browserContextIds} = await connection.send('Browser.getBrowserContexts');
|
|
|
|
const browser = new Browser(connection, browserContextIds, defaultViewport, process, closeCallback);
|
|
|
|
await connection.send('Browser.enable');
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Connection} connection
|
|
|
|
* @param {!Array<string>} browserContextIds
|
|
|
|
* @param {?Puppeteer.Viewport} defaultViewport
|
|
|
|
* @param {?Puppeteer.ChildProcess} process
|
|
|
|
* @param {function():void} closeCallback
|
|
|
|
*/
|
|
|
|
constructor(connection, browserContextIds, defaultViewport, process, closeCallback) {
|
2018-12-06 19:24:00 +00:00
|
|
|
super();
|
|
|
|
this._connection = connection;
|
|
|
|
this._defaultViewport = defaultViewport;
|
|
|
|
this._process = process;
|
|
|
|
this._closeCallback = closeCallback;
|
|
|
|
|
|
|
|
/** @type {!Map<string, ?Target>} */
|
|
|
|
this._pageTargets = new Map();
|
|
|
|
|
2019-02-01 22:09:18 +00:00
|
|
|
this._defaultContext = new BrowserContext(this._connection, this, null);
|
|
|
|
/** @type {!Map<string, !BrowserContext>} */
|
|
|
|
this._contexts = new Map();
|
|
|
|
for (const browserContextId of browserContextIds)
|
|
|
|
this._contexts.set(browserContextId, new BrowserContext(this._connection, this, browserContextId));
|
|
|
|
|
2019-02-16 01:57:48 +00:00
|
|
|
this._connection.on(Events.Connection.Disconnected, () => this.emit(Events.Browser.Disconnected));
|
|
|
|
|
2018-12-06 19:24:00 +00:00
|
|
|
this._eventListeners = [
|
|
|
|
helper.addEventListener(this._connection, 'Browser.tabOpened', this._onTabOpened.bind(this)),
|
|
|
|
helper.addEventListener(this._connection, 'Browser.tabClosed', this._onTabClosed.bind(this)),
|
|
|
|
helper.addEventListener(this._connection, 'Browser.tabNavigated', this._onTabNavigated.bind(this)),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:57:48 +00:00
|
|
|
wsEndpoint() {
|
|
|
|
return this._connection.url();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this._connection.dispose();
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:09:18 +00:00
|
|
|
/**
|
|
|
|
* @return {!BrowserContext}
|
|
|
|
*/
|
|
|
|
async createIncognitoBrowserContext() {
|
|
|
|
const {browserContextId} = await this._connection.send('Browser.createBrowserContext');
|
|
|
|
const context = new BrowserContext(this._connection, this, browserContextId);
|
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Array<!BrowserContext>}
|
|
|
|
*/
|
|
|
|
browserContexts() {
|
|
|
|
return [this._defaultContext, ...Array.from(this._contexts.values())];
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultBrowserContext() {
|
|
|
|
return this._defaultContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _disposeContext(browserContextId) {
|
|
|
|
await this._connection.send('Browser.removeBrowserContext', {browserContextId});
|
|
|
|
this._contexts.delete(browserContextId);
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:24:00 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async userAgent() {
|
|
|
|
const info = await this._connection.send('Browser.getInfo');
|
|
|
|
return info.userAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async version() {
|
|
|
|
const info = await this._connection.send('Browser.getInfo');
|
|
|
|
return info.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {?Puppeteer.ChildProcess}
|
|
|
|
*/
|
|
|
|
process() {
|
|
|
|
return this._process;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function(!Target):boolean} predicate
|
|
|
|
* @param {{timeout?: number}=} options
|
|
|
|
* @return {!Promise<!Target>}
|
|
|
|
*/
|
|
|
|
async waitForTarget(predicate, options = {}) {
|
|
|
|
const {
|
|
|
|
timeout = 30000
|
|
|
|
} = options;
|
|
|
|
const existingTarget = this.targets().find(predicate);
|
|
|
|
if (existingTarget)
|
|
|
|
return existingTarget;
|
|
|
|
let resolve;
|
|
|
|
const targetPromise = new Promise(x => resolve = x);
|
2019-02-08 02:03:48 +00:00
|
|
|
this.on(Events.Browser.TargetCreated, check);
|
2018-12-06 19:24:00 +00:00
|
|
|
this.on('targetchanged', check);
|
|
|
|
try {
|
|
|
|
if (!timeout)
|
|
|
|
return await targetPromise;
|
|
|
|
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
|
|
|
} finally {
|
2019-02-08 02:03:48 +00:00
|
|
|
this.removeListener(Events.Browser.TargetCreated, check);
|
2018-12-06 19:24:00 +00:00
|
|
|
this.removeListener('targetchanged', check);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Target} target
|
|
|
|
*/
|
|
|
|
function check(target) {
|
|
|
|
if (predicate(target))
|
|
|
|
resolve(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:09:18 +00:00
|
|
|
/**
|
|
|
|
* @return {Promise<Page>}
|
|
|
|
*/
|
|
|
|
newPage() {
|
|
|
|
return this._createPageInContext(this._defaultContext._browserContextId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?string} browserContextId
|
|
|
|
* @return {Promise<Page>}
|
|
|
|
*/
|
|
|
|
async _createPageInContext(browserContextId) {
|
|
|
|
const {pageId} = await this._connection.send('Browser.newPage', {
|
|
|
|
browserContextId: browserContextId || undefined
|
|
|
|
});
|
2018-12-06 19:24:00 +00:00
|
|
|
const target = this._pageTargets.get(pageId);
|
|
|
|
return await target.page();
|
|
|
|
}
|
|
|
|
|
|
|
|
async pages() {
|
|
|
|
const pageTargets = Array.from(this._pageTargets.values());
|
|
|
|
return await Promise.all(pageTargets.map(target => target.page()));
|
|
|
|
}
|
|
|
|
|
|
|
|
targets() {
|
|
|
|
return Array.from(this._pageTargets.values());
|
|
|
|
}
|
|
|
|
|
2019-02-09 03:21:54 +00:00
|
|
|
async _onTabOpened({pageId, url, browserContextId, openerId}) {
|
2019-02-01 22:09:18 +00:00
|
|
|
const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;
|
2019-02-09 03:21:54 +00:00
|
|
|
const opener = openerId ? this._pageTargets.get(openerId) : null;
|
|
|
|
const target = new Target(this._connection, this, context, pageId, url, opener);
|
2018-12-06 19:24:00 +00:00
|
|
|
this._pageTargets.set(pageId, target);
|
2019-02-09 03:21:54 +00:00
|
|
|
if (opener && opener._pagePromise) {
|
|
|
|
const openerPage = await opener._pagePromise;
|
|
|
|
if (openerPage.listenerCount(Events.Page.Popup)) {
|
|
|
|
const popupPage = await target.page();
|
|
|
|
openerPage.emit(Events.Page.Popup, popupPage);
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 02:03:48 +00:00
|
|
|
this.emit(Events.Browser.TargetCreated, target);
|
|
|
|
context.emit(Events.BrowserContext.TargetCreated, target);
|
2018-12-06 19:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onTabClosed({pageId}) {
|
|
|
|
const target = this._pageTargets.get(pageId);
|
|
|
|
this._pageTargets.delete(pageId);
|
2019-02-08 02:03:48 +00:00
|
|
|
this.emit(Events.Browser.TargetDestroyed, target);
|
|
|
|
target.browserContext().emit(Events.BrowserContext.TargetDestroyed, target);
|
2018-12-06 19:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onTabNavigated({pageId, url}) {
|
|
|
|
const target = this._pageTargets.get(pageId);
|
|
|
|
target._url = url;
|
2019-02-08 02:03:48 +00:00
|
|
|
this.emit(Events.Browser.TargetChanged, target);
|
|
|
|
target.browserContext().emit(Events.BrowserContext.TargetChanged, target);
|
2018-12-06 19:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
await this._closeCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Target {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} connection
|
|
|
|
* @param {!Browser} browser
|
2019-02-01 22:09:18 +00:00
|
|
|
* @param {!BrowserContext} context
|
2018-12-06 19:24:00 +00:00
|
|
|
* @param {string} pageId
|
|
|
|
* @param {string} url
|
2019-02-09 03:21:54 +00:00
|
|
|
* @param {?Target} opener
|
2018-12-06 19:24:00 +00:00
|
|
|
*/
|
2019-02-09 03:21:54 +00:00
|
|
|
constructor(connection, browser, context, pageId, url, opener) {
|
2018-12-06 19:24:00 +00:00
|
|
|
this._browser = browser;
|
2019-02-01 22:09:18 +00:00
|
|
|
this._context = context;
|
2018-12-06 19:24:00 +00:00
|
|
|
this._connection = connection;
|
|
|
|
this._pageId = pageId;
|
|
|
|
/** @type {?Promise<!Page>} */
|
|
|
|
this._pagePromise = null;
|
|
|
|
this._url = url;
|
2019-02-09 03:21:54 +00:00
|
|
|
this._opener = opener;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {?Target}
|
|
|
|
*/
|
|
|
|
opener() {
|
|
|
|
return this._opener;
|
2018-12-06 19:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {"page"|"background_page"|"service_worker"|"other"|"browser"}
|
|
|
|
*/
|
|
|
|
type() {
|
|
|
|
return 'page';
|
|
|
|
}
|
|
|
|
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:09:18 +00:00
|
|
|
/**
|
|
|
|
* @return {!BrowserContext}
|
|
|
|
*/
|
|
|
|
browserContext() {
|
|
|
|
return this._context;
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:24:00 +00:00
|
|
|
async page() {
|
|
|
|
if (!this._pagePromise)
|
|
|
|
this._pagePromise = Page.create(this._connection, this, this._pageId, this._browser._defaultViewport);
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser() {
|
|
|
|
return this._browser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:09:18 +00:00
|
|
|
class BrowserContext extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Connection} connection
|
|
|
|
* @param {!Browser} browser
|
|
|
|
* @param {?string} browserContextId
|
|
|
|
*/
|
|
|
|
constructor(connection, browser, browserContextId) {
|
|
|
|
super();
|
|
|
|
this._connection = connection;
|
|
|
|
this._browser = browser;
|
|
|
|
this._browserContextId = browserContextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Array<Target>}
|
|
|
|
*/
|
|
|
|
targets() {
|
|
|
|
return this._browser.targets().filter(target => target.browserContext() === this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function(Target):boolean} predicate
|
|
|
|
* @param {{timeout?: number}=} options
|
|
|
|
* @return {!Promise<Target>}
|
|
|
|
*/
|
|
|
|
waitForTarget(predicate, options) {
|
|
|
|
return this._browser.waitForTarget(target => target.browserContext() === this && predicate(target), options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isIncognito() {
|
|
|
|
return !!this._browserContextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
newPage() {
|
|
|
|
return this._browser._createPageInContext(this._browserContextId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Browser}
|
|
|
|
*/
|
|
|
|
browser() {
|
|
|
|
return this._browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
assert(this._browserContextId, 'Non-incognito contexts cannot be closed!');
|
|
|
|
await this._browser._disposeContext(this._browserContextId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:18:43 +00:00
|
|
|
module.exports = {Browser, BrowserContext, Target};
|