UNPKG

2.33 kBJavaScriptView Raw
1const Page = require('./Page');
2const {helper} = require('./helper');
3
4class Target {
5 /**
6 * @param {!Puppeteer.TargetInfo} targetInfo
7 * @param {!function():!Promise<!Puppeteer.CDPSession>} sessionFactory
8 * @param {boolean} ignoreHTTPSErrors
9 * @param {boolean} setDefaultViewport
10 * @param {!Puppeteer.TaskQueue} screenshotTaskQueue
11 */
12 constructor(targetInfo, sessionFactory, ignoreHTTPSErrors, setDefaultViewport, screenshotTaskQueue) {
13 this._targetInfo = targetInfo;
14 this._targetId = targetInfo.targetId;
15 this._sessionFactory = sessionFactory;
16 this._ignoreHTTPSErrors = ignoreHTTPSErrors;
17 this._setDefaultViewport = setDefaultViewport;
18 this._screenshotTaskQueue = screenshotTaskQueue;
19 /** @type {?Promise<!Puppeteer.Page>} */
20 this._pagePromise = null;
21 this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill);
22 this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill);
23 this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
24 if (this._isInitialized)
25 this._initializedCallback(true);
26 }
27
28 /**
29 * @return {!Promise<!Puppeteer.CDPSession>}
30 */
31 createCDPSession() {
32 return this._sessionFactory();
33 }
34
35 /**
36 * @return {!Promise<?Page>}
37 */
38 async page() {
39 if (this._targetInfo.type === 'page' && !this._pagePromise) {
40 this._pagePromise = this._sessionFactory()
41 .then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._setDefaultViewport, this._screenshotTaskQueue));
42 }
43 return this._pagePromise;
44 }
45
46 /**
47 * @return {string}
48 */
49 url() {
50 return this._targetInfo.url;
51 }
52
53 /**
54 * @return {"page"|"service_worker"|"other"|"browser"}
55 */
56 type() {
57 const type = this._targetInfo.type;
58 if (type === 'page' || type === 'service_worker' || type === 'browser')
59 return type;
60 return 'other';
61 }
62
63 /**
64 * @param {!Puppeteer.TargetInfo} targetInfo
65 */
66 _targetInfoChanged(targetInfo) {
67 this._targetInfo = targetInfo;
68
69 if (!this._isInitialized && (this._targetInfo.type !== 'page' || this._targetInfo.url !== '')) {
70 this._isInitialized = true;
71 this._initializedCallback(true);
72 return;
73 }
74 }
75}
76
77helper.tracePublicAPI(Target);
78
79module.exports = Target;
\No newline at end of file