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-12 提交的版本,查看 最新版本

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