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');
|
|
|
|
const {helper, debugError} = require('./helper');
|
|
|
|
const {ExecutionContext, JSHandle} = require('./ExecutionContext');
|
|
|
|
|
|
|
|
class Worker extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* @param {Puppeteer.CDPSession} client
|
|
|
|
* @param {string} url
|
2018-06-07 18:21:35 +00:00
|
|
|
* @param {function(!string, !Array<!JSHandle>)} consoleAPICalled
|
2018-06-25 22:10:36 +00:00
|
|
|
* @param {function(!Protocol.Runtime.ExceptionDetails)} 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);
|
|
|
|
|
2018-06-07 18:21:35 +00:00
|
|
|
this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory)));
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function()|string} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<*>}
|
|
|
|
*/
|
|
|
|
async evaluate(pageFunction, ...args) {
|
|
|
|
return (await this._executionContextPromise).evaluate(pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function()|string} pageFunction
|
|
|
|
* @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};
|
2018-05-21 21:31:11 +00:00
|
|
|
helper.tracePublicAPI(Worker);
|