PageAutomator

Automate the actions on the page

当前为 2023-01-27 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/458951/1142464/PageAutomator.js

  1. // ==UserScript==
  2. // @name PageAutomator
  3. // @description Automate the actions on the page
  4. // @version 1.0.0
  5. // @author aolko
  6. // @match *
  7. // @namespace https://gf.qytechs.cn/ru/users/5008-aolko
  8. // @run-at document-start
  9. // @grant none
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.6.5/mousetrap.min.js
  11. // ==/UserScript==
  12.  
  13. function PageAutomator() {
  14. var Mousetrap = require("mousetrap");
  15.  
  16. // Mouse events
  17. mouse: {
  18. this.hover = function (selector) {
  19. var element = document.querySelector(selector);
  20. element.dispatchEvent(new MouseEvent("mouseover"));
  21. };
  22.  
  23. this.click = function (selector, button) {
  24. var element = document.querySelector(selector);
  25. if (button === "left") {
  26. element.dispatchEvent(new MouseEvent("click"));
  27. } else if (button === "right") {
  28. element.dispatchEvent(new MouseEvent("contextmenu"));
  29. }
  30. };
  31.  
  32. this.scroll = function (amount) {
  33. window.scrollBy(0, amount);
  34. };
  35.  
  36. this.scrollTo = function (element) {
  37. element.scrollIntoView({
  38. behavior: "smooth",
  39. block: "start",
  40. inline: "nearest",
  41. });
  42. };
  43.  
  44. this.hold = function (selector, button) {
  45. var element = document.querySelector(selector);
  46. if (button === "left") {
  47. element.dispatchEvent(new MouseEvent("mousedown"));
  48. } else if (button === "right") {
  49. element.dispatchEvent(
  50. new MouseEvent("mousedown", {
  51. button: 2,
  52. })
  53. );
  54. }
  55. };
  56.  
  57. this.moveToPosition = function (x, y) {
  58. window.dispatchEvent(
  59. new MouseEvent("mousemove", {
  60. clientX: x,
  61. clientY: y,
  62. })
  63. );
  64. };
  65. this.moveToElement = function (selector) {
  66. var element = document.querySelector(selector);
  67. var rect = element.getBoundingClientRect();
  68. var x = rect.left + rect.width / 2;
  69. var y = rect.top + rect.height / 2;
  70. this.moveToPosition(x, y);
  71. };
  72.  
  73. this.getPosition = function () {
  74. var position = {
  75. x: 0,
  76. y: 0,
  77. };
  78. document.addEventListener("mousemove", function (event) {
  79. position.x = event.clientX;
  80. position.y = event.clientY;
  81. });
  82. return position;
  83. };
  84. }
  85.  
  86. // Keyboard events
  87. keyboard: {
  88. this.keyPress = function (key) {
  89. var event = new KeyboardEvent("keypress", {
  90. key: key,
  91. });
  92. document.dispatchEvent(event);
  93. };
  94.  
  95. this.keyUp = function (key) {
  96. var event = new KeyboardEvent("keyup", {
  97. key: key,
  98. });
  99. document.dispatchEvent(event);
  100. };
  101.  
  102. this.keyDown = function (key) {
  103. var event = new KeyboardEvent("keydown", {
  104. key: key,
  105. });
  106. document.dispatchEvent(event);
  107. };
  108.  
  109. this.holdKey = function (key, action) {
  110. var keys = {
  111. ctrl: 17,
  112. shift: 16,
  113. alt: 18,
  114. win: 91,
  115. };
  116. var event = new KeyboardEvent("keydown", {
  117. keyCode: keys[key],
  118. which: keys[key],
  119. });
  120. document.dispatchEvent(event);
  121. action();
  122. var event = new KeyboardEvent("keyup", {
  123. keyCode: keys[key],
  124. which: keys[key],
  125. });
  126. document.dispatchEvent(event);
  127. };
  128.  
  129. this.holdKeySequence = function (sequence, action) {
  130. Mousetrap.bind(
  131. sequence,
  132. function () {
  133. action();
  134. Mousetrap.unbind(sequence);
  135. },
  136. "keydown"
  137. );
  138. };
  139.  
  140. this.setKeyState = function (key, state) {
  141. if (key === "numlock") {
  142. var event = new KeyboardEvent("keydown", {
  143. key: "NumLock",
  144. code: "NumLock",
  145. });
  146. document.dispatchEvent(event);
  147. } else if (key === "scrolllock") {
  148. var event = new KeyboardEvent("keydown", {
  149. key: "ScrollLock",
  150. code: "ScrollLock",
  151. });
  152. document.dispatchEvent(event);
  153. } else if (key === "capslock") {
  154. var event = new KeyboardEvent("keydown", {
  155. key: "CapsLock",
  156. code: "CapsLock",
  157. });
  158. document.dispatchEvent(event);
  159. }
  160. };
  161. }
  162.  
  163. input: {
  164. // Block input
  165. this.blockInput = function () {
  166. document.addEventListener("keydown", function (event) {
  167. event.preventDefault();
  168. });
  169. document.addEventListener("mousedown", function (event) {
  170. event.preventDefault();
  171. });
  172. };
  173. }
  174.  
  175. timer: {
  176. // Timer events
  177. this.wait = function (ms) {
  178. var start = new Date().getTime();
  179. var end = start;
  180. while (end < start + ms) {
  181. end = new Date().getTime();
  182. }
  183. };
  184.  
  185. this.waitForElement = function (selector) {
  186. var element = document.querySelector(selector);
  187. while (!element) {
  188. element = document.querySelector(selector);
  189. }
  190. };
  191.  
  192. this.waitForMouse = function (cursor) {
  193. var currentCursor = document.body.style.cursor;
  194. while (currentCursor !== cursor) {
  195. currentCursor = document.body.style.cursor;
  196. }
  197. };
  198. }
  199.  
  200. // Conditionals
  201. this.ifElement = function (selector, condition, value) {
  202. var element = document.querySelector(selector);
  203. if (condition === "contains") {
  204. if (element.innerHTML.includes(value)) {
  205. return true;
  206. } else {
  207. return false;
  208. }
  209. } else if (condition === "does not contain") {
  210. if (!element.innerHTML.includes(value)) {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. } else if (condition === "is") {
  216. if (element.innerHTML === value) {
  217. return true;
  218. } else {
  219. return false;
  220. }
  221. } else if (condition === "is not") {
  222. if (element.innerHTML !== value) {
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. };
  229. dialogs: {
  230. // Dialogs/Message Boxes
  231. this.showNotification = function (title, text) {
  232. var notification = new Notification(title, {
  233. body: text,
  234. });
  235. };
  236.  
  237. this.showDialog = function (title, text) {
  238. var dialog = document.createElement("dialog");
  239. var titleElement = document.createElement("strong");
  240. titleElement.innerHTML = title;
  241. var textElement = document.createElement("p");
  242. textElement.innerHTML = text;
  243. dialog.appendChild(titleElement);
  244. dialog.appendChild(textElement);
  245. document.body.appendChild(dialog);
  246. dialog.show();
  247. };
  248.  
  249. this.showCustomDialog = function (html) {
  250. var dialog = document.createElement("dialog");
  251. dialog.innerHTML = html;
  252. document.body.appendChild(dialog);
  253. dialog.show();
  254. };
  255. }
  256.  
  257. clipboard: {
  258. // Clipboard
  259. this.getClipboardText = function () {
  260. return navigator.clipboard.readText().then((text) => {
  261. return text;
  262. });
  263. };
  264.  
  265. this.setClipboardText = function (text) {
  266. navigator.clipboard.writeText(text);
  267. };
  268.  
  269. this.clearClipboard = function () {
  270. navigator.clipboard.writeText("");
  271. };
  272. }
  273. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址