UNPKG

3.56 kBJavaScriptView Raw
1const {Page} = require('./Page');
2const {helper} = require('./helper');
3
4class Target {
5 /**
6 * @param {!Protocol.Target.TargetInfo} targetInfo
7 * @param {!Puppeteer.BrowserContext} browserContext
8 * @param {!function():!Promise<!Puppeteer.CDPSession>} sessionFactory
9 * @param {boolean} ignoreHTTPSErrors
10 * @param {?Puppeteer.Viewport} defaultViewport
11 * @param {!Puppeteer.TaskQueue} screenshotTaskQueue
12 */
13 constructor(targetInfo, browserContext, sessionFactory, ignoreHTTPSErrors, defaultViewport, screenshotTaskQueue) {
14 this._targetInfo = targetInfo;
15 this._browserContext = browserContext;
16 this._targetId = targetInfo.targetId;
17 this._sessionFactory = sessionFactory;
18 this._ignoreHTTPSErrors = ignoreHTTPSErrors;
19 this._defaultViewport = defaultViewport;
20 this._screenshotTaskQueue = screenshotTaskQueue;
21 /** @type {?Promise<!Puppeteer.Page>} */
22 this._pagePromise = null;
23 this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill);
24 this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill);
25 this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
26 if (this._isInitialized)
27 this._initializedCallback(true);
28 }
29
30 /**
31 * @return {!Promise<!Puppeteer.CDPSession>}
32 */
33 createCDPSession() {
34 return this._sessionFactory();
35 }
36
37 /**
38 * @return {!Promise<?Page>}
39 */
40 /* async */ page() {return (fn => {
41 const gen = fn.call(this);
42 return new Promise((resolve, reject) => {
43 function step(key, arg) {
44 let info, value;
45 try {
46 info = gen[key](arg);
47 value = info.value;
48 } catch (error) {
49 reject(error);
50 return;
51 }
52 if (info.done) {
53 resolve(value);
54 } else {
55 return Promise.resolve(value).then(
56 value => {
57 step('next', value);
58 },
59 err => {
60 step('throw', err);
61 });
62 }
63 }
64 return step('next');
65 });
66})(function*(){
67 if ((this._targetInfo.type === 'page' || this._targetInfo.type === 'background_page') && !this._pagePromise) {
68 this._pagePromise = this._sessionFactory()
69 .then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._defaultViewport, this._screenshotTaskQueue));
70 }
71 return this._pagePromise;
72 });}
73
74 /**
75 * @return {string}
76 */
77 url() {
78 return this._targetInfo.url;
79 }
80
81 /**
82 * @return {"page"|"background_page"|"service_worker"|"other"|"browser"}
83 */
84 type() {
85 const type = this._targetInfo.type;
86 if (type === 'page' || type === 'background_page' || type === 'service_worker' || type === 'browser')
87 return type;
88 return 'other';
89 }
90
91 /**
92 * @return {!Puppeteer.Browser}
93 */
94 browser() {
95 return this._browserContext.browser();
96 }
97
98 /**
99 * @return {!Puppeteer.BrowserContext}
100 */
101 browserContext() {
102 return this._browserContext;
103 }
104
105 /**
106 * @return {Puppeteer.Target}
107 */
108 opener() {
109 const { openerId } = this._targetInfo;
110 if (!openerId)
111 return null;
112 return this.browser()._targets.get(openerId);
113 }
114
115 /**
116 * @param {!Protocol.Target.TargetInfo} targetInfo
117 */
118 _targetInfoChanged(targetInfo) {
119 this._targetInfo = targetInfo;
120
121 if (!this._isInitialized && (this._targetInfo.type !== 'page' || this._targetInfo.url !== '')) {
122 this._isInitialized = true;
123 this._initializedCallback(true);
124 return;
125 }
126 }
127}
128
129helper.tracePublicAPI(Target);
130
131module.exports = {Target};