Worker.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright 2018 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const EventEmitter = require('events');
  17. const {debugError} = require('./helper');
  18. const {ExecutionContext} = require('./ExecutionContext');
  19. const {JSHandle} = require('./JSHandle');
  20. class Worker extends EventEmitter {
  21. /**
  22. * @param {Puppeteer.CDPSession} client
  23. * @param {string} url
  24. * @param {function(string, !Array<!JSHandle>, Protocol.Runtime.StackTrace=):void} consoleAPICalled
  25. * @param {function(!Protocol.Runtime.ExceptionDetails):void} exceptionThrown
  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. /** @type {function(!Protocol.Runtime.RemoteObject):!JSHandle} */
  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. // This might fail if the target is closed before we recieve all execution contexts.
  40. this._client.send('Runtime.enable', {}).catch(debugError);
  41. this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory), event.stackTrace));
  42. this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
  43. }
  44. /**
  45. * @return {string}
  46. */
  47. url() {
  48. return this._url;
  49. }
  50. /**
  51. * @return {!Promise<ExecutionContext>}
  52. */
  53. async executionContext() {
  54. return this._executionContextPromise;
  55. }
  56. /**
  57. * @param {Function|string} pageFunction
  58. * @param {!Array<*>} args
  59. * @return {!Promise<*>}
  60. */
  61. async evaluate(pageFunction, ...args) {
  62. return (await this._executionContextPromise).evaluate(pageFunction, ...args);
  63. }
  64. /**
  65. * @param {Function|string} pageFunction
  66. * @param {!Array<*>} args
  67. * @return {!Promise<!JSHandle>}
  68. */
  69. async evaluateHandle(pageFunction, ...args) {
  70. return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
  71. }
  72. }
  73. module.exports = {Worker};