Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

performActions.js 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const USKeyboardLayout_1 = require("puppeteer-core/lib/cjs/puppeteer/common/USKeyboardLayout");
  7. const getElementRect_1 = __importDefault(require("./getElementRect"));
  8. const constants_1 = require("../constants");
  9. const KEY = 'key';
  10. const POINTER = 'pointer';
  11. const sleep = (time = 0) => new Promise((resolve) => setTimeout(resolve, time));
  12. async function performActions({ actions }) {
  13. const page = this.getPageHandle();
  14. const lastPointer = {};
  15. for (const action of actions) {
  16. if (action.type === null || action.type === 'null') {
  17. for (const singleAction of action.actions) {
  18. await sleep(singleAction.duration);
  19. }
  20. continue;
  21. }
  22. if (action.type === 'key') {
  23. const skipChars = [];
  24. for (const singleAction of action.actions) {
  25. if (singleAction.type === 'pause') {
  26. await sleep(singleAction.duration);
  27. continue;
  28. }
  29. const cmd = singleAction.type.slice(KEY.length).toLowerCase();
  30. const keyboardFn = page.keyboard[cmd].bind(page.keyboard);
  31. if (cmd === 'up' && skipChars[0] === singleAction.value) {
  32. skipChars.shift();
  33. continue;
  34. }
  35. if (!USKeyboardLayout_1.keyDefinitions[singleAction.value]) {
  36. await page.keyboard.sendCharacter(singleAction.value);
  37. skipChars.push(singleAction.value);
  38. continue;
  39. }
  40. await keyboardFn(singleAction.value);
  41. continue;
  42. }
  43. continue;
  44. }
  45. if (action.type === 'pointer') {
  46. if (action.parameters && action.parameters.pointerType && action.parameters.pointerType !== 'mouse') {
  47. throw new Error('Currently only "mouse" is supported as pointer type');
  48. }
  49. for (const singleAction of action.actions) {
  50. if (singleAction.type === 'pause') {
  51. await sleep(singleAction.duration);
  52. continue;
  53. }
  54. const cmd = singleAction.type.slice(POINTER.length).toLowerCase();
  55. const keyboardFn = page.mouse[cmd].bind(page.mouse);
  56. let { x, y, duration, button, origin } = singleAction;
  57. if (cmd === 'move') {
  58. if (typeof x === 'number' &&
  59. typeof y === 'number' &&
  60. origin === 'pointer' &&
  61. lastPointer.x && lastPointer.y) {
  62. x += lastPointer.x;
  63. y += lastPointer.y;
  64. }
  65. if (origin && typeof origin[constants_1.ELEMENT_KEY] === 'string' && typeof x === 'number' && typeof y === 'number') {
  66. const elemRect = await getElementRect_1.default.call(this, { elementId: origin[constants_1.ELEMENT_KEY] });
  67. x += elemRect.x + (elemRect.width / 2);
  68. y += elemRect.y + (elemRect.height / 2);
  69. }
  70. lastPointer.x = x;
  71. lastPointer.y = y;
  72. await keyboardFn(x, y, { steps: 10 });
  73. continue;
  74. }
  75. else {
  76. const pptrButton = (button === 1 ? 'middle' : (button === 2 ? 'right' : 'left'));
  77. await keyboardFn({ button: pptrButton });
  78. }
  79. if (duration) {
  80. await sleep(duration);
  81. }
  82. continue;
  83. }
  84. continue;
  85. }
  86. throw new Error(`Unknown action type ("${action.type}"), allowed are only: null, key and pointer`);
  87. }
  88. }
  89. exports.default = performActions;