Gelbooru Endless Scroll

Adds endless scroll function to Gelbooru

2016-12-18 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @id             gelbooru-endless-scroll
// @name           Gelbooru Endless Scroll
// @version        1.3
// @namespace      intermission
// @author         intermission
// @license        WTFPL; http://www.wtfpl.net/about/
// @description    Adds endless scroll function to Gelbooru
// @include        http://gelbooru.com/index.php?*
// @include        https://gelbooru.com/index.php?*
// @run-at         document-end
// @grant          none
// ==/UserScript==

(function(){
	"use strict";
	var d = document, _on = false, test_var, url,
	v = ["span.thumb[id^='s']", "div.pagination"],
	vis = function(el) {
		var rect = el.getBoundingClientRect();
		return (
			rect.top + rect.height >= 0 &&
			( d.documentElement.clientHeight - rect.bottom ) + rect.height >= 0
		);
	}, target = d.querySelector(v[1]), total,
	page = function(doc) {
		var images, pageNo = d.createElement`span`, frag = d.createDocumentFragment(), container;
		images = [].slice.call(doc.querySelectorAll(v[0]));
		if (images.length == 0) {
			fetch`/intermission.php`.then(req);
			throw undefined;
		}
		pageNo.innerHTML = "Page " + url.index + "~<span>out of " + total + "</span>";
		pageNo.className = "thumb";
		pageNo.style = "display:flex;flex-direction:column;"
		frag.appendChild(pageNo);
		pageNo.firstElementChild.style = "margin:auto 0 30px";
		images.map(a => {
			let fn = e => {
				e.target.onload = null;
				a.style.overflow = "visible";
			};
			a.style.overflow = "hidden";
			a.querySelector("img").onload = fn;
			frag.appendChild(a);
		});
		container = d.querySelector(v[0]).parentNode;
		if (container.lastElementChild.matches`span`) container.appendChild(frag);
		else container.insertBefore(frag, container.querySelector`span.thumb:last-of-type + *`);
		if (list.length > 0) {
			events();
			process();
		} return;
	}, req = () => fetch(url.href).then(
		x => x.text().then(
			text => page((new DOMParser()).parseFromString(text, "text/html"))
		)
	).catch(err => {
		if (typeof err != "undefined") {
			console.error("Something unexpected happened\n", err);
			setTimeout(() => req(), 5000);
		}
	}), process = function() {
		if (vis(target)) {
			events();
			url = list.shift();
			return req();
		}
	}, events = function() {
		var name = (_on = !_on) ? "addEventListener" : "removeEventListener";
		window[name]("scroll", process, false);
		window[name]("resize", process, false);
		window[name]("visibilitychange", process, false);
	}, list = [];
	if (!target || !target.querySelector`a`) return;
	{
		let pagination = target.lastElementChild,
		n = d.querySelectorAll(v[0]).length,
		pid = window.location.href.match(/pid=([0-9]+)/),
		start_index = pid ? pid[1] / n + 1 : 1,
		end_index;
		if (!pagination.href) return;
		else {
			end_index = pagination.href.match(/pid=([0-9]+)/)[1] / n + 1;
			do {
				list.push({
					href: pagination.href.replace(/pid=[0-9]+/, "pid=" + start_index * n),
					index: ++start_index
				});
			} while(start_index != end_index);
			total = ++list.length;
		}
	}
	d.addEventListener("gelbooru-slide", () => {
		if (list.length > 0)
			events();
	}, false);
	events();
	process();
}())