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

As of 2017-02-27. See the latest version.

// ==UserScript==
// @name        Add Keyboard Shortcut for Generic Next/Previous Page
// @namespace   AddKeyboardShortcutForGenericNextPreviousPage
// @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".
// @author      jcunews
// @include     *://*/*
// @version     1.0.3
// @grant       none
// ==/UserScript==

addEventListener("keydown", function(ev) {
  var rxPrevious = /^prev(ious)?\b|\bprev(ious)?$/i;
  var rxNext = /^next\b/i;

  function clickLink(rx) {
    for (var i = document.links.length-1; i >= 0; i--) {
      if (rx.test(document.links[i].textContent.trim())) {
        ev.preventDefault();
        document.links[i].click();
        return true;
      }
    }
    return false;
  }

  if (ev.ctrlKey && !ev.altKey && !ev.shiftKey) {
    if (document.activeElement && (
        (/^(INPUT|TEXTAREA)$/).test(document.activeElement.tagName) ||
        document.activeElement.isContentEditable)) return;
    switch (ev.key) {
      case "ArrowLeft": //previous
        if (clickLink(rxPrevious)) return;
        break;
      case "ArrowRight": //next
        if (clickLink(rxNext)) return;
        break;
    }
  }
}, false);