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 whose text starts/ends with e.g. "Next", "Prev", or "Previous".

目前為 2017-02-27 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Add Keyboard Shortcut for Generic Next/Previous Page
  3. // @namespace AddKeyboardShortcutForGenericNextPreviousPage
  4. // @description Add CTRL+ArrowLeft and CTRL+ArrowRight for generic next/previous page. It will click the last found link whose text starts/ends with e.g. "Next", "Prev", or "Previous".
  5. // @author jcunews
  6. // @include *://*/*
  7. // @version 1.0.3
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. addEventListener("keydown", function(ev) {
  12. var rxPrevious = /^prev(ious)?\b|\bprev(ious)?$/i;
  13. var rxNext = /^next\b/i;
  14.  
  15. function clickLink(rx) {
  16. for (var i = document.links.length-1; i >= 0; i--) {
  17. if (rx.test(document.links[i].textContent.trim())) {
  18. ev.preventDefault();
  19. document.links[i].click();
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
  27. if (document.activeElement && (
  28. (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
  29. document.activeElement.isContentEditable)) return;
  30. switch (ev.key) {
  31. case "ArrowLeft": //previous
  32. if (clickLink(rxPrevious)) return;
  33. break;
  34. case "ArrowRight": //next
  35. if (clickLink(rxNext)) return;
  36. break;
  37. }
  38. }
  39. }, false);