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

  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.2
  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. console.log(document.activeElement);
  28. console.log(document.activeElement.tagName, document.activeElement.isContentEditable);
  29. if (document.activeElement && (
  30. (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
  31. document.activeElement.isContentEditable)) return;
  32. switch (ev.key) {
  33. case "ArrowLeft": //previous
  34. if (clickLink(rxPrevious)) return;
  35. break;
  36. case "ArrowRight": //next
  37. if (clickLink(rxNext)) return;
  38. break;
  39. }
  40. }
  41. }, false);