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 |
|
27 | constructor(client, url, consoleAPICalled, exceptionThrown) {
|
28 | super();
|
29 | this._client = client;
|
30 | this._url = url;
|
31 | this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
|
32 |
|
33 | let jsHandleFactory;
|
34 | this._client.once('Runtime.executionContextCreated', async event => {
|
35 | jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
|
36 | const executionContext = new ExecutionContext(client, event.context, null);
|
37 | this._executionContextCallback(executionContext);
|
38 | });
|
39 |
|
40 | this._client.send('Runtime.enable', {}).catch(debugError);
|
41 |
|
42 | this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory)));
|
43 | this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 | url() {
|
50 | return this._url;
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 | async executionContext() {
|
57 | return this._executionContextPromise;
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | async evaluate(pageFunction, ...args) {
|
66 | return (await this._executionContextPromise).evaluate(pageFunction, ...args);
|
67 | }
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | async evaluateHandle(pageFunction, ...args) {
|
75 | return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
|
76 | }
|
77 | }
|
78 |
|
79 | module.exports = {Worker};
|
80 | helper.tracePublicAPI(Worker);
|