jsHandle.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.JSHandle = void 0;
  6. exports.assertMaxArguments = assertMaxArguments;
  7. exports.parseResult = parseResult;
  8. exports.serializeArgument = serializeArgument;
  9. var _channelOwner = require("./channelOwner");
  10. var _serializers = require("../protocol/serializers");
  11. let _Symbol$asyncDispose;
  12. /**
  13. * 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. _Symbol$asyncDispose = Symbol.asyncDispose;
  28. class JSHandle extends _channelOwner.ChannelOwner {
  29. static from(handle) {
  30. return handle._object;
  31. }
  32. constructor(parent, type, guid, initializer) {
  33. super(parent, type, guid, initializer);
  34. this._preview = void 0;
  35. this._preview = this._initializer.preview;
  36. this._channel.on('previewUpdated', ({
  37. preview
  38. }) => this._preview = preview);
  39. }
  40. async evaluate(pageFunction, arg) {
  41. const result = await this._channel.evaluateExpression({
  42. expression: String(pageFunction),
  43. isFunction: typeof pageFunction === 'function',
  44. arg: serializeArgument(arg)
  45. });
  46. return parseResult(result.value);
  47. }
  48. async evaluateHandle(pageFunction, arg) {
  49. const result = await this._channel.evaluateExpressionHandle({
  50. expression: String(pageFunction),
  51. isFunction: typeof pageFunction === 'function',
  52. arg: serializeArgument(arg)
  53. });
  54. return JSHandle.from(result.handle);
  55. }
  56. async getProperty(propertyName) {
  57. const result = await this._channel.getProperty({
  58. name: propertyName
  59. });
  60. return JSHandle.from(result.handle);
  61. }
  62. async getProperties() {
  63. const map = new Map();
  64. for (const {
  65. name,
  66. value
  67. } of (await this._channel.getPropertyList()).properties) map.set(name, JSHandle.from(value));
  68. return map;
  69. }
  70. async jsonValue() {
  71. return parseResult((await this._channel.jsonValue()).value);
  72. }
  73. asElement() {
  74. return null;
  75. }
  76. async [_Symbol$asyncDispose]() {
  77. await this.dispose();
  78. }
  79. async dispose() {
  80. return await this._channel.dispose();
  81. }
  82. async _objectCount() {
  83. return await this._wrapApiCall(async () => {
  84. const {
  85. count
  86. } = await this._channel.objectCount();
  87. return count;
  88. });
  89. }
  90. toString() {
  91. return this._preview;
  92. }
  93. }
  94. // This function takes care of converting all JSHandles to their channels,
  95. // so that generic channel serializer converts them to guids.
  96. exports.JSHandle = JSHandle;
  97. function serializeArgument(arg) {
  98. const handles = [];
  99. const pushHandle = channel => {
  100. handles.push(channel);
  101. return handles.length - 1;
  102. };
  103. const value = (0, _serializers.serializeValue)(arg, value => {
  104. if (value instanceof JSHandle) return {
  105. h: pushHandle(value._channel)
  106. };
  107. return {
  108. fallThrough: value
  109. };
  110. });
  111. return {
  112. value,
  113. handles
  114. };
  115. }
  116. function parseResult(value) {
  117. return (0, _serializers.parseSerializedValue)(value, undefined);
  118. }
  119. function assertMaxArguments(count, max) {
  120. if (count > max) throw new Error('Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.');
  121. }