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", or "Previous".

目前为 2020-12-17 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @version 1.0.10
  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", or "Previous".
  8. // @include *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /*
  13. The link/button text more specifically, are those which starts with (non case sesitive) "Next", "Prev", "Previous";
  14. or ends with "Prev", "Previous", "Next". e.g. "Next", "> Next", "Next Page", "Prev", "< Prev", "< Previous", etc.
  15. but not "< Prev Page" because the word "prev" or "previous" is not at the start/end of text.
  16.  
  17. 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.
  18. */
  19.  
  20. (function(rxPrev, rxNext) {
  21. rxPrevious = /\bprev(ious)?\b/i;
  22. rxNext = /\bnext\b/i;
  23. rxCarousel = /carousel/i;
  24.  
  25. addEventListener("keydown", function(ev, e) {
  26.  
  27. function clickLink(rx, e, i, l, r) {
  28. e = document.querySelectorAll('a,button,input[type="button"],input[type="submit"]');
  29. for (i = 0; i < e.length; i++) {
  30. if (
  31. (
  32. ((e[i].tagName === "A") && rx.test(e[i].getAttribute("rel"))) ||
  33. ((e[i].tagName === "A") && Array.from(e[i].classList).some(cl => rx.test(cl))) ||
  34. ((e[i].tagName === "INPUT") && rx.test(e[i].getAttribute("value"))) ||
  35. ((e[i].tagName !== "INPUT") && rx.test(e[i].textContent.trim()))
  36. ) && (!rxCarousel.test(e[i].className))
  37. ) {
  38. ev.preventDefault();
  39. e[i].click();
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45.  
  46. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  47. if (document.activeElement && (
  48. (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
  49. document.activeElement.isContentEditable)) return;
  50. switch (ev.key) {
  51. case "ArrowLeft": //previous
  52. if (e = document.querySelector('link[rel="prev"][href]')) {
  53. ev.preventDefault();
  54. location.href = e.href;
  55. return;
  56. }
  57. if (clickLink(rxPrevious)) return;
  58. break;
  59. case "ArrowRight": //next
  60. if (e = document.querySelector('link[rel="next"][href]')) {
  61. ev.preventDefault();
  62. location.href = e.href;
  63. return;
  64. }
  65. if (clickLink(rxNext)) return;
  66. break;
  67. }
  68. }
  69. });
  70. })();

QingJ © 2025

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