gelbooru-endless-scroll

Adds endless scroll function to Gelbooru

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

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

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.0.1
// @namespace      intermission
// @author         intermission
// @license        CC0; https://wiki.creativecommons.org/wiki/CC0
// @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(){
	var d = document, _on = false, test_var,
	vis = function(el) {
		var rect = el.getBoundingClientRect();
		return (
			rect.top + rect.height >= 0 &&
			( d.documentElement.clientHeight - rect.bottom ) + rect.height >= 0
		);
	}, target = d.querySelector("div.pagination"),
	page = function(doc, url) {
		var images, pageNo = d.createElement("span"), frag = d.createDocumentFragment();
		images = [].slice.call(doc.querySelectorAll("span.thumb[id^='s']"));
		if (images.length == 0) {
			let x = new XMLHttpRequest();
			x.open("get", "/intermission.php", false);
			x.onreadystatechange = function() {
				if (x.readyState == 4)
					if (x.status == 200) {
						return req(url);
					}
			};
			return x.send();
		}
		pageNo.textContent = "Page " + url.index + "~";
		pageNo.className = "thumb";
		frag.appendChild(pageNo);
		images.map(a => frag.appendChild(a));
		d.querySelector("span.thumb[id^='s']").parentNode.appendChild(frag);
		if (list.length > 0) events();
	}, req = function(url) {
		var x = new XMLHttpRequest();
		x.open("get", url.href, false);
		x.onreadystatechange = function() {
			if (x.readyState == 4)
				if (x.status == 200 || x.status == 304) {
					let div = d.createElement("div");
					div.innerHTML = x.responseText;
					page(div, url);
				} else {
					setTimeout(a => req(url), 5000);
				}
		};
		x.send();
	}, process = function() {
		if (vis(target)) {
			events();
			req(list.shift());
		}
	}, 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,
		pid = window.location.href.match(/pid=([0-9]+)/),
		start_index = pid ? pid[1] / 42 + 1 : 1,
		end_index;
		if (!pagination.href) return;
		else {
			end_index = pagination.href.match(/pid=([0-9]+)/)[1] / 42 + 1;
			do {
				list.push({
					href: pagination.href.replace(/pid=[0-9]+/, "pid=" + start_index * 42),
					index: ++start_index
				});
			} while(start_index != end_index);
		}
	}
	events();
	process();
}())