ExecutionContext.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Copyright 2017 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 {helper, assert} = require('./helper');
  17. const {createJSHandle, JSHandle} = require('./JSHandle');
  18. const EVALUATION_SCRIPT_URL = '__puppeteer_evaluation_script__';
  19. const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
  20. class ExecutionContext {
  21. /**
  22. * @param {!Puppeteer.CDPSession} client
  23. * @param {!Protocol.Runtime.ExecutionContextDescription} contextPayload
  24. * @param {?Puppeteer.DOMWorld} world
  25. */
  26. constructor(client, contextPayload, world) {
  27. this._client = client;
  28. this._world = world;
  29. this._contextId = contextPayload.id;
  30. }
  31. /**
  32. * @return {?Puppeteer.Frame}
  33. */
  34. frame() {
  35. return this._world ? this._world.frame() : null;
  36. }
  37. /**
  38. * @param {Function|string} pageFunction
  39. * @param {...*} args
  40. * @return {!Promise<*>}
  41. */
  42. async evaluate(pageFunction, ...args) {
  43. return await this._evaluateInternal(true /* returnByValue */, pageFunction, ...args);
  44. }
  45. /**
  46. * @param {Function|string} pageFunction
  47. * @param {...*} args
  48. * @return {!Promise<!JSHandle>}
  49. */
  50. async evaluateHandle(pageFunction, ...args) {
  51. return this._evaluateInternal(false /* returnByValue */, pageFunction, ...args);
  52. }
  53. /**
  54. * @param {boolean} returnByValue
  55. * @param {Function|string} pageFunction
  56. * @param {...*} args
  57. * @return {!Promise<*>}
  58. */
  59. async _evaluateInternal(returnByValue, pageFunction, ...args) {
  60. const suffix = `//# sourceURL=${EVALUATION_SCRIPT_URL}`;
  61. if (helper.isString(pageFunction)) {
  62. const contextId = this._contextId;
  63. const expression = /** @type {string} */ (pageFunction);
  64. const expressionWithSourceUrl = SOURCE_URL_REGEX.test(expression) ? expression : expression + '\n' + suffix;
  65. const {exceptionDetails, result: remoteObject} = await this._client.send('Runtime.evaluate', {
  66. expression: expressionWithSourceUrl,
  67. contextId,
  68. returnByValue,
  69. awaitPromise: true,
  70. userGesture: true
  71. }).catch(rewriteError);
  72. if (exceptionDetails)
  73. throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
  74. return returnByValue ? helper.valueFromRemoteObject(remoteObject) : createJSHandle(this, remoteObject);
  75. }
  76. if (typeof pageFunction !== 'function')
  77. throw new Error(`Expected to get |string| or |function| as the first argument, but got "${pageFunction}" instead.`);
  78. let functionText = pageFunction.toString();
  79. try {
  80. new Function('(' + functionText + ')');
  81. } catch (e1) {
  82. // This means we might have a function shorthand. Try another
  83. // time prefixing 'function '.
  84. if (functionText.startsWith('async '))
  85. functionText = 'async function ' + functionText.substring('async '.length);
  86. else
  87. functionText = 'function ' + functionText;
  88. try {
  89. new Function('(' + functionText + ')');
  90. } catch (e2) {
  91. // We tried hard to serialize, but there's a weird beast here.
  92. throw new Error('Passed function is not well-serializable!');
  93. }
  94. }
  95. let callFunctionOnPromise;
  96. try {
  97. callFunctionOnPromise = this._client.send('Runtime.callFunctionOn', {
  98. functionDeclaration: functionText + '\n' + suffix + '\n',
  99. executionContextId: this._contextId,
  100. arguments: args.map(convertArgument.bind(this)),
  101. returnByValue,
  102. awaitPromise: true,
  103. userGesture: true
  104. });
  105. } catch (err) {
  106. if (err instanceof TypeError && err.message.startsWith('Converting circular structure to JSON'))
  107. err.message += ' Are you passing a nested JSHandle?';
  108. throw err;
  109. }
  110. const { exceptionDetails, result: remoteObject } = await callFunctionOnPromise.catch(rewriteError);
  111. if (exceptionDetails)
  112. throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
  113. return returnByValue ? helper.valueFromRemoteObject(remoteObject) : createJSHandle(this, remoteObject);
  114. /**
  115. * @param {*} arg
  116. * @return {*}
  117. * @this {ExecutionContext}
  118. */
  119. function convertArgument(arg) {
  120. if (typeof arg === 'bigint') // eslint-disable-line valid-typeof
  121. return { unserializableValue: `${arg.toString()}n` };
  122. if (Object.is(arg, -0))
  123. return { unserializableValue: '-0' };
  124. if (Object.is(arg, Infinity))
  125. return { unserializableValue: 'Infinity' };
  126. if (Object.is(arg, -Infinity))
  127. return { unserializableValue: '-Infinity' };
  128. if (Object.is(arg, NaN))
  129. return { unserializableValue: 'NaN' };
  130. const objectHandle = arg && (arg instanceof JSHandle) ? arg : null;
  131. if (objectHandle) {
  132. if (objectHandle._context !== this)
  133. throw new Error('JSHandles can be evaluated only in the context they were created!');
  134. if (objectHandle._disposed)
  135. throw new Error('JSHandle is disposed!');
  136. if (objectHandle._remoteObject.unserializableValue)
  137. return { unserializableValue: objectHandle._remoteObject.unserializableValue };
  138. if (!objectHandle._remoteObject.objectId)
  139. return { value: objectHandle._remoteObject.value };
  140. return { objectId: objectHandle._remoteObject.objectId };
  141. }
  142. return { value: arg };
  143. }
  144. /**
  145. * @param {!Error} error
  146. * @return {!Protocol.Runtime.evaluateReturnValue}
  147. */
  148. function rewriteError(error) {
  149. if (error.message.includes('Object reference chain is too long'))
  150. return {result: {type: 'undefined'}};
  151. if (error.message.includes('Object couldn\'t be returned by value'))
  152. return {result: {type: 'undefined'}};
  153. if (error.message.endsWith('Cannot find context with specified id') || error.message.endsWith('Inspected target navigated or closed'))
  154. throw new Error('Execution context was destroyed, most likely because of a navigation.');
  155. throw error;
  156. }
  157. }
  158. /**
  159. * @param {!JSHandle} prototypeHandle
  160. * @return {!Promise<!JSHandle>}
  161. */
  162. async queryObjects(prototypeHandle) {
  163. assert(!prototypeHandle._disposed, 'Prototype JSHandle is disposed!');
  164. assert(prototypeHandle._remoteObject.objectId, 'Prototype JSHandle must not be referencing primitive value');
  165. const response = await this._client.send('Runtime.queryObjects', {
  166. prototypeObjectId: prototypeHandle._remoteObject.objectId
  167. });
  168. return createJSHandle(this, response.objects);
  169. }
  170. /**
  171. * @param {Protocol.DOM.BackendNodeId} backendNodeId
  172. * @return {Promise<Puppeteer.ElementHandle>}
  173. */
  174. async _adoptBackendNodeId(backendNodeId) {
  175. const {object} = await this._client.send('DOM.resolveNode', {
  176. backendNodeId: backendNodeId,
  177. executionContextId: this._contextId,
  178. });
  179. return /** @type {Puppeteer.ElementHandle}*/(createJSHandle(this, object));
  180. }
  181. /**
  182. * @param {Puppeteer.ElementHandle} elementHandle
  183. * @return {Promise<Puppeteer.ElementHandle>}
  184. */
  185. async _adoptElementHandle(elementHandle) {
  186. assert(elementHandle.executionContext() !== this, 'Cannot adopt handle that already belongs to this execution context');
  187. assert(this._world, 'Cannot adopt handle without DOMWorld');
  188. const nodeInfo = await this._client.send('DOM.describeNode', {
  189. objectId: elementHandle._remoteObject.objectId,
  190. });
  191. return this._adoptBackendNodeId(nodeInfo.node.backendNodeId);
  192. }
  193. }
  194. module.exports = {ExecutionContext, EVALUATION_SCRIPT_URL};