2018-02-26 20:10:06 +00:00
|
|
|
const Page = require('./Page');
|
|
|
|
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-04-03 22:05:27 +00:00
|
|
|
* @param {boolean} setDefaultViewport
|
2018-02-26 20:10:06 +00:00
|
|
|
* @param {!Puppeteer.TaskQueue} screenshotTaskQueue
|
|
|
|
*/
|
2018-05-10 20:26:08 +00:00
|
|
|
constructor(targetInfo, browserContext, sessionFactory, ignoreHTTPSErrors, setDefaultViewport, 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-04-03 22:05:27 +00:00
|
|
|
this._setDefaultViewport = setDefaultViewport;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._screenshotTaskQueue = screenshotTaskQueue;
|
|
|
|
/** @type {?Promise<!Puppeteer.Page>} */
|
|
|
|
this._pagePromise = null;
|
|
|
|
this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill);
|
|
|
|
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() {
|
|
|
|
if (this._targetInfo.type === 'page' && !this._pagePromise) {
|
|
|
|
this._pagePromise = this._sessionFactory()
|
2018-04-03 22:05:27 +00:00
|
|
|
.then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._setDefaultViewport, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-04-17 17:37:17 +00:00
|
|
|
module.exports = Target;
|