2018-08-06 18:31:33 +00:00
|
|
|
const {Page} = require('./Page');
|
2018-02-26 20:10:06 +00:00
|
|
|
const {helper} = require('./helper');
|
|
|
|
|
|
|
|
class Target {
|
|
|
|
/**
|
2018-05-10 20:26:08 +00:00
|
|
|
* @param {!Protocol.Target.TargetInfo} targetInfo
|
|
|
|
* @param {!Puppeteer.BrowserContext} browserContext
|
2018-02-26 20:10:06 +00:00
|
|
|
* @param {!function():!Promise<!Puppeteer.CDPSession>} sessionFactory
|
|
|
|
* @param {boolean} ignoreHTTPSErrors
|
2018-08-01 23:23:03 +00:00
|
|
|
* @param {?Puppeteer.Viewport} defaultViewport
|
2018-02-26 20:10:06 +00:00
|
|
|
* @param {!Puppeteer.TaskQueue} screenshotTaskQueue
|
|
|
|
*/
|
2018-08-01 23:23:03 +00:00
|
|
|
constructor(targetInfo, browserContext, sessionFactory, ignoreHTTPSErrors, defaultViewport, screenshotTaskQueue) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetInfo = targetInfo;
|
2018-05-10 20:26:08 +00:00
|
|
|
this._browserContext = browserContext;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetId = targetInfo.targetId;
|
|
|
|
this._sessionFactory = sessionFactory;
|
|
|
|
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
2018-08-01 23:23:03 +00:00
|
|
|
this._defaultViewport = defaultViewport;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._screenshotTaskQueue = screenshotTaskQueue;
|
|
|
|
/** @type {?Promise<!Puppeteer.Page>} */
|
|
|
|
this._pagePromise = null;
|
2018-12-13 01:09:42 +00:00
|
|
|
this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill).then(async success => {
|
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
const opener = this.opener();
|
|
|
|
if (!opener || !opener._pagePromise || this.type() !== 'page')
|
|
|
|
return true;
|
|
|
|
const openerPage = await opener._pagePromise;
|
|
|
|
if (!openerPage.listenerCount(Page.Events.Popup))
|
|
|
|
return true;
|
|
|
|
const popupPage = await this.page();
|
|
|
|
openerPage.emit(Page.Events.Popup, popupPage);
|
|
|
|
return true;
|
|
|
|
});
|
2018-02-26 20:10:06 +00:00
|
|
|
this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill);
|
|
|
|
this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
|
|
|
|
if (this._isInitialized)
|
|
|
|
this._initializedCallback(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<!Puppeteer.CDPSession>}
|
|
|
|
*/
|
|
|
|
createCDPSession() {
|
|
|
|
return this._sessionFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<?Page>}
|
|
|
|
*/
|
|
|
|
async page() {
|
2018-06-14 20:58:51 +00:00
|
|
|
if ((this._targetInfo.type === 'page' || this._targetInfo.type === 'background_page') && !this._pagePromise) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._pagePromise = this._sessionFactory()
|
2018-08-01 23:23:03 +00:00
|
|
|
.then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._defaultViewport, this._screenshotTaskQueue));
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._targetInfo.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-16 21:36:34 +00:00
|
|
|
* @return {"page"|"background_page"|"service_worker"|"other"|"browser"}
|
2018-02-26 20:10:06 +00:00
|
|
|
*/
|
|
|
|
type() {
|
|
|
|
const type = this._targetInfo.type;
|
2018-05-16 21:36:34 +00:00
|
|
|
if (type === 'page' || type === 'background_page' || type === 'service_worker' || type === 'browser')
|
2018-02-26 20:10:06 +00:00
|
|
|
return type;
|
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
|
2018-04-17 17:37:17 +00:00
|
|
|
/**
|
|
|
|
* @return {!Puppeteer.Browser}
|
|
|
|
*/
|
|
|
|
browser() {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._browserContext.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 20:10:06 +00:00
|
|
|
/**
|
2018-05-10 20:26:08 +00:00
|
|
|
* @return {!Puppeteer.BrowserContext}
|
|
|
|
*/
|
|
|
|
browserContext() {
|
|
|
|
return this._browserContext;
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:06:29 +00:00
|
|
|
/**
|
2018-08-24 22:17:36 +00:00
|
|
|
* @return {?Puppeteer.Target}
|
2018-06-01 00:06:29 +00:00
|
|
|
*/
|
|
|
|
opener() {
|
|
|
|
const { openerId } = this._targetInfo;
|
|
|
|
if (!openerId)
|
|
|
|
return null;
|
|
|
|
return this.browser()._targets.get(openerId);
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:26:08 +00:00
|
|
|
/**
|
|
|
|
* @param {!Protocol.Target.TargetInfo} targetInfo
|
2018-02-26 20:10:06 +00:00
|
|
|
*/
|
|
|
|
_targetInfoChanged(targetInfo) {
|
|
|
|
this._targetInfo = targetInfo;
|
|
|
|
|
|
|
|
if (!this._isInitialized && (this._targetInfo.type !== 'page' || this._targetInfo.url !== '')) {
|
|
|
|
this._isInitialized = true;
|
|
|
|
this._initializedCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.tracePublicAPI(Target);
|
|
|
|
|
2018-08-06 18:31:33 +00:00
|
|
|
module.exports = {Target};
|