wkExecutionContext.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.WKExecutionContext = void 0;
  6. var js = _interopRequireWildcard(require("../javascript"));
  7. var _utilityScriptSerializers = require("../isomorphic/utilityScriptSerializers");
  8. var _protocolError = require("../protocolError");
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. /**
  12. * Copyright 2017 Google Inc. All rights reserved.
  13. * Modifications copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class WKExecutionContext {
  28. constructor(session, contextId) {
  29. this._session = void 0;
  30. this._contextId = void 0;
  31. this._session = session;
  32. this._contextId = contextId;
  33. }
  34. async rawEvaluateJSON(expression) {
  35. try {
  36. const response = await this._session.send('Runtime.evaluate', {
  37. expression,
  38. contextId: this._contextId,
  39. returnByValue: true
  40. });
  41. if (response.wasThrown) throw new js.JavaScriptErrorInEvaluate(response.result.description);
  42. return response.result.value;
  43. } catch (error) {
  44. throw rewriteError(error);
  45. }
  46. }
  47. async rawEvaluateHandle(expression) {
  48. try {
  49. const response = await this._session.send('Runtime.evaluate', {
  50. expression,
  51. contextId: this._contextId,
  52. returnByValue: false
  53. });
  54. if (response.wasThrown) throw new js.JavaScriptErrorInEvaluate(response.result.description);
  55. return response.result.objectId;
  56. } catch (error) {
  57. throw rewriteError(error);
  58. }
  59. }
  60. rawCallFunctionNoReply(func, ...args) {
  61. this._session.send('Runtime.callFunctionOn', {
  62. functionDeclaration: func.toString(),
  63. objectId: args.find(a => a instanceof js.JSHandle)._objectId,
  64. arguments: args.map(a => a instanceof js.JSHandle ? {
  65. objectId: a._objectId
  66. } : {
  67. value: a
  68. }),
  69. returnByValue: true,
  70. emulateUserGesture: true
  71. }).catch(() => {});
  72. }
  73. async evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds) {
  74. try {
  75. const response = await this._session.send('Runtime.callFunctionOn', {
  76. functionDeclaration: expression,
  77. objectId: utilityScript._objectId,
  78. arguments: [{
  79. objectId: utilityScript._objectId
  80. }, ...values.map(value => ({
  81. value
  82. })), ...objectIds.map(objectId => ({
  83. objectId
  84. }))],
  85. returnByValue,
  86. emulateUserGesture: true,
  87. awaitPromise: true
  88. });
  89. if (response.wasThrown) throw new js.JavaScriptErrorInEvaluate(response.result.description);
  90. if (returnByValue) return (0, _utilityScriptSerializers.parseEvaluationResultValue)(response.result.value);
  91. return utilityScript._context.createHandle(response.result);
  92. } catch (error) {
  93. throw rewriteError(error);
  94. }
  95. }
  96. async getProperties(context, objectId) {
  97. const response = await this._session.send('Runtime.getProperties', {
  98. objectId,
  99. ownProperties: true
  100. });
  101. const result = new Map();
  102. for (const property of response.properties) {
  103. if (!property.enumerable || !property.value) continue;
  104. result.set(property.name, context.createHandle(property.value));
  105. }
  106. return result;
  107. }
  108. createHandle(context, remoteObject) {
  109. const isPromise = remoteObject.className === 'Promise';
  110. return new js.JSHandle(context, isPromise ? 'promise' : remoteObject.subtype || remoteObject.type, renderPreview(remoteObject), remoteObject.objectId, potentiallyUnserializableValue(remoteObject));
  111. }
  112. async releaseHandle(objectId) {
  113. await this._session.send('Runtime.releaseObject', {
  114. objectId
  115. });
  116. }
  117. objectCount(objectId) {
  118. throw new Error('Method not implemented in WebKit.');
  119. }
  120. }
  121. exports.WKExecutionContext = WKExecutionContext;
  122. function potentiallyUnserializableValue(remoteObject) {
  123. const value = remoteObject.value;
  124. const isUnserializable = remoteObject.type === 'number' && ['NaN', '-Infinity', 'Infinity', '-0'].includes(remoteObject.description);
  125. return isUnserializable ? js.parseUnserializableValue(remoteObject.description) : value;
  126. }
  127. function rewriteError(error) {
  128. if (!js.isJavaScriptErrorInEvaluate(error) && !(0, _protocolError.isSessionClosedError)(error)) return new Error('Execution context was destroyed, most likely because of a navigation.');
  129. return error;
  130. }
  131. function renderPreview(object) {
  132. if (object.type === 'undefined') return 'undefined';
  133. if ('value' in object) return String(object.value);
  134. if (object.description === 'Object' && object.preview) {
  135. const tokens = [];
  136. for (const {
  137. name,
  138. value
  139. } of object.preview.properties) tokens.push(`${name}: ${value}`);
  140. return `{${tokens.join(', ')}}`;
  141. }
  142. if (object.subtype === 'array' && object.preview) return js.sparseArrayToString(object.preview.properties);
  143. return object.description;
  144. }