Arrow Keys Flip Pages

Trigger Pages Flips with custom arrow key shortcuts

当前为 2024-09-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Arrow Keys Flip Pages
  3. // @namespace https://4kliksAlex.github.com/
  4. // @version v0.2.2
  5. // @description Trigger Pages Flips with custom arrow key shortcuts
  6. // @author 4kliksAlex
  7. // @match *://*/*
  8. // @icon https://upload.wikimedia.org/wikipedia/commons/c/c7/Computer_keyboard_German-key-4.svg
  9. // @grant none
  10. // @license GPLv3
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. "use strict";
  15.  
  16. const selectors = {
  17. "*://*.oracle.com/javase/tutorial/*": {
  18. ArrowLeft: "body > div.MainFlow_wide > div:nth-child(6) > a:nth-child(1)",
  19. ArrowRight: "body > div.MainFlow_wide > div:nth-child(6) > a:nth-child(3)",
  20. },
  21. // "*://*.google.com/search?*": {
  22. // ArrowLeft: "#pnprev",
  23. // ArrowRight: "#pnnext",
  24. // },
  25. "*://*.leetcode.*/leetbook/read/*": {
  26. ArrowLeft: "#lc-home > div > div.css-17lel75-layer1-Container.e19m4yof0 > div.css-1qeh80o-MainContainer.e19m4yof1 > div.css-op50bu-ContentLayoutContainer.e1t6ajs0 > div > div.css-1p8jlme-ContentContainer.e12hyigg1 > div:nth-child(4) > div.css-ob1q0v-Container.elt4qmr0 > a:nth-child(1)",
  27. ArrowRight: "#lc-home > div > div.css-17lel75-layer1-Container.e19m4yof0 > div.css-1qeh80o-MainContainer.e19m4yof1 > div.css-op50bu-ContentLayoutContainer.e1t6ajs0 > div > div.css-1p8jlme-ContentContainer.e12hyigg1 > div:nth-child(4) > div.css-ob1q0v-Container.elt4qmr0 > a:nth-child(2)"
  28. },
  29. "*://docs.spring.io/**": {
  30. ArrowLeft: "body > div.body > main > div.content > article > nav > span.next > a",
  31. ArrowRight: "body > div.body > main > div.content > article > nav > span.prev > a",
  32. }
  33. };
  34.  
  35. function getSelectorsForCurrentURL() {
  36. const url = window.location.href;
  37. for (const pattern in selectors) {
  38. const urlPattern = new URLPattern(pattern, {
  39. ignoreCase: true,
  40. });
  41. const patternMatch = urlPattern.test(url);
  42. console.info(pattern, patternMatch);
  43. if (patternMatch) {
  44. return selectors[pattern];
  45. }
  46. }
  47. return null;
  48. }
  49.  
  50. const currentSelectors = getSelectorsForCurrentURL();
  51. if (currentSelectors) {
  52. document.addEventListener("keydown", function (event) {
  53. const selector = currentSelectors[event.key];
  54. if (selector) {
  55. event.preventDefault();
  56. const element = document.querySelector(selector);
  57. if (element) {
  58. element.click();
  59. }
  60. }
  61. });
  62. }
  63. })();