Next Previous Image Key Navigation

Quick scroll to next/previous image on a page with f/r buttons

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name Next Previous Image Key Navigation
// @author waka <[email protected]>
// @namespace http://waka.name/
// @version 1.0.1
// @description  Quick scroll to next/previous image on a page with f/r buttons
// @include *
// ==/UserScript==

(function(){
  var sizeLimit = 200;
  var rButton = 114;
  var fButton = 102;
  var positions = [];
  var offsetshift = 0;
  //shift pixels down to avoid floating bars - format: domain,pixels,domain,pixels,...
  //example: http://foo.example.com/ -> domain=example - assume 25 pixels shift -> ,"example","25"
  var shift=["facebook","40","sankakucomplex","25","soup","34","reddit","50"];
  
  document.addEventListener('keypress', keypressHandler, false);
  
  function keypressHandler(event){
    if (event.ctrlKey || event.shiftKey || event.altKey) return;
    if (event.target.tagName && event.target.tagName.match(/input|select|textarea/i)) return;

    var code = event.keyCode || event.which;
    if (code != rButton && code != fButton) return;

    if (positions.length < document.images.length) {
      positions = [];
      for (var index = 0; index < document.images.length; index++) {
        var image = document.images[index];
        if (Math.min(image.width, image.height) < sizeLimit) continue;
        positions.push(getYOffset(image));
      }
    }

    var scroll = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

    positions = positions.sort(sort);
    var next = true;

    if (code == rButton) {
      positions = positions.reverse();
      next = false;
    }
    
    var domain = window.location.host.split('.')[1];
    var length = shift.length,
    element = null;
    for (var i = 0; i < length; i++) {
        element = shift[i];
        // Do something with element i.
        if (domain == element) {
            p = i + 1;
            offsetshift = shift[p];
        }
        i++;
    }

    for (index = 0; index < positions.length; index++) {
      var offset = positions[index] - offsetshift;
      if ((next && offset <= scroll) || (!next && offset >= scroll)) continue;
      scrollTo(offset, scroll);
      return;
    }
  }
  
  function scrollTo(offset, currentScroll) {
    if (currentScroll == document.documentElement.scrollTop) {
      document.documentElement.scrollTop = offset;
    } else {
      document.body.scrollTop = offset;
    }
  }
  
  function getYOffset(node) {
    for (var offset = 0; node; offset += node.offsetTop, node = node.offsetParent);
    return offset;
  }
  
  function sort(a, b) { return a < b ? -1 : 1; }
})()