PageAutomator

Automate the actions on the page

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

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