joelbu / imgur scroll by image

// ==UserScript==
// @name         imgur scroll by image
// @namespace    ch.joelbu.imgur-scroll-by-image
// @description  This helps to scroll through an imgur album image by image with keypresses on left and right arror or comma and period
// @author       joelbu
// @match        http://imgur.com/*
// @match        https://imgur.com/*
// @license      MIT
// @copyright    2018, joelbu (https://openuserjs.org//users/joelbu)
// ==/UserScript==

// Set to false if you do not want to override the left and right key functions (changing albums)
var overrideLeftRight = true;

var currentId = document.querySelector('.post-image-container').id;
var headerHidden = false;

function getCurrentVisible() {
  // Usually there is one invisible picture loaded in the DOM so current is nextSibling of first match
  return document.querySelector('.post-image-container').nextSibling;
}

window.addEventListener("keydown", function (e) {
  var key = e.keyCode ? e.keyCode : e.which;

  if (!headerHidden) {
    document.querySelector('.post-header').style.visibility = "hidden";
    headerHidden = true;
  }

  if (key == 190 || (overrideLeftRight && key == 39)) {
    e.preventDefault();
    e.stopPropagation();
    var current = document.getElementById(currentId);
    if (null == current) {
      current = getCurrentVisible()
    }
    var next = current.nextSibling;
    next.scrollIntoView();
    currentId = next.id;

  }
  else if (key == 188 || (overrideLeftRight && key == 37)) {
    e.preventDefault();
    e.stopPropagation();
    var current = document.getElementById(currentId);
    if (null == current) {
      current = getCurrentVisible()
    }
    var previous = current.previousSibling;
    previous.scrollIntoView();
    currentId = previous.id;
  }

}, true);