MrTimcakes / Center Image

// ==UserScript==
// @name          Center Image
// @namespace     CenterImage
// @author        MrTimcakes AKA Ducky <Ducke.uk>
// @version       2.0.1
// @updateURL     https://openuserjs.org/install/MrTimcakes/Center_Image.user.js
// @downloadURL   https://openuserjs.org/install/MrTimcakes/Center_Image.user.js
// @homepage      https://github.com/MrTimcakes/CenterImage
// @description   Centers images in both directions when opened directly in the browser.
// @icon          https://raw.githubusercontent.com/MrTimcakes/CenterImage/master/Icon48.png
// @icon64        https://raw.githubusercontent.com/MrTimcakes/CenterImage/master/Icon64.png
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_registerMenuCommand
// @noframes
// @run-at        document-start
// @include       *
// @include       file:///*
// ==/UserScript==

var images = document.images;
if(!images || images.length !== 1 || images[0].src !== location.href){return false;}

function $(id) {return document.getElementById(id);}

"undefined"!==typeof GM_registerMenuCommand&&GM_registerMenuCommand("Center Image Configuration",cfg,"c");

function cfg(){
	if(typeof GM_setValue !== "undefined")
	{
		function saveCfg()
		{
			GM_setValue("bgColor", $("bgColor").value);
			GM_setValue("fitBoth", $("fitBoth").checked);
			GM_setValue("fitEither", $("fitEither").checked);
			GM_setValue("fitSmaller", $("fitSmaller").checked);
			GM_setValue("js", $("customJs").value);
			alert("Configuration Saved");
			if($("bgColor").value){document.body.bgColor = $("bgColor").value;}else{document.body.removeAttribute("bgColor");}
		}
		if(img){img.removeEventListener("click", rescale, true);}
		window.removeEventListener("keydown", onkeydown, true);
		if(document.head){document.head.innerHTML = "";}
		document.body.innerHTML = '<style>@import "http://fonts.googleapis.com/css?family=Raleway";body{background-color:#DDD}main{background-color:#64C8FA;border:1px solid #AAA;font-family:"Raleway",sans-serif;text-align:center;margin:0 auto;padding:0 50px;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)}#options{text-align:left}input{display:inline-block}p{margin-bottom:2.5px}label[for="customJs"]{display:block;text-align:center}textarea{display:block;margin:0 auto;margin-bottom:5px}#save{display:block;text-align:center;margin:0 auto;margin-bottom:5px}</style><main>\n<h1>Configuration</h1>\n<div id="options">\n<label for="bgColor">Background Color: </label><input id="bgColor" type="color" /><br>\n<p>Fit images to window if:</p>\n<input type="checkbox" id="fitBoth" /><label for="fitBoth">Larger than window in both width and height</label><br>\n<input type="checkbox" id="fitEither" /><label for="fitEither">Larger than window in width or height</label><br>\n<input type="checkbox" id="fitSmaller" /><label for="fitSmaller">Smaller than window</label><br>\n<label for="customJs">Custom javascript:</label>\n<textarea id="customJs" cols="45" rows="5"></textarea>\n<button id="save">Save Configuration</button>\n</div>\n</main>';
      
		$("bgColor").value = GM_getValue("bgColor", "");
		$("fitBoth").checked = GM_getValue("fitBoth", true);
		$("fitEither").checked = GM_getValue("fitEither", true);
		$("fitSmaller").checked = GM_getValue("fitSmaller");
		$("customJs").value = GM_getValue("js", "");
		$("save").addEventListener("click", saveCfg, true);
	}
	else{
		alert("Sorry, Chrome userscripts in native mode can't have configurations! Install TamperMonkey extension. (it's very good)");
	}
}

var bgColor;
var fitBoth = true;
var fitEither = true;
var fitSmaller;
var customJs;
if(typeof GM_getValue !== "undefined")
{
	bgColor = GM_getValue("bgColor");
	fitBoth = GM_getValue("fitBoth", true);
	fitEither = GM_getValue("fitEither", true);
	fitSmaller = GM_getValue("fitSmaller");
	customJs = GM_getValue("js", "");
}

var images = document.images;
if(!images || images.length !== 1 || images[0].src !== location.href){return false;}

document.head.innerHTML = "<style>html{overflow:auto!important}body{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAABYBAMAAACDuy0HAAAAG1BMVEX+/v4BAQH///8KCgoDAwN/f3/19fWAgID8/PzhDwT2AAAACXRSTlMFBQUFBQUFBQWHDtP9AAALwklEQVR4Xg3KOWOyWhAA0Bn2ci57eXEvQY1JCZp8sQTjVoJLTAkaE0swbj/7ve4UB37FLW4q86Lwwlh86J/ASAkpWaj+Krbb31HzH0Kjc2tIl7SADaWbpZBPE5dds6jJNyNdjAyKWqdroIixWRQIY6E/kOY7hIciL/ZfrAO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bAaeGASGV9bh/X2wPsVTmBLxmTTQsBGFkEOkZJTsGAm+HrtMDbWwvTXOutX1u7BxIq9Xib6DkFMbUitNdrYsULkahsAhBEh9FjdzL9BNARxTSr7T3u1rE+IWUmCIpwTZHZCu5l9THCuCcOhZqfekuQxjQ7EoyGUJAwCv/q1JOuJeCc/3lknb76zAquO/DAQhK/62cP8X2s3+IBLIhvL8RHopoHpIArJysYTTmMMeubPXh8W760AvMVH67jqgg06+/ne5MZ631z6yROhloh3dPQirZoEpr80wgt/cEbhbAQTmRLtGh8lxCwDBBb5OeJ4aEq25XBNMT2rzWedW2zIzj+CCDKlnlyJBzT81qBWp69h7vlb3TmEV+DNm2rqj1iT7BQuwVVsuPkwq1e5P8tgNjVbIlMzwXeM11kZqjx3KKFOJzc3CAyFVhi8fxVZ5FvhdAM5mM6kS6OgKu16MFglq3/b/QVIwdw7HUCyeW04JPjC5dO+GC9OfqfB4VX+wwuift+ths2Ss3i6nkOE+JFyD+wKFL+WMX6nwwDva0S1/O8Mlnida69Ph96fuFvCoRMvXnCfsLPPmC/hA5RnMNE4fDK0pVOQ4BHLaErzv/wD99ABmjNZk0AAAAABJRU5ErkJggg==) repeat scroll 0 0 #222!important;overflow:hidden;text-align:center!important;color:#EEE!important}img{border:medium solid #272727!important;margin:0 auto!important;position:absolute!important;transform:translate(-50%,-50%)!important}img.zoom-in{cursor:-moz-zoom-in;cursor:-webkit-zoom-in!important;cursor:zoom-in}img.zoom-out{cursor:-moz-zoom-out;cursor:-webkit-zoom-out!important;cursor:zoom-out}img.drag{cursor:move!important}</style>"; // center image
images[0].style.top = "49.5%";
images[0].style.left = "50%";
document.body.height = images[0].naturalHeight;
document.body.width = images[0].naturalWidth;

function cancelEvent(a){
	a = a ? a : window.event;
	if(a.stopPropagation){
		a.stopPropagation();
	}
	if(a.preventDefault){
		a.preventDefault();
	}
	a.cancelBubble = true;
	a.cancel = true;
	a.returnValue = false;
	return false;
}

KeyEvent = {
  KEY_SPACE: 32,
  KEY_LEFT: 37,
  KEY_UP: 38,
  KEY_RIGHT: 39,
  KEY_DOWN: 40,
  KEY_A: 65,
  KEY_D: 68,
  KEY_P: 80,
  KEY_Q: 81,
  KEY_S: 83,
  KEY_W: 87,
  KEY_NUMPAD2: 98,
  KEY_NUMPAD4: 100,
  KEY_NUMPAD5: 101,
  KEY_NUMPAD6: 102,
  KEY_NUMPAD8: 104
};

function onkeydown(b){
	var a = (window.event) ? b.keyCode : b.which;
	
	if(a != KeyEvent.KEY_SPACE && (b.altKey || b.ctrlKey || b.metaKey))
	{
		return;
	}
		
	switch (a)
	{
	case KeyEvent.KEY_RIGHT:
	case KeyEvent.KEY_D:
	case KeyEvent.KEY_NUMPAD6:
		images[0].style.left = parseInt(images[0].style.left) + 1 + '%';
		cancelEvent(b);
		break;
	case KeyEvent.KEY_LEFT:
	case KeyEvent.KEY_A:
	case KeyEvent.KEY_NUMPAD4:
		images[0].style.left = parseInt(images[0].style.left) - 1 + '%';
		cancelEvent(b);
		break;
	case KeyEvent.KEY_UP:
    case KeyEvent.KEY_W:
	case KeyEvent.KEY_NUMPAD8:
		images[0].style.top = parseInt(images[0].style.top) - 1 + '%';
		cancelEvent(b);
		break;
	case KeyEvent.KEY_S:
    case KeyEvent.KEY_DOWN:
	case KeyEvent.KEY_NUMPAD2:
		images[0].style.top = parseInt(images[0].style.top) + 1 + '%';
		cancelEvent(b);
		break;
	case KeyEvent.KEY_SPACE:
		images[0].style.top = "50%";
        images[0].style.left = "50%";
		cancelEvent(b);
		break;
	case KeyEvent.KEY_Q:
	case KeyEvent.KEY_NUMPAD5:
		//rescale(0);
		cancelEvent(b);
		break;
	case KeyEvent.KEY_P:
		cfg();
		cancelEvent(b);
	}
}

window.addEventListener("keydown", onkeydown, true);