Adds endless scroll function to Gelbooru
当前为
// ==UserScript==
// @id gelbooru-endless-scroll
// @name Gelbooru Endless Scroll
// @version 1.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(){
"use strict";
var d = document, _on = false, test_var, url,
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`, total,
page = function(doc) {
var images, pageNo = d.createElement`span`, frag = d.createDocumentFragment(), container;
images = [].slice.call(doc.querySelectorAll`span.thumb[id^='s']`);
if (images.length == 0)
return fetch`/intermission.php`.then(x => req())
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 => frag.appendChild(a));
container = d.querySelector`span.thumb[id^='s']`.parentNode;
if (container.lastElementChild.matches`span`) container.appendChild(frag);
else throw console.error`Something modified the DOM unexpectedly!`;
if (list.length > 0) events();
return setTimeout(process);
}, req = () => fetch(url.href).then(
x => x.text().then(text => page((new DOMParser()).parseFromString(text, "text/html")))
).catch(err => {
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,
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);
total = ++list.length;
}
}
events();
process();
}())