jsHandle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var jsHandle_exports = {};
  20. __export(jsHandle_exports, {
  21. JSHandle: () => JSHandle,
  22. assertMaxArguments: () => assertMaxArguments,
  23. parseResult: () => parseResult,
  24. serializeArgument: () => serializeArgument
  25. });
  26. module.exports = __toCommonJS(jsHandle_exports);
  27. var import_channelOwner = require("./channelOwner");
  28. var import_errors = require("./errors");
  29. var import_serializers = require("../protocol/serializers");
  30. class JSHandle extends import_channelOwner.ChannelOwner {
  31. static from(handle) {
  32. return handle._object;
  33. }
  34. constructor(parent, type, guid, initializer) {
  35. super(parent, type, guid, initializer);
  36. this._preview = this._initializer.preview;
  37. this._channel.on("previewUpdated", ({ preview }) => this._preview = preview);
  38. }
  39. async evaluate(pageFunction, arg) {
  40. const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: serializeArgument(arg) });
  41. return parseResult(result.value);
  42. }
  43. async _evaluateFunction(functionDeclaration) {
  44. const result = await this._channel.evaluateExpression({ expression: functionDeclaration, isFunction: true, arg: serializeArgument(void 0) });
  45. return parseResult(result.value);
  46. }
  47. async evaluateHandle(pageFunction, arg) {
  48. const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: serializeArgument(arg) });
  49. return JSHandle.from(result.handle);
  50. }
  51. async getProperty(propertyName) {
  52. const result = await this._channel.getProperty({ name: propertyName });
  53. return JSHandle.from(result.handle);
  54. }
  55. async getProperties() {
  56. const map = /* @__PURE__ */ new Map();
  57. for (const { name, value } of (await this._channel.getPropertyList()).properties)
  58. map.set(name, JSHandle.from(value));
  59. return map;
  60. }
  61. async jsonValue() {
  62. return parseResult((await this._channel.jsonValue()).value);
  63. }
  64. asElement() {
  65. return null;
  66. }
  67. async [Symbol.asyncDispose]() {
  68. await this.dispose();
  69. }
  70. async dispose() {
  71. try {
  72. await this._channel.dispose();
  73. } catch (e) {
  74. if ((0, import_errors.isTargetClosedError)(e))
  75. return;
  76. throw e;
  77. }
  78. }
  79. toString() {
  80. return this._preview;
  81. }
  82. }
  83. function serializeArgument(arg) {
  84. const handles = [];
  85. const pushHandle = (channel) => {
  86. handles.push(channel);
  87. return handles.length - 1;
  88. };
  89. const value = (0, import_serializers.serializeValue)(arg, (value2) => {
  90. if (value2 instanceof JSHandle)
  91. return { h: pushHandle(value2._channel) };
  92. return { fallThrough: value2 };
  93. });
  94. return { value, handles };
  95. }
  96. function parseResult(value) {
  97. return (0, import_serializers.parseSerializedValue)(value, void 0);
  98. }
  99. function assertMaxArguments(count, max) {
  100. if (count > max)
  101. throw new Error("Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.");
  102. }
  103. // Annotate the CommonJS export names for ESM import in node:
  104. 0 && (module.exports = {
  105. JSHandle,
  106. assertMaxArguments,
  107. parseResult,
  108. serializeArgument
  109. });