1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const { helper } = require('./helper');
|
18 | const Target = require('./Target');
|
19 | const EventEmitter = require('events');
|
20 | const TaskQueue = require('./TaskQueue');
|
21 |
|
22 | class Browser extends EventEmitter {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(connection, options = {}, process, closeCallback) {
|
30 | super();
|
31 | this._ignoreHTTPSErrors = !!options.ignoreHTTPSErrors;
|
32 | this._appMode = !!options.appMode;
|
33 | this._process = process;
|
34 | this._screenshotTaskQueue = new TaskQueue();
|
35 | this._connection = connection;
|
36 | this._closeCallback = closeCallback || new Function();
|
37 |
|
38 | this._targets = new Map();
|
39 | this._connection.setClosedCallback(() => {
|
40 | this.emit(Browser.Events.Disconnected);
|
41 | });
|
42 | this._connection.on('Target.targetCreated', this._targetCreated.bind(this));
|
43 | this._connection.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
|
44 | this._connection.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 | process() {
|
51 | return this._process;
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | static async create(connection, options, process, closeCallback) {
|
61 | const browser = new Browser(connection, options, process, closeCallback);
|
62 | await connection.send('Target.setDiscoverTargets', {discover: true});
|
63 | return browser;
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 | async _targetCreated(event) {
|
70 | const targetInfo = event.targetInfo;
|
71 | const target = new Target(targetInfo, () => this._connection.createSession(targetInfo.targetId), this._ignoreHTTPSErrors, !this._appMode, this._screenshotTaskQueue);
|
72 | console.assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
|
73 | this._targets.set(event.targetInfo.targetId, target);
|
74 |
|
75 | if (await target._initializedPromise)
|
76 | this.emit(Browser.Events.TargetCreated, target);
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 | async _targetDestroyed(event) {
|
83 | const target = this._targets.get(event.targetId);
|
84 | target._initializedCallback(false);
|
85 | this._targets.delete(event.targetId);
|
86 | target._closedCallback();
|
87 | if (await target._initializedPromise)
|
88 | this.emit(Browser.Events.TargetDestroyed, target);
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 | _targetInfoChanged(event) {
|
95 | const target = this._targets.get(event.targetInfo.targetId);
|
96 | console.assert(target, 'target should exist before targetInfoChanged');
|
97 | const previousURL = target.url();
|
98 | const wasInitialized = target._isInitialized;
|
99 | target._targetInfoChanged(event.targetInfo);
|
100 | if (wasInitialized && previousURL !== target.url())
|
101 | this.emit(Browser.Events.TargetChanged, target);
|
102 | }
|
103 |
|
104 | |
105 |
|
106 |
|
107 | wsEndpoint() {
|
108 | return this._connection.url();
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 | async newPage() {
|
115 | const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank'});
|
116 | const target = await this._targets.get(targetId);
|
117 | console.assert(await target._initializedPromise, 'Failed to create target for page');
|
118 | const page = await target.page();
|
119 | return page;
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 | targets() {
|
126 | return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 | async pages() {
|
133 | const pages = await Promise.all(this.targets().map(target => target.page()));
|
134 | return pages.filter(page => !!page);
|
135 | }
|
136 |
|
137 | |
138 |
|
139 |
|
140 | async version() {
|
141 | const version = await this._getVersion();
|
142 | return version.product;
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 | async userAgent() {
|
149 | const version = await this._getVersion();
|
150 | return version.userAgent;
|
151 | }
|
152 |
|
153 | async close() {
|
154 | await this._closeCallback.call(null);
|
155 | this.disconnect();
|
156 | }
|
157 |
|
158 | disconnect() {
|
159 | this._connection.dispose();
|
160 | }
|
161 |
|
162 | |
163 |
|
164 |
|
165 | _getVersion() {
|
166 | return this._connection.send('Browser.getVersion');
|
167 | }
|
168 | }
|
169 |
|
170 |
|
171 | Browser.Events = {
|
172 | TargetCreated: 'targetcreated',
|
173 | TargetDestroyed: 'targetdestroyed',
|
174 | TargetChanged: 'targetchanged',
|
175 | Disconnected: 'disconnected'
|
176 | };
|
177 |
|
178 | helper.tracePublicAPI(Browser);
|
179 |
|
180 | module.exports = Browser;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
\ | No newline at end of file |