APUG.org show image keyboard control

Keyboard navigation for APUG user gallery images.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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;
  }
});