PageAutomator

Automate the actions on the page

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

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

  1. // ==UserScript==
  2. // @name PageAutomator
  3. // @description Automate the actions on the page
  4. // @version 1.0.2
  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.  
  15. // Mouse events
  16. mouse: {
  17. this.hover = function (selector) {
  18. var element = document.querySelector(selector);
  19. element.dispatchEvent(new MouseEvent("mouseover"));
  20. return this;
  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. return this;
  31. };
  32.  
  33. this.scroll = function (amount) {
  34. window.scrollBy(0, amount);
  35. return this;
  36. };
  37.  
  38. this.scrollTo = function (element) {
  39. element.scrollIntoView({
  40. behavior: "smooth",
  41. block: "start",
  42. inline: "nearest",
  43. });
  44. return this;
  45. };
  46.  
  47. this.hold = function (selector, button) {
  48. var element = document.querySelector(selector);
  49. if (button === "left") {
  50. element.dispatchEvent(new MouseEvent("mousedown"));
  51. } else if (button === "right") {
  52. element.dispatchEvent(
  53. new MouseEvent("mousedown", {
  54. button: 2,
  55. })
  56. );
  57. }
  58. return this;
  59. };
  60.  
  61. this.moveToPosition = function (x, y) {
  62. window.dispatchEvent(
  63. new MouseEvent("mousemove", {
  64. clientX: x,
  65. clientY: y,
  66. })
  67. );
  68. return this;
  69. };
  70. this.moveToElement = function (selector) {
  71. var element = document.querySelector(selector);
  72. var rect = element.getBoundingClientRect();
  73. var x = rect.left + rect.width / 2;
  74. var y = rect.top + rect.height / 2;
  75. this.moveToPosition(x, y);
  76. return this;
  77. };
  78.  
  79. this.getPosition = function () {
  80. var position = {
  81. x: 0,
  82. y: 0,
  83. };
  84. document.addEventListener("mousemove", function (event) {
  85. position.x = event.clientX;
  86. position.y = event.clientY;
  87. });
  88. return position;
  89. return this;
  90. };
  91. }
  92.  
  93. // Keyboard events
  94. keyboard: {
  95. this.keyPress = function (key) {
  96. var event = new KeyboardEvent("keypress", {
  97. key: key,
  98. });
  99. document.dispatchEvent(event);
  100. return this;
  101. };
  102.  
  103. this.keyUp = function (key) {
  104. var event = new KeyboardEvent("keyup", {
  105. key: key,
  106. });
  107. document.dispatchEvent(event);
  108. return this;
  109. };
  110.  
  111. this.keyDown = function (key) {
  112. var event = new KeyboardEvent("keydown", {
  113. key: key,
  114. });
  115. document.dispatchEvent(event);
  116. return this;
  117. };
  118.  
  119. this.holdKey = function (key, action) {
  120. var keys = {
  121. ctrl: 17,
  122. shift: 16,
  123. alt: 18,
  124. win: 91,
  125. };
  126. var event = new KeyboardEvent("keydown", {
  127. keyCode: keys[key],
  128. which: keys[key],
  129. });
  130. document.dispatchEvent(event);
  131. action();
  132. var event = new KeyboardEvent("keyup", {
  133. keyCode: keys[key],
  134. which: keys[key],
  135. });
  136. document.dispatchEvent(event);
  137. return this;
  138. };
  139.  
  140. this.holdKeySequence = function (sequence, action) {
  141. Mousetrap.bind(
  142. sequence,
  143. function () {
  144. action();
  145. Mousetrap.unbind(sequence);
  146. },
  147. "keydown"
  148. );
  149. return this;
  150. };
  151.  
  152. this.setKeyState = function (key, state) {
  153. if (key === "numlock") {
  154. var event = new KeyboardEvent("keydown", {
  155. key: "NumLock",
  156. code: "NumLock",
  157. });
  158. document.dispatchEvent(event);
  159. } else if (key === "scrolllock") {
  160. var event = new KeyboardEvent("keydown", {
  161. key: "ScrollLock",
  162. code: "ScrollLock",
  163. });
  164. document.dispatchEvent(event);
  165. } else if (key === "capslock") {
  166. var event = new KeyboardEvent("keydown", {
  167. key: "CapsLock",
  168. code: "CapsLock",
  169. });
  170. document.dispatchEvent(event);
  171. }
  172. return this;
  173. };
  174. }
  175.  
  176. input: {
  177. // Block input
  178. this.blockInput = function () {
  179. document.addEventListener("keydown", function (event) {
  180. event.preventDefault();
  181. });
  182. document.addEventListener("mousedown", function (event) {
  183. event.preventDefault();
  184. });
  185. return this;
  186. };
  187. }
  188.  
  189. timer: {
  190. // Timer events
  191. this.wait = function (ms) {
  192. var start = new Date().getTime();
  193. var end = start;
  194. while (end < start + ms) {
  195. end = new Date().getTime();
  196. }
  197. return this;
  198. };
  199.  
  200. this.waitForElement = function (selector) {
  201. var element = document.querySelector(selector);
  202. while (!element) {
  203. element = document.querySelector(selector);
  204. }
  205. return this;
  206. };
  207.  
  208. this.waitForMouse = function (cursor) {
  209. var currentCursor = document.body.style.cursor;
  210. while (currentCursor !== cursor) {
  211. currentCursor = document.body.style.cursor;
  212. }
  213. return this;
  214. };
  215. }
  216.  
  217. // Conditionals
  218. this.ifElement = function (selector, condition, value) {
  219. var element = document.querySelector(selector);
  220. if (condition === "contains") {
  221. if (element.innerHTML.includes(value)) {
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. } else if (condition === "does not contain") {
  227. if (!element.innerHTML.includes(value)) {
  228. return true;
  229. } else {
  230. return false;
  231. }
  232. } else if (condition === "is") {
  233. if (element.innerHTML === value) {
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. } else if (condition === "is not") {
  239. if (element.innerHTML !== value) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. return this;
  246. };
  247.  
  248. dialogs: {
  249. // Dialogs/Message Boxes
  250. this.showNotification = function (title, text) {
  251. var notification = new Notification(title, {
  252. body: text,
  253. });
  254. return this;
  255. };
  256.  
  257. this.showDialog = function (title, text) {
  258. var dialog = document.createElement("dialog");
  259. var titleElement = document.createElement("strong");
  260. titleElement.innerHTML = title;
  261. var textElement = document.createElement("p");
  262. textElement.innerHTML = text;
  263. dialog.appendChild(titleElement);
  264. dialog.appendChild(textElement);
  265. document.body.appendChild(dialog);
  266. dialog.show();
  267. return this;
  268. };
  269.  
  270. this.showCustomDialog = function (html) {
  271. var dialog = document.createElement("dialog");
  272. dialog.innerHTML = html;
  273. document.body.appendChild(dialog);
  274. dialog.show();
  275. return this;
  276. };
  277. }
  278.  
  279. clipboard: {
  280. // Clipboard
  281. this.getClipboardText = function () {
  282. return navigator.clipboard.readText().then((text) => {
  283. return text;
  284. });
  285. return this;
  286. };
  287.  
  288. this.setClipboardText = function (text) {
  289. navigator.clipboard.writeText(text);
  290. return this;
  291. };
  292.  
  293. this.clearClipboard = function () {
  294. navigator.clipboard.writeText("");
  295. return this;
  296. };
  297. }
  298. router: {
  299. // function to handle different actions based on URL
  300. this.ifUrl = function(url, action) {
  301. if (url === '/' && window.location.pathname === '/') {
  302. action();
  303. } else if (window.location.href === url) {
  304. action();
  305. }
  306. };
  307. // function to navigate to a specified URL
  308. this.navigate = function(url) {
  309. window.location.href = url;
  310. };
  311. // function to expose current URL
  312. currentUrl: {
  313. this.get_domain = function get_domain() {
  314. return window.location.hostname;
  315. };
  316. this.get_protocol =function get_protocol() {
  317. return window.location.protocol;
  318. };
  319. this.get_page = function get_page() {
  320. return window.location.pathname;
  321. };
  322. this.get_query = function get_query() {
  323. return window.location.search;
  324. };
  325. }
  326. }
  327.  
  328. }

QingJ © 2025

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