input.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var input_exports = {};
  30. __export(input_exports, {
  31. Keyboard: () => Keyboard,
  32. Mouse: () => Mouse,
  33. Touchscreen: () => Touchscreen,
  34. keypadLocation: () => keypadLocation,
  35. resolveSmartModifier: () => resolveSmartModifier,
  36. resolveSmartModifierString: () => resolveSmartModifierString
  37. });
  38. module.exports = __toCommonJS(input_exports);
  39. var import_utils = require("../utils");
  40. var keyboardLayout = __toESM(require("./usKeyboardLayout"));
  41. var import_dom = require("./dom");
  42. const keypadLocation = keyboardLayout.keypadLocation;
  43. const kModifiers = ["Alt", "Control", "Meta", "Shift"];
  44. class Keyboard {
  45. constructor(raw, page) {
  46. this._pressedModifiers = /* @__PURE__ */ new Set();
  47. this._pressedKeys = /* @__PURE__ */ new Set();
  48. this._raw = raw;
  49. this._page = page;
  50. }
  51. async down(progress, key) {
  52. const description = this._keyDescriptionForString(key);
  53. const autoRepeat = this._pressedKeys.has(description.code);
  54. this._pressedKeys.add(description.code);
  55. if (kModifiers.includes(description.key))
  56. this._pressedModifiers.add(description.key);
  57. await this._raw.keydown(progress, this._pressedModifiers, key, description, autoRepeat);
  58. }
  59. _keyDescriptionForString(str) {
  60. const keyString = resolveSmartModifierString(str);
  61. let description = usKeyboardLayout.get(keyString);
  62. if (!description)
  63. throw new import_dom.NonRecoverableDOMError(`Unknown key: "${keyString}"`);
  64. const shift = this._pressedModifiers.has("Shift");
  65. description = shift && description.shifted ? description.shifted : description;
  66. if (this._pressedModifiers.size > 1 || !this._pressedModifiers.has("Shift") && this._pressedModifiers.size === 1)
  67. return { ...description, text: "" };
  68. return description;
  69. }
  70. async up(progress, key) {
  71. const description = this._keyDescriptionForString(key);
  72. if (kModifiers.includes(description.key))
  73. this._pressedModifiers.delete(description.key);
  74. this._pressedKeys.delete(description.code);
  75. await this._raw.keyup(progress, this._pressedModifiers, key, description);
  76. }
  77. async insertText(progress, text) {
  78. await this._raw.sendText(progress, text);
  79. }
  80. async type(progress, text, options) {
  81. const delay = options && options.delay || void 0;
  82. for (const char of text) {
  83. if (usKeyboardLayout.has(char)) {
  84. await this.press(progress, char, { delay });
  85. } else {
  86. if (delay)
  87. await progress.wait(delay);
  88. await this.insertText(progress, char);
  89. }
  90. }
  91. }
  92. async press(progress, key, options = {}) {
  93. function split(keyString) {
  94. const keys = [];
  95. let building = "";
  96. for (const char of keyString) {
  97. if (char === "+" && building) {
  98. keys.push(building);
  99. building = "";
  100. } else {
  101. building += char;
  102. }
  103. }
  104. keys.push(building);
  105. return keys;
  106. }
  107. const tokens = split(key);
  108. key = tokens[tokens.length - 1];
  109. for (let i = 0; i < tokens.length - 1; ++i)
  110. await this.down(progress, tokens[i]);
  111. await this.down(progress, key);
  112. if (options.delay)
  113. await progress.wait(options.delay);
  114. await this.up(progress, key);
  115. for (let i = tokens.length - 2; i >= 0; --i)
  116. await this.up(progress, tokens[i]);
  117. }
  118. async ensureModifiers(progress, mm) {
  119. const modifiers = mm.map(resolveSmartModifier);
  120. for (const modifier of modifiers) {
  121. if (!kModifiers.includes(modifier))
  122. throw new Error("Unknown modifier " + modifier);
  123. }
  124. const restore = Array.from(this._pressedModifiers);
  125. for (const key of kModifiers) {
  126. const needDown = modifiers.includes(key);
  127. const isDown = this._pressedModifiers.has(key);
  128. if (needDown && !isDown)
  129. await this.down(progress, key);
  130. else if (!needDown && isDown)
  131. await this.up(progress, key);
  132. }
  133. return restore;
  134. }
  135. _modifiers() {
  136. return this._pressedModifiers;
  137. }
  138. }
  139. function resolveSmartModifierString(key) {
  140. if (key === "ControlOrMeta")
  141. return process.platform === "darwin" ? "Meta" : "Control";
  142. return key;
  143. }
  144. function resolveSmartModifier(m) {
  145. return resolveSmartModifierString(m);
  146. }
  147. class Mouse {
  148. constructor(raw, page) {
  149. this._x = 0;
  150. this._y = 0;
  151. this._lastButton = "none";
  152. this._buttons = /* @__PURE__ */ new Set();
  153. this._raw = raw;
  154. this._page = page;
  155. this._keyboard = this._page.keyboard;
  156. }
  157. currentPoint() {
  158. return { x: this._x, y: this._y };
  159. }
  160. async move(progress, x, y, options = {}) {
  161. const { steps = 1 } = options;
  162. const fromX = this._x;
  163. const fromY = this._y;
  164. this._x = x;
  165. this._y = y;
  166. for (let i = 1; i <= steps; i++) {
  167. const middleX = fromX + (x - fromX) * (i / steps);
  168. const middleY = fromY + (y - fromY) * (i / steps);
  169. await this._raw.move(progress, middleX, middleY, this._lastButton, this._buttons, this._keyboard._modifiers(), !!options.forClick);
  170. }
  171. }
  172. async down(progress, options = {}) {
  173. const { button = "left", clickCount = 1 } = options;
  174. this._lastButton = button;
  175. this._buttons.add(button);
  176. await this._raw.down(progress, this._x, this._y, this._lastButton, this._buttons, this._keyboard._modifiers(), clickCount);
  177. }
  178. async up(progress, options = {}) {
  179. const { button = "left", clickCount = 1 } = options;
  180. this._lastButton = "none";
  181. this._buttons.delete(button);
  182. await this._raw.up(progress, this._x, this._y, button, this._buttons, this._keyboard._modifiers(), clickCount);
  183. }
  184. async click(progress, x, y, options = {}) {
  185. const { delay = null, clickCount = 1, steps } = options;
  186. if (delay) {
  187. await this.move(progress, x, y, { forClick: true, steps });
  188. for (let cc = 1; cc <= clickCount; ++cc) {
  189. await this.down(progress, { ...options, clickCount: cc });
  190. await progress.wait(delay);
  191. await this.up(progress, { ...options, clickCount: cc });
  192. if (cc < clickCount)
  193. await progress.wait(delay);
  194. }
  195. } else {
  196. const promises = [];
  197. const movePromise = this.move(progress, x, y, { forClick: true, steps });
  198. if (steps !== void 0 && steps > 1)
  199. await movePromise;
  200. else
  201. promises.push(movePromise);
  202. for (let cc = 1; cc <= clickCount; ++cc) {
  203. promises.push(this.down(progress, { ...options, clickCount: cc }));
  204. promises.push(this.up(progress, { ...options, clickCount: cc }));
  205. }
  206. await Promise.all(promises);
  207. }
  208. }
  209. async wheel(progress, deltaX, deltaY) {
  210. await this._raw.wheel(progress, this._x, this._y, this._buttons, this._keyboard._modifiers(), deltaX, deltaY);
  211. }
  212. }
  213. const aliases = /* @__PURE__ */ new Map([
  214. ["ShiftLeft", ["Shift"]],
  215. ["ControlLeft", ["Control"]],
  216. ["AltLeft", ["Alt"]],
  217. ["MetaLeft", ["Meta"]],
  218. ["Enter", ["\n", "\r"]]
  219. ]);
  220. const usKeyboardLayout = buildLayoutClosure(keyboardLayout.USKeyboardLayout);
  221. function buildLayoutClosure(layout) {
  222. const result = /* @__PURE__ */ new Map();
  223. for (const code in layout) {
  224. const definition = layout[code];
  225. const description = {
  226. key: definition.key || "",
  227. keyCode: definition.keyCode || 0,
  228. keyCodeWithoutLocation: definition.keyCodeWithoutLocation || definition.keyCode || 0,
  229. code,
  230. text: definition.text || "",
  231. location: definition.location || 0
  232. };
  233. if (definition.key.length === 1)
  234. description.text = description.key;
  235. let shiftedDescription;
  236. if (definition.shiftKey) {
  237. (0, import_utils.assert)(definition.shiftKey.length === 1);
  238. shiftedDescription = { ...description };
  239. shiftedDescription.key = definition.shiftKey;
  240. shiftedDescription.text = definition.shiftKey;
  241. if (definition.shiftKeyCode)
  242. shiftedDescription.keyCode = definition.shiftKeyCode;
  243. }
  244. result.set(code, { ...description, shifted: shiftedDescription });
  245. if (aliases.has(code)) {
  246. for (const alias of aliases.get(code))
  247. result.set(alias, description);
  248. }
  249. if (definition.location)
  250. continue;
  251. if (description.key.length === 1)
  252. result.set(description.key, description);
  253. if (shiftedDescription)
  254. result.set(shiftedDescription.key, { ...shiftedDescription, shifted: void 0 });
  255. }
  256. return result;
  257. }
  258. class Touchscreen {
  259. constructor(raw, page) {
  260. this._raw = raw;
  261. this._page = page;
  262. }
  263. async tap(progress, x, y) {
  264. if (!this._page.browserContext._options.hasTouch)
  265. throw new Error("hasTouch must be enabled on the browser context before using the touchscreen.");
  266. await this._raw.tap(progress, x, y, this._page.keyboard._modifiers());
  267. }
  268. }
  269. // Annotate the CommonJS export names for ESM import in node:
  270. 0 && (module.exports = {
  271. Keyboard,
  272. Mouse,
  273. Touchscreen,
  274. keypadLocation,
  275. resolveSmartModifier,
  276. resolveSmartModifierString
  277. });