ExHentai Viewer

A script that helps you visit exhentai more conveniently.

As of 2019-01-05. See the latest version.

// ==UserScript==
// @name ExHentai Viewer
// @namespace Violentmonkey Scripts
// @description A script that helps you visit exhentai more conveniently.
// @match *://exhentai.org/*
// @grant       GM_setValue
// @grant       GM_getValue
// @version 0.0.1.20190105083610
// ==/UserScript==


var custom_filter = GM_getValue('custom_filter', []);

if (window.location.href.includes("/s/")) {
	EhViewer();
} else if (window.location.href.includes("/g/")) {
	addToTag();
} else if (document.getElementById('searchbox') !== null) {
	AddFilter();
}

function addToTag(){
	var tb = document.getElementById('taglist').firstElementChild.firstElementChild;
	tb.innerHTML += '<tr><td class="tc">addTag:</td><td><div id="addTag" class="gt" style="cursor:pointer">+</div></td></tr>';
	var bt = document.getElementById('addTag');
	bt.addEventListener('click', newTag, false);
	function newTag(){
		var tagStr = window.prompt("Add filter like format below:", "name@value");
		if(tagStr!==null){
			tagStr = tagStr.split("@");
			custom_filter.push({'name':tagStr[0], 'tag':tagStr[1]});
			GM_setValue('custom_filter', custom_filter);
		}
	}
}

function AddFilter(){
	var form = document.getElementsByTagName('form')[0];
	var search_box = document.getElementsByClassName("nopm")[0].firstElementChild;
	var p = document.getElementById('custom_filter');
	if(p === null) {
		form.insertBefore(document.createElement('br'), form.children[2]);
		p = form.insertBefore(document.createElement('p'), form.children[3]);
		form.insertBefore(document.createElement('br'), form.children[4]);
		p.setAttribute('class', 'nopm');
		p.setAttribute('id', 'custom_filter');
	} else {
		p.innerHTML = '';
	}
	for (var i in custom_filter) {
		filter = custom_filter[i];
		var t = p.appendChild(document.createElement('input'));
		t.setAttribute('type', 'button');
		t.setAttribute('value', filter.name);
		t.setAttribute('tag', filter.tag);
		t.addEventListener('click', addTag, false);
		if (search_box.value.includes(filter.tag)){
			t.style.color='grey';
		} else {
			t.style.color='white';
		}
	}
	var t=p.appendChild(document.createElement('input'));
	t.setAttribute('type', 'button');
	t.setAttribute('value', "+");
	t.addEventListener('click', newTag, false);

	function addTag(e){
		var t = e.srcElement;
		var tag = t.getAttribute('tag');
		if (search_box.value.includes(tag)){
			search_box.value = search_box.value.replace(('"'+tag+'"'), "");
			t.style.color='white';
		} else {
			search_box.value += ('"'+tag+'"');
			t.style.color='grey';
		}
	}

	function newTag(){
		var tagStr = window.prompt("Add filter like format below", "name@value");
		if(tagStr!==null){
			tagStr = tagStr.split("@");
			custom_filter.push({'name':tagStr[0], 'tag':tagStr[1]});
			GM_setValue('custom_filter', custom_filter);
			AddFilter();
		}
	}
}


function EhViewer(){
	var zoomInterval;
	var oldSi = si;
	var float_icon = new Array([]);
	var float_link = new Array([]);
	var currentScale = 1;
	var float_list = document.createElement("ul");
	document.body.appendChild(float_list);
	for(var i=0; i<5; i++){
		float_icon[i]=document.createElement("li");
		float_link[i]=document.createElement("a");
		float_list.appendChild(float_icon[i]);
		float_icon[i].appendChild(float_link[i]);
	}
	float_list.setAttribute("class", "float_list");
	float_icon[0].setAttribute("class", "float_icon prev_page");
	float_icon[1].setAttribute("class", "float_icon next_page");
	float_icon[2].setAttribute("class", "float_icon zoom_in");
	float_icon[3].setAttribute("class", "float_icon zoom_out");
	float_icon[4].setAttribute("class", "float_icon gallery");

	float_icon[0].innerText="➕";
	float_icon[1].innerText="➖";
	float_icon[2].innerText="◀";
	float_icon[3].innerText="▶";
	float_icon[4].innerText="📇";

	document.addEventListener("keydown", keyDown);
	float_icon[0].addEventListener('mousedown', zoomIn, false);
	float_icon[1].addEventListener('mousedown', zoomOut, false);
	float_icon[2].addEventListener('click', prevPage, false);
	float_icon[3].addEventListener('click', nextPage, false);
	document.addEventListener('mouseup', function(){clearInterval(zoomInterval);});
	float_icon[4].setAttribute("onclick", "window.open(document.getElementsByClassName('sb')[0].firstChild.href,'_self');");

	function keyDown(e) {
	   var keycode = e.which;  
		switch(keycode){
			case 37: {
				setNewPage();
				break;
			}
			case 39: {
				setNewPage();
				break;
			}
			case 187: {
				setScale("zoomIn");
				break;
			}
			case 189: {
				setScale("zoomOut");
				break;
			}
			case 188: {
				window.scrollBy(0, -window.innerHeight);
				break;			
			}
			case 190: {
				window.scrollBy(0, window.innerHeight);
				break;
			}
			case 219: {
				window.scrollBy(0, -window.innerHeight*0.3);
				break;
			}
			case 221: {
				window.scrollBy(0, window.innerHeight*0.3);
				break;
			}
			default: {
				// console.log(keycode);
			}
		}
	}

	function setNewPage() {
				var listenChange = setInterval(function(){
					if(oldSi != si){
						var pic = document.getElementById("img");
						var width = Number(pic.style.width.replace("px", ""));
						var height = Number(pic.style.height.replace("px", ""));
						var page = document.getElementsByTagName('span');
						var footMark = document.getElementById('i4').firstChild;
						var currentPage = page[0].innerText;
						var totalPage = page[1].innerText;
						footMark.innerText = currentPage +"P / "+ totalPage +"P :: "+ footMark.innerText;
						width *= currentScale;
						height *= currentScale;
						pic.style.width = width + "px";
						pic.style.height = height + "px";
						oldSi = si;
						clearInterval(listenChange);
					}
				}, 200);
			}

	function setScale(scale){
		var pic = document.getElementById("img");
		var width = Number(pic.style.width.replace("px", ""));
		var height = Number(pic.style.height.replace("px", ""));
		// console.log(pic.src, width, height, max_width);
		switch(scale){
			case 'zoomIn': {
				var max_width = Number(pic.style.maxWidth.replace("px", ""));
				if (width < max_width){
					width *= 1.1;
					height *= 1.1;
					currentScale *= 1.1;
				} else{
				}
				break;
			}
			case 'zoomOut': {
				if (height >= window.innerHeight){
					width /= 1.1;
					height /= 1.1;
					currentScale /= 1.1;
				} else {
				}
				break;
			}
		}
		pic.style.width = width + "px";
		pic.style.height = height + "px";
	}

	function prevPage(){
		document.getElementById('prev').onclick();
		setNewPage();
	}
	function nextPage(){
		document.getElementById('next').onclick();
		setNewPage();
	}
	function zoomIn(){
		setScale('zoomIn');
		zoomInterval = setInterval(function(){
			setScale('zoomIn');
		}, 200);
	}
	function zoomOut(){
		setScale('zoomOut');
		zoomInterval = setInterval(function(){
			setScale('zoomOut');
		}, 200);
	}

	function addNewStyle(newStyle) {
	    var styleElement = document.getElementById('styles_js');

	    if (!styleElement) {
	        styleElement = document.createElement('style');
	        styleElement.type = 'text/css';
	        styleElement.id = 'styles_js';
	        document.getElementsByTagName('head')[0].appendChild(styleElement);
	        styleElement.appendChild(document.createTextNode(newStyle));
	    } else {
	    	styleElement.innerText = newStyle;
	    }
	}

	newStyle = 'h1, #i2, #i5, #i6, #i7, .ip, .sn{display:none!important;}.float_list{display:block; position:fixed; bottom:10px; right:10px; list-style:none;z-index:1005;}.float_icon{user-select:none;background-color:#404040; margin:5px; width:50px; height:50px; line-height:50px; font-size:12px; border-radius:50%; cursor:pointer;}.float_icon:hover{transition-duration:0.2s;font-size:13px; background-color:#44454B; box-shadow:0 0 2px 1px #454545;}.float_icon:active{transition-duration:0.2s;font-size:12px; }::-webkit-scrollbar{display:none;}';

	addNewStyle(newStyle);
}