APUG.org show image keyboard control

Keyboard navigation for APUG user gallery images.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        APUG.org show image keyboard control
// @namespace   http://oscar.carlsson.photography/
// @description Keyboard navigation for APUG user gallery images.
// @include     http://www.apug.org/gallery1/showimage.php?i=*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @version     1
// @grant       none
// ==/UserScript==
function find_links() {
  links = {};
  
  $('div.smallfont a').each(function (e) {
    if ($(this) [0].textContent === 'Previous Image')
      links.prev = $(this) [0];

    if ($(this) [0].textContent === 'Next Image')
      links.next = $(this) [0];
  });
  return links;
}

function upwards() {
  var breadcrumbs = $('div#breadcrumb li.navbit'),
      previous = null;
  for (var index = 0; index < breadcrumbs.length; index++) {
    var breadcrumb = breadcrumbs[index],
        classes = breadcrumb.className.split(/\s+/);
    
    if ($.inArray('lastnavbit', classes) === - 1) {
      previous = breadcrumb;
      continue;
    } else {
      // fetch link from the previous element
      var children = $(previous).children();
      for (var idx = 0; idx < children.length; idx++) {
        var child = children[idx],
        url = child.href;
        if (url) window.location.assign(url);
      }
    }
  }
}

$(window).keyup(function (e) {
  var key = e.keyCode;

  if (key == 39 || key == 37)
    var links = find_links();

  if (key == 37) {
    window.location.assign(links.prev.href);
    return;
  }
  if (key == 39) {
    window.location.assign(links.next.href);
    return;
  }
  if (key == 85) { // u
    upwards();
    return;
  }
});