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".

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @version 1.0.19
  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://gf.qytechs.cn/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.stopImmediatePropagation();
  47. ev.stopPropagation();
  48. ev.preventDefault();
  49. e[i].click();
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55.  
  56. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  57. a = document.activeElement;
  58. while (a && a.shadowRoot?.activeElement) a = a.shadowRoot.activeElement;
  59. if (a && ((/^(INPUT|TEXTAREA)$/).test(a.tagName) || a.isContentEditable)) return;
  60. switch (ev.key) {
  61. case "ArrowLeft": //previous
  62. if (e = document.querySelector('link[rel="prev"][href]')) {
  63. ev.stopImmediatePropagation();
  64. ev.stopPropagation();
  65. ev.preventDefault();
  66. location.href = e.href;
  67. return
  68. }
  69. if (clickLink(rxPrevious)) return;
  70. break;
  71. case "ArrowRight": //next
  72. if (e = document.querySelector('link[rel="next"][href]')) {
  73. ev.stopImmediatePropagation();
  74. ev.stopPropagation();
  75. ev.preventDefault();
  76. location.href = e.href;
  77. return
  78. }
  79. if (clickLink(rxNext)) return;
  80. break
  81. }
  82. }
  83. }, true)
  84. })()

QingJ © 2025

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