2018-05-21 21:31:11 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
const EventEmitter = require('events');
|
2019-01-26 04:21:14 +00:00
|
|
|
const {debugError} = require('./helper');
|
2019-01-15 04:34:50 +00:00
|
|
|
const {ExecutionContext} = require('./ExecutionContext');
|
|
|
|
const {JSHandle} = require('./JSHandle');
|
2020-04-21 08:20:25 +00:00
|
|
|
// Used as a TypeDef
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const {CDPSession} = require('./Connection');
|
2018-05-21 21:31:11 +00:00
|
|
|
|
|
|
|
class Worker extends EventEmitter {
|
|
|
|
/**
|
2020-04-21 08:20:25 +00:00
|
|
|
* @param {CDPSession} client
|
2018-05-21 21:31:11 +00:00
|
|
|
* @param {string} url
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {function(string, !Array<!JSHandle>, Protocol.Runtime.StackTrace=):void} consoleAPICalled
|
|
|
|
* @param {function(!Protocol.Runtime.ExceptionDetails):void} exceptionThrown
|
2018-05-21 21:31:11 +00:00
|
|
|
*/
|
2018-06-25 22:10:36 +00:00
|
|
|
constructor(client, url, consoleAPICalled, exceptionThrown) {
|
2018-05-21 21:31:11 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
|
|
|
this._url = url;
|
|
|
|
this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
|
2018-06-07 18:21:35 +00:00
|
|
|
/** @type {function(!Protocol.Runtime.RemoteObject):!JSHandle} */
|
|
|
|
let jsHandleFactory;
|
2018-05-31 01:08:27 +00:00
|
|
|
this._client.once('Runtime.executionContextCreated', async event => {
|
2018-06-07 18:21:35 +00:00
|
|
|
jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
|
2018-08-16 23:16:27 +00:00
|
|
|
const executionContext = new ExecutionContext(client, event.context, null);
|
2018-05-21 21:31:11 +00:00
|
|
|
this._executionContextCallback(executionContext);
|
|
|
|
});
|
|
|
|
// This might fail if the target is closed before we recieve all execution contexts.
|
|
|
|
this._client.send('Runtime.enable', {}).catch(debugError);
|
|
|
|
|
2019-01-11 02:05:28 +00:00
|
|
|
this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory), event.stackTrace));
|
2018-06-25 22:10:36 +00:00
|
|
|
this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
|
2018-05-21 21:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<ExecutionContext>}
|
|
|
|
*/
|
|
|
|
async executionContext() {
|
|
|
|
return this._executionContextPromise;
|
|
|
|
}
|
2018-06-07 01:16:17 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2018-06-07 01:16:17 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<*>}
|
|
|
|
*/
|
|
|
|
async evaluate(pageFunction, ...args) {
|
|
|
|
return (await this._executionContextPromise).evaluate(pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2018-06-07 01:16:17 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<!JSHandle>}
|
|
|
|
*/
|
|
|
|
async evaluateHandle(pageFunction, ...args) {
|
|
|
|
return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
|
|
|
|
}
|
2018-05-21 21:31:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 18:31:33 +00:00
|
|
|
module.exports = {Worker};
|