1 | const {Page} = require('./Page');
|
2 | const {helper} = require('./helper');
|
3 |
|
4 | class Target {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
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 |
|
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 |
|
32 |
|
33 | createCDPSession() {
|
34 | return this._sessionFactory();
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 | 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 |
|
76 |
|
77 | url() {
|
78 | return this._targetInfo.url;
|
79 | }
|
80 |
|
81 | |
82 |
|
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 |
|
93 |
|
94 | browser() {
|
95 | return this._browserContext.browser();
|
96 | }
|
97 |
|
98 | |
99 |
|
100 |
|
101 | browserContext() {
|
102 | return this._browserContext;
|
103 | }
|
104 |
|
105 | |
106 |
|
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 |
|
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 |
|
129 | helper.tracePublicAPI(Target);
|
130 |
|
131 | module.exports = {Target};
|