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-02-01 提交的版本。查看 最新版本

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

QingJ © 2025

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