Add Keyboard Shortcut for Generic Next/Previous Page

Add CTRL+ArrowLeft and CTRL+ArrowRight for generic next/previous page. It will click the last found link/button whose text starts/ends with e.g. "Next", "Prev", "Back", or "Previous".

目前为 2023-09-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @version 1.0.18
  5. // @license GNU AGPLv3
  6. // @author jcunews
  7. // @description Add CTRL+ArrowLeft and CTRL+ArrowRight for generic next/previous page. It will click the last found link/button whose text starts/ends with e.g. "Next", "Prev", "Back", or "Previous".
  8. // @website https://greasyfork.org/en/users/85671-jcunews
  9. // @include *://*/*
  10. // @include *:*
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. /*
  15. The link/button text more specifically, are those which starts with (non case sesitive) "Next", "Prev", "Previous";
  16. or ends with "Prev", "Back", "Previous", "Next". e.g. "Next", "> Next", "Next Page", "Prev", "< Prev", "< Previous", etc.
  17. but not "< Prev Page" because the word "prev" or "previous" is not at the start/end of text.
  18. The ID and class names are also checked.
  19.  
  20. This script doesn't take into account of links whose contents is an image rather than text, or whose text is a CSS text contents.
  21.  
  22. If next/previous navigation link is specified in the HTML metadata, it will be used as a priority.
  23. */
  24.  
  25. (function(rxPrev, rxNext, ts) {
  26. rxPrevious = /\u00ab|(\b|_)(?:back|prev(ious)?)(\b|_)/i;
  27. rxNext = /\u00bb|(\b|_)next(\b|_)/i;
  28. rxCarousel = /carousel/i;
  29.  
  30. addEventListener("keydown", function(ev, e, a) {
  31.  
  32. function clickLink(rx, e, i, l, r, rx2) {
  33. rx2 = new RegExp("[a-z].*?(" + rx.source + ")", "i");
  34. e = document.querySelectorAll('a,button,input[type="button"],input[type="submit"]');
  35. for (i = e.length - 1; i >= 0; i--) {
  36. if (
  37. (
  38. ((e[i].tagName === "A") && rx.test(e[i].rel) && !rx2.test(e[i].rel)) ||
  39. ((e[i].tagName === "A") && Array.from(e[i].classList).some(cl => rx.test(cl) && !rx2.test(cl))) ||
  40. ((e[i].tagName === "INPUT") && rx.test(e[i].value) && !rx2.test(e[i].value)) ||
  41. (rx.test(e[i].textContent) && !rx2.test(e[i].textContent)) ||
  42. (e[i].id && rx.test(e[i].id)) ||
  43. Array.from(e[i].classList).some(s => rx.test(s))
  44. ) && (!rxCarousel.test(e[i].className))
  45. ) {
  46. ev.preventDefault();
  47. e[i].click();
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53.  
  54. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  55. a = document.activeElement;
  56. while (a && a.shadowRoot?.activeElement) a = a.shadowRoot.activeElement;
  57. if (a && ((/^(INPUT|TEXTAREA)$/).test(a.tagName) || a.isContentEditable)) return;
  58. switch (ev.key) {
  59. case "ArrowLeft": //previous
  60. if (e = document.querySelector('link[rel="prev"][href]')) {
  61. ev.preventDefault();
  62. location.href = e.href;
  63. return;
  64. }
  65. if (clickLink(rxPrevious)) return;
  66. break;
  67. case "ArrowRight": //next
  68. if (e = document.querySelector('link[rel="next"][href]')) {
  69. ev.preventDefault();
  70. location.href = e.href;
  71. return;
  72. }
  73. if (clickLink(rxNext)) return;
  74. break;
  75. }
  76. }
  77. }, true);
  78. })();