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

目前为 2019-12-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @version 1.0.6
  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 = /^prev(ious)?\b|\bprev(ious)?$/i;
  22. rxNext = /^next\b|\bnext$/i;
  23. rxCarousel = /carousel/i;
  24.  
  25. addEventListener("keydown", function(ev) {
  26.  
  27. function clickLink(rx, e, i, r) {
  28. e = document.querySelectorAll('a,button,input[type="button"],input[type="submit"]');
  29. for (i = e.length-1; i >= 0; i--) {
  30. if (
  31. (
  32. ((e[i].tagName === "A") && rx.test(e[i].getAttribute("rel"))) ||
  33. ((e[i].tagName === "INPUT") && rx.test(e[i].getAttribute("value"))) ||
  34. ((e[i].tagName !== "INPUT") && rx.test(e[i].textContent.trim()))
  35. ) && (!rxCarousel.test(e[i].className))
  36. ) {
  37. ev.preventDefault();
  38. e[i].click();
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44.  
  45. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  46. if (document.activeElement && (
  47. (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
  48. document.activeElement.isContentEditable)) return;
  49. switch (ev.key) {
  50. case "ArrowLeft": //previous
  51. if (clickLink(rxPrevious)) return;
  52. break;
  53. case "ArrowRight": //next
  54. if (clickLink(rxNext)) return;
  55. break;
  56. }
  57. }
  58. });
  59. })();