1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const EventEmitter = require('events');
|
17 | const {helper, debugError} = require('./helper');
|
18 | const {ExecutionContext, JSHandle} = require('./ExecutionContext');
|
19 |
|
20 | class Worker extends EventEmitter {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | constructor(client, url, logEntryAdded) {
|
27 | super();
|
28 | this._client = client;
|
29 | this._url = url;
|
30 | this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
|
31 | this._client.once('Runtime.executionContextCreated', async event => {
|
32 | const jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
|
33 | const executionContext = new ExecutionContext(client, event.context, jsHandleFactory, null);
|
34 | this._executionContextCallback(executionContext);
|
35 | });
|
36 |
|
37 | this._client.send('Runtime.enable', {}).catch(debugError);
|
38 |
|
39 | this._client.on('Log.entryAdded', logEntryAdded);
|
40 | this._client.send('Log.enable', {}).catch(debugError);
|
41 | }
|
42 |
|
43 | |
44 |
|
45 |
|
46 | url() {
|
47 | return this._url;
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 | async executionContext() {
|
54 | return this._executionContextPromise;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | async evaluate(pageFunction, ...args) {
|
63 | return (await this._executionContextPromise).evaluate(pageFunction, ...args);
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 | async evaluateHandle(pageFunction, ...args) {
|
72 | return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
|
73 | }
|
74 | }
|
75 |
|
76 | module.exports = Worker;
|
77 | helper.tracePublicAPI(Worker);
|