bidiPreloadScript.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. /*
  3. * Copyright 2023 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.BidiPreloadScript = void 0;
  21. const uuid_js_1 = require("../../../utils/uuid.js");
  22. const channelProxy_js_1 = require("../script/channelProxy.js");
  23. /**
  24. * BiDi IDs are generated by the server and are unique within the context.
  25. *
  26. * CDP preload script IDs are generated by the client and are unique
  27. * within the session.
  28. *
  29. * The mapping between BiDi and CDP preload script IDs is 1:many.
  30. * BiDi IDs are needed by the mapper to keep track of potential multiple CDP IDs
  31. * in the client.
  32. */
  33. class BidiPreloadScript {
  34. /** BiDi ID, an automatically generated UUID. */
  35. #id = (0, uuid_js_1.uuidv4)();
  36. /** CDP preload scripts. */
  37. #cdpPreloadScripts = [];
  38. /** The script itself, in a format expected by the spec i.e. a function. */
  39. #functionDeclaration;
  40. /** Browsing context ID. */
  41. #contextId;
  42. /** Targets, in which the preload script is initialized. */
  43. #targetIds = new Set();
  44. /** Channels to be added as arguments to functionDeclaration. */
  45. #channels;
  46. get id() {
  47. return this.#id;
  48. }
  49. get contextId() {
  50. return this.#contextId;
  51. }
  52. get targetIds() {
  53. return this.#targetIds;
  54. }
  55. constructor(params) {
  56. if (params.sandbox !== undefined) {
  57. // TODO: Handle sandbox.
  58. throw new Error('Sandbox is not supported yet');
  59. }
  60. this.#channels =
  61. params.arguments?.map((a) => new channelProxy_js_1.ChannelProxy(a.value)) ?? [];
  62. this.#functionDeclaration = params.functionDeclaration;
  63. this.#contextId = params.context ?? null;
  64. }
  65. /** Channels of the preload script. */
  66. get channels() {
  67. return this.#channels;
  68. }
  69. /**
  70. * Adds the script to the given CDP targets by calling the
  71. * `Page.addScriptToEvaluateOnNewDocument` command.
  72. */
  73. async initInTargets(cdpTargets) {
  74. await Promise.all(Array.from(cdpTargets).map((cdpTarget) => this.initInTarget(cdpTarget)));
  75. }
  76. /**
  77. * String to be evaluated. Wraps user-provided function so that the following
  78. * steps are run:
  79. * 1. Create channels.
  80. * 2. Store the created channels in window.
  81. * 3. Call the user-provided function with channels as arguments.
  82. */
  83. #getEvaluateString() {
  84. const channelsArgStr = `[${this.channels
  85. .map((c) => c.getEvalInWindowStr())
  86. .join(', ')}]`;
  87. return `(()=>{(${this.#functionDeclaration})(...${channelsArgStr})})()`;
  88. }
  89. /**
  90. * Adds the script to the given CDP target by calling the
  91. * `Page.addScriptToEvaluateOnNewDocument` command.
  92. */
  93. async initInTarget(cdpTarget) {
  94. const addCdpPreloadScriptResult = await cdpTarget.cdpClient.sendCommand('Page.addScriptToEvaluateOnNewDocument', {
  95. source: this.#getEvaluateString(),
  96. });
  97. this.#cdpPreloadScripts.push({
  98. target: cdpTarget,
  99. preloadScriptId: addCdpPreloadScriptResult.identifier,
  100. });
  101. this.#targetIds.add(cdpTarget.targetId);
  102. }
  103. /**
  104. * Schedules the script to be run right after
  105. * `Runtime.runIfWaitingForDebugger`, but does not wait for result.
  106. */
  107. scheduleEvaluateInTarget(cdpTarget) {
  108. void cdpTarget.cdpClient.sendCommand('Runtime.evaluate', {
  109. expression: this.#getEvaluateString(),
  110. });
  111. }
  112. /**
  113. * Removes this script from all CDP targets.
  114. */
  115. async remove() {
  116. for (const cdpPreloadScript of this.#cdpPreloadScripts) {
  117. const cdpTarget = cdpPreloadScript.target;
  118. const cdpPreloadScriptId = cdpPreloadScript.preloadScriptId;
  119. await cdpTarget.cdpClient.sendCommand('Page.removeScriptToEvaluateOnNewDocument', {
  120. identifier: cdpPreloadScriptId,
  121. });
  122. }
  123. }
  124. /**
  125. * Removes the provided cdp target from the list of cdp preload scripts.
  126. */
  127. cdpTargetIsGone(cdpTargetId) {
  128. this.#cdpPreloadScripts = this.#cdpPreloadScripts.filter((cdpPreloadScript) => cdpPreloadScript.target?.targetId !== cdpTargetId);
  129. this.#targetIds.delete(cdpTargetId);
  130. }
  131. }
  132. exports.BidiPreloadScript = BidiPreloadScript;
  133. //# sourceMappingURL=bidiPreloadScript.js.map