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

目前为 2021-12-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @version 1.0.12
  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.  
  19. 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.
  20.  
  21. If next/previous navigation link is specified in the HTML metadata, it will be used as a priority.
  22. */
  23.  
  24. (function(rxPrev, rxNext, ts) {
  25. rxPrevious = /\b(?:back|prev(ious)?)\b/i;
  26. rxNext = /\bnext\b/i;
  27. rxCarousel = /carousel/i;
  28.  
  29. addEventListener("keydown", function(ev, e) {
  30.  
  31. function clickLink(rx, e, i, l, r) {
  32. e = document.querySelectorAll('a,button,input[type="button"],input[type="submit"]');
  33. for (i = 0; i < e.length; i++) {
  34. if (
  35. (
  36. ((e[i].tagName === "A") && rx.test(e[i].rel)) ||
  37. ((e[i].tagName === "A") && Array.from(e[i].classList).some(cl => rx.test(cl))) ||
  38. ((e[i].tagName === "INPUT") && rx.test(e[i].value)) ||
  39. rx.test(e[i].textContent)
  40. ) && (!rxCarousel.test(e[i].className))
  41. ) {
  42. ev.preventDefault();
  43. e[i].click();
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  51. if (document.activeElement && (
  52. (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
  53. document.activeElement.isContentEditable)) return;
  54. switch (ev.key) {
  55. case "ArrowLeft": //previous
  56. if (e = document.querySelector('link[rel="prev"][href]')) {
  57. ev.preventDefault();
  58. location.href = e.href;
  59. return;
  60. }
  61. if (clickLink(rxPrevious)) return;
  62. break;
  63. case "ArrowRight": //next
  64. if (e = document.querySelector('link[rel="next"][href]')) {
  65. ev.preventDefault();
  66. location.href = e.href;
  67. return;
  68. }
  69. if (clickLink(rxNext)) return;
  70. break;
  71. }
  72. }
  73. }, true);
  74. })();