PageAutomator

Automate the actions on the page

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

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

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