Gelbooru Image Viewer

Adds a fullscreen image view option when you click on images

Устаревшая версия за 16.12.2016. Перейдите к последней версии.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey, Greasemonkey или Violentmonkey.

Для установки этого скрипта вам необходимо установить расширение, такое как Tampermonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Violentmonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Userscripts.

Чтобы установить этот скрипт, сначала вы должны установить расширение браузера, например Tampermonkey.

Чтобы установить этот скрипт, вы должны установить расширение — менеджер скриптов.

(у меня уже есть менеджер скриптов, дайте мне установить скрипт!)

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

(у меня уже есть менеджер стилей, дайте мне установить скрипт!)

// ==UserScript==
// @id             gelbooru-slide
// @name           Gelbooru Image Viewer
// @version        1.6
// @namespace      intermission
// @author         intermission
// @license        WTFPL; http://www.wtfpl.net/about/
// @description    Adds a fullscreen image view option when you click on images
// @include        http://gelbooru.com/index.php?*
// @include        https://gelbooru.com/index.php?*
// @run-at         document-start
// @grant          GM_registerMenuCommand
// ==/UserScript==

(function(){
	"use strict";
	var d = document, w = window, array = a => [].slice.call(a), observer, request, slideEl, slider, slidin, base = a => a.split("/").pop().split(".")[0].split("_").pop(), keyDown, find, current, preload, stor = localStorage, l, ns = "gelbooru-slide", toggle = stor[ns] == "true", cache, notification, pos, posEl, menu, menuEl;
	
	if (!stor[ns]) stor[ns] = "false";
	
	GM_registerMenuCommand("Current image mode: " + (toggle ? "Always original size" : "Sample only"), _ => {
		stor[ns] = toggle ? "false" : "true";
		return location.reload();
	});
	
	l = toggle ? ["ul>li>a[style*='font-weight:']", "href"] : ["#image", "src"];
	
	current = _ => d.querySelector("img.preview[src*='" + base(slideEl.src) + "']").parentNode;
	
	cache = (a, b) => {
		let id = a.href.match(/id=([0-9]+)/)[1], val = toggle ? "original" : "sample";
		if (!b) {
			let ret;
			try { ret = JSON.parse(stor[ns + id])[val] } catch(e) {}
			return ret || "loading";
		} else {
			let obj, temp;
			try { temp = JSON.parse(stor[ns + id]) } catch(e) {}
			obj = temp || {};
			obj[val] = b;
			stor[ns + id] = JSON.stringify(obj);
			return b;
		}
	};
	
	pos = a => {
		if (typeof a === "boolean") {
			let no = posEl.firstElementChild;
			if (a) no.innerHTML = Number(no.innerHTML) + 1;
			else no.innerHTML = Number(no.innerHTML) - 1;
		} else {
			if (slideEl) {
				let thumbs = array(d.querySelectorAll`span.thumb a[data-full]`);
				posEl = d.createElement`div`;
				posEl.insertAdjacentHTML("beforeend", "<span>" + (thumbs.indexOf(current()) + 1) + "</span> / " + thumbs.length);
				posEl.setAttribute("style", "position: fixed; bottom: 20px; left: 0; display: block; pointer-events: none;");
				d.body.appendChild(posEl);
			} else posEl = posEl.remove();
		} return;
	};
	
	find = function(el, method) {
		var a;
		el = el.parentNode;
		do {
			try {
				el = el[(method ? "next" : "previous") + "ElementSibling"];
				a = el.querySelector`a[data-full]`;
			} catch(err) {
				return false;
			}
			if (a) break;
			a = false;
		} while(!a);
		return a;
	};
	
	menu = e => {
		let _l = e.clientX + 1, _t = e.clientY + 1,
			left = (_l > w.innerWidth - 139 ? (_l - 139) : _l) + "px",
			top = (_t > w.innerHeight - 48 ? (_t - 48) : _t) + "px";
		if (menuEl) {
			menuEl.removeAttribute("class");
			menuEl.style.left = left;
			menuEl.style.top = top;
			setTimeout(() => {menuEl.classList.add("menuel")}, 10);
		} else {
			let href = current().href;
			menuEl = d.createElement("div");
			menuEl.id = "menuel";
			menuEl.insertAdjacentHTML("beforeend", '<a href="'+href+'" style="margin-bottom: 2px">Open in This Tab</a><a href="'+href+'" target="_blank">Open in New Tab</a>');
			menuEl.style.left = left;
			menuEl.style.top = top;
			d.body.appendChild(menuEl);
			menuEl.classList.add("menuel");
		} return;
	};
	
	preload = function() {
		var curr = current();
		request(find(curr, true));
		return request(find(curr, false));
	};
	
	keyDown = function(e) {
		var move;
		switch(e.keyCode) {
			case 32: case 39:
				move = true;
				break;
			case 37:
				move = false;
				break;
			case 38:
				if (e.event) menu(e.event);
				else w.location = current().href;
				return;
			case 40:
				e.preventDefault();
				return slideEl.click();
		}
		if (typeof move != "undefined") {
			e = find(current(), move);
			if (e) {
				slideEl.slide(e.firstElementChild.src);
				pos(move);
				preload();
			} else if (!notification) {
				notification = d.createElement`div`;
				notification.classList.add`nomoreimages`;
				notification.setAttribute("style", "background: linear-gradient(to " + (move ? "right" : "left") + ", transparent, rgba(255,0,0,.5));" + (move ? "right" : "left") + ": 0;");
				d.body.appendChild(notification);
			}
		} return;
	};
	
	slider = function(a) {
		if (slidin) {
			let center;
			slidin = !(a = current());
			slideEl = slideEl.remove();
			pos();
			d.body.classList.remove`sliding`;
			a.classList.add`outlined`;
			d.removeEventListener("keydown", keyDown, false);
			center = a.offsetTop + a.offsetHeight / 2 - w.innerHeight / 2;
			w.scrollTo(0, center < 0 ? 0 : center);
		} else {
			slidin = true;
			d.body.classList.add`sliding`;
			array(d.querySelectorAll`span>a.outlined`).map(a => a.classList.remove`outlined`);
			slideEl = d.createElement`img`;
			slideEl.id = "slide";
			slideEl.alt = "Loading...";
			Object.defineProperty(slideEl, "slide", {
				value: function(src) {
					var data;
					this.src = src;
					data = current().dataset.full;
					if (data == "loading") request(current());
					else {
						if (/\.gif(?:\?\d+)?$/.test(data)) this.onload = _ => {
								this.removeAttribute("src");
								this.setAttribute("src", data);
								this.onload = null;
							};
						this.src = data;
					}
				}
			});
			slideEl.onclick = slider;
			slideEl.onmouseup = e => e.button === 1 && keyDown({keyCode:38, event:e});
			d.body.appendChild(slideEl);
			slideEl.slide(a.firstElementChild.src);
			d.addEventListener("keydown", keyDown, false);
			pos(); preload();
		} return d.dispatchEvent(new CustomEvent(ns, {bubbles:true}));
	};
	
	request = function(node) {
		if (!node || node.dataset.alreadyLoading || node.dataset.full != "loading") return;
		node.dataset.alreadyLoading = "true";
		return fetch(node.href)
			.then(x => x.text().then(text => {
				let doc = (new DOMParser()).parseFromString(text, "text/html"), img, _base;
				if (img = doc.querySelector(l[0])) {
					_base = base(img[l[1]]);
					node.dataset.full = cache(node, img[l[1]]);
					if (slideEl && slideEl.src.indexOf(_base) > -1)
						slideEl.slide(img[l[1]]);
					return;
				} else {
					node.removeAttribute`data-already-loading`;
					fetch`/intermission.php`.then(_ => request(node));
					throw undefined;
				}
			})).catch(err => {
				if (typeof err == "undefined") return;
				console.error("Failed HTTP request\nDo you have an internet connection?\n", err);
				return node.removeAttribute`data-already-loading`;
			});
	};
	
	observer = new MutationObserver(function(mutations) {
		function process(node) {
			var a;
			try {
				if (node.matches`span.thumb[id^='s']` && (a = node.firstElementChild) && !a.dataset.full) {
					if (node.querySelector`img[alt*='webm']`) return;
					a.dataset.full = cache(a);
					a.onclick = e => e.button === 0 && (e.preventDefault(), e.stopPropagation(), slider(e.target.parentNode));
				}
			} catch(e) {} return;
		}
		return mutations.forEach(mutation => array(mutation.addedNodes).forEach(process)); 
	});
	observer.observe(d, {
		childList: true,
		subtree: true
	});
	
	d.addEventListener("animationend", e => {
		if (e.animationName == "Outlined") e.target.classList.remove`outlined`;
		else if (e.animationName == "nomoreimages") notification = e.target.remove();
		else if (e.animationName == "menuelement") menuEl = e.target.remove();
	}, false);
	
	w.addEventListener("keypress", e => (e.target.matches`span.thumb>a[data-full]` && (e.key === "Enter" || e.keyCode === 13)) && (e.preventDefault(), slider(e.target)), false);
	
	w.addEventListener("wheel", e => slidin && keyDown({keyCode: e.deltaY > 0 ? 39 : e.deltaY < 0 ? 37 : 0}), false);
	
	if (stor[ns + "-firstrun"] != "1.5.3") {
		(function(l){
			var a;
			for (a in l)
				if (/^gelbooru-slide./.test(a)) l.removeItem(a);
		}(stor));
		stor[ns + "-firstrun"] = "1.5.3";
	}
	
	{
		let css = d.createElement`style`;
		css.textContent =
		`@keyframes Outlined {
			0% { outline: 6px solid orange }
			60% { outline: 6px solid orange }
			100% { outline: 6px solid transparent }
		}
		@keyframes nomoreimages {
			0% { opacity: 0 }
			20% { opacity: 1 }
			100% { opacity: 0 }
		}
		@keyframes menuelement {
			0% { opacity: 1 }
			80% { opacity: 1 }
			100% { opacity: 0 }
		}
		body.sliding > *:not(#slide) {
			display: none
		}
		#slide {
			width: 100vw;
			height: 100vh;
			object-fit: contain
		}
		.outlined {
			outline: 6px solid transparent;
			animation-duration: 4s;
			animation-name: Outlined
		}
		.nomoreimages {
			display: block ! important;
			width: 33vw;
			height: 100vh;
			top: 0;
			position: fixed;
			animation-duration: 1s;
			animation-name: nomoreimages
		}
		span.thumb {
			max-width: 180px;
			max-height: 180px;
		}
		#menuel {
			opacity: 1;
			position: fixed;
			display: block ! important;
			padding: 2px;
			background: black;
			width: 139px;
			height: 44px;
			animation-duration: 1s
		}
		.menuel {
			animation-name: menuelement
		}
		#menuel:hover {
			animation-name: keepalive
		}
		#menuel a {
			background: #fff;
			display: block
		}`;
		return d.head.appendChild(css);
	}
}())