Manga cleaner & loader

img:click/arrow-key, back:top-left, width:top-right/w-key, load:bottom-right, remove crap

As of 06.01.2015. See ბოლო ვერსია.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name     Manga cleaner & loader      
// @version     1.0.2
// @description     img:click/arrow-key, back:top-left, width:top-right/w-key, load:bottom-right, remove crap
// @namespace     https://greasyfork.org/users/3159 
// @grant     GM_getValue
// @grant     GM_setValue
// @include     http://g.e-hentai.org/s/*
// @include     http*://exhentai.org/s/*
// @include     http://hentairules.net/galleries*/picture.php?/*
// ==/UserScript==

var loc = location.hostname.indexOf('hentairules.net') > -1 ? 1 : 2;
if (loc == 1) {
    location.href = document.getElementById('derivativeSwitchBox').children[5].href; //need to reload page for this to take effect
	var next = document.getElementById('linkNext').href;
	var o = document.getElementById('theMainImage').src;
	var t = document.getElementsByClassName('browsePath')[0].children[1].innerText;
}
else if (loc == 2) {
	var a = document.getElementsByTagName('a');
	var next = a[2].href;
	var last = a[3].href;
	var o = a[4].children[0].src;
	var t = document.title;
}
var array = [o];
var w = 0;
var i = 0;
var y = 0;
var gi;
document.head.innerHTML = '<title>' + t + '</title><style>html{height:100%}body{height:100%;margin:0}img{width:100%;max-width:100%;margin-left:auto;margin-right:auto;display:block}div{width:100px;height:100px;position:fixed}div:hover{background:rgba(0,0,0,.5)}</style>';
document.body.innerHTML = '';
img = new Image();
img.src = o;
document.body.appendChild(img);

load = document.createElement("div");
load.style.cssText = "right:0;bottom:0";
load.onclick = function () {
	y = 1;
	load.style.display = "none";
	width.style.position = back.style.position = "absolute";
	if (array[1]) {
		aimg = new Image();
		aimg.src = array[1];
		document.body.appendChild(aimg);
	}
};

back = document.createElement("div");
back.style.cssText = "top:0;left:0";
back.onclick = function () {
	if (i > 0) {
		img.src = array[--i];
	}
};

sstyle = document.createElement("style");
document.head.appendChild(sstyle);

if (GM_getValue("magsave")) {
	sstyle.innerText = "img{max-height:100%;width:inherit}";
}

function cwidth() {
	if (GM_getValue("magsave")) {
		sstyle.innerText = "img{max-height:inherit;width:100%}";
		GM_setValue("magsave", 0);
	} else {
		sstyle.innerText = "img{max-height:100%;width:inherit}";
		GM_setValue("magsave", 1);
	}
}

width = document.createElement("div");
width.style.cssText = "top:0;right:0";
width.onclick = function () {
	cwidth();
};

document.body.appendChild(back);
document.body.appendChild(width);
document.body.appendChild(load);

var gi;
function current() {
	gi = document.getElementsByTagName('img');
	for (var a = 0; a < gi.length; a++) {
		if (window.pageYOffset + window.innerHeight >= gi[a].offsetTop) {
			o = a;
		}
	}
	return o;
}

function cnimage(i) {
	if (!gi[i] && array[i]) {
		bimg = new Image();
		bimg.src = array[i];
		document.body.appendChild(bimg);
	}
}

window.onscroll = function () {
	if (y) {
		var x = current();
		cnimage(x + 1);
		cnimage(x + 2);
		cnimage(x + 3);
		cnimage(x + 4);
	}
};

function parray() {
	r = document.implementation.createHTMLDocument("temp");
	x = new XMLHttpRequest();
	x.onreadystatechange = function () {
		if (x.readyState == 4 && x.status == 200) {
			var a = x.responseText;
			r.documentElement.innerHTML = a;
			if (loc == 1) {
				ti = r.getElementsByTagName('img')[0].outerHTML.split('src="')[1].split('"')[0];
				nn = r.getElementsByClassName('pwg-button-icon-right')[0].outerHTML;
				next = nn.split('href="')[1] ? nn.split('href="')[1].split('"')[0] : "";
				if (next === "") {
					w = 2;
				}
			}
			else if (loc == 2) {
				aa = r.getElementsByTagName('a');
				ti = aa[4].children[0].src;
				next = aa[2].href;
				if (next == last) {
					w++;
				}
			}
			array.push(ti);
			if (w < 2) {
				parray();
			}
		}
	};
	x.open('GET', next, true);
	x.send();
	return;
}
parray();

img.onclick = function () {
	if ((i + 1) < array.length) {
		img.src = array[++i];
	}
};

window.document.onkeydown = function () {};
document.addEventListener("keydown", function (e) {
	switch (e.which) {
	case 39:
		if ((i + 1) < array.length) {
			img.src = array[++i];
		}
		break;
	case 37:
		if (i > 0) {
			img.src = array[--i];
		}
		break;
	case 87:
		cwidth();
		break;
	}
});

timg1 = new Image();
timg2 = new Image();
timg3 = new Image();
timg4 = new Image();
timg5 = new Image();
timg6 = new Image();
timg7 = new Image();
timg8 = new Image();

img.onload = function () {
	window.scrollTo(0, 0);
	array[i + 1] ? timg1.src = array[i + 1] : null;
	array[i + 2] ? timg1.src = array[i + 2] : null;
	array[i + 3] ? timg1.src = array[i + 3] : null;
	array[i + 4] ? timg1.src = array[i + 4] : null;
	array[i - 1] ? timg1.src = array[i - 1] : null;
	array[i - 2] ? timg1.src = array[i - 2] : null;
	array[i - 3] ? timg1.src = array[i - 3] : null;
	array[i - 4] ? timg1.src = array[i - 4] : null;
};