E621 helper v2.0 (2020.06)

Script to help navigating e621 (created in 2020.06)

// ==UserScript==
// @name E621 helper v2.0 (2020.06)
// @namespace Violentmonkey Scripts
// @match https://e621.net/posts/*
// @grant none
// @description:en Script to help navigating e621 (created in 2020.06)
// @version 0.0.1.20191009221730
// @description Script to help navigating e621 (created in 2020.06)
// ==/UserScript==


var prev_pool = null;
var prev_search = null;

var next_pool = null;
var next_search = null;

var download = null;
var resize = null;

var image = null;

function keyPress(kk){
  //Left Arrow
  if(kk == 37 && prev_pool){
    prev_pool.click();
  //Right Arrow
  } else if(kk == 39 && next_pool){
    if(checkResize()){ goWithResize(); }
    else if(next_pool){ next_pool.click(); }
  //Numeric 1
  } else if(kk == 97 && prev_search){
    prev_search.click();
  //Numeric 3
  } else if(kk == 99 && next_search){
    if(checkResize()){ window.location.href = next_search.href + "##"; }
    else if(next_search){ next_search.click(); }
  //Shift
  } else if(kk == 96 && resize){
    resize.click();
  //Enter
  } else if(kk == 13 && download){
    download.click();
  }
}

function getWidth(height, ow, oh){
  var ratio = oh / ow;
  return ratio * height;
}

window.addEventListener("load", function(){
  image = document.getElementById("image");
  start();
});

function goWithResize(){
  if(next_pool.href){
    window.location.href = next_pool.href + "##";
  }else if(next_search.href){
    window.location.href = next_search.href + "##";
  }
}

function checkResize(){
  return window.location.href.indexOf("##") > -1;
}

function doResize(){
  if(checkResize()){ resize.click(); }
}

function start(){
  window.addEventListener("keydown", function(e){
    e = e || window.event;
    if(e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 13 || e.keyCode == 96 || e.keyCode == 97 || e.keyCode == 99){
      keyPress(e.keyCode);
    } else if(e.keyCode == 32 ){
      goWithResize();
    }
  });
  
  image.addEventListener("click", function(e){
    goWithResize();
  });
  
  var search_nav = document.getElementById("search-seq-nav");  
  var pool_nav = document.getElementById("pool-nav");

  if(search_nav){
    prev_search = search_nav.children[0].children[0].children[0];
    next_search = search_nav.children[0].children[0].children[2];
  }
  if(pool_nav){
    prev_pool = pool_nav.children[0].children[0].children[1];
    next_pool = pool_nav.children[0].children[0].children[3];
  }
  
  download = document.getElementById("image-download-link").children[0];
  resize = document.getElementById("current-image-resize-mode");
  
  doResize();
  
  sidebar = document.getElementsByClassName("sidebar")[0];
  img = document.getElementById("image");
  
  console.log("E621 helper loaded :)");
  console.log("=================================");
  console.log("Key layout:");
  console.log("\tPrevious pool post: Left arrow key.");
  console.log("\tNext pool post: Right arrow key.");
  console.log("\tPrevious search post: Numeric 1 key.");
  console.log("\tNext search post: Numeric 3 key.");
  console.log("\tResize image: Numeric 0 key.");
  console.log("\tDownload image: Enter key.");
  console.log("\tSpace key and clicking the image will attempt the next pool image, ");
  console.log("\t\tfailing that, the next search image. It will also automatically");
  console.log("\t\tchange the picture size to \"Sample\".");
  console.log("=================================");
  
  console.log("Have fun~");
  console.log("\t\t-WolfyD");
}