Gelbooru Endless Scroll

Adds endless scroll function to Gelbooru

Tính đến 19-03-2017. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @id             gelbooru-endless-scroll
// @name           Gelbooru Endless Scroll
// @version        1.5
// @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?*
// @include        http://rule34.xxx/index.php?*
// @include        https://rule34.xxx/index.php?*
// @include        http://*.booru.org/index.php?*
// @include        https://*.booru.org/index.php?*
// @include        http://e621.net/post/index/*
// @include        https://e621.net/post/index/*
// @run-at         document-end
// @grant          none
// ==/UserScript==

(function(){
  "use strict";
  var d = document, _on = false, url,
  v = "span.thumb",
  vis = function() {
    var rect = target().getBoundingClientRect();
    console.log(rect);
    return rect.x === 0 && rect.y === 0 ? false : (
      rect.top + rect.height >= 0 &&
      ( document.documentElement.clientHeight - rect.bottom ) + rect.height >= 0
    );
  }, total,
  page = function(doc) {
    var images, pageNo = d.createElement("span"), frag = d.createDocumentFragment(), container;
    images = [...doc.querySelectorAll(v)];
    if (images.length === 0)
      throw Error("API error");
    pageNo.innerHTML = '<span style="height:inherit;display:flex;flex-direction:column;">Page ' + url.index + '~<span style="margin:auto 0 30px">out of ' + total + "</span></span>";
    pageNo.className = "thumb";
    frag.appendChild(pageNo);
    images.map(a => {
      let fn = e => {
        e.target.onload = null;
        a.removeAttribute("style");
      };
      a.style.overflow = "hidden";
      a.querySelector("img").onload = fn;
      frag.appendChild(a);
    });
    container = d.querySelector(v).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()) {
      events();
      url = list.shift();
      return req();
    }
  }, events = function() {
    var name = (_on = !_on) ? "addEventListener" : "removeEventListener";
    window[name]("scroll", process);
    window[name]("resize", process);
    window[name]("visibilitychange", process);
  }, list = [], r = /(pid=|index\/)([0-9]+)/;
  const target = () => (d.querySelector("div.pagination") || d.querySelector("div#paginator"));
  if (!target() || !target().querySelector("a") || !/\/post\/|page=post/.test(location.href)) return;
  else {
    let pid = location.href.match(r),
    p = ((pid && pid[1] == "index/") ? target().lastElementChild.previousElementSibling : target().lastElementChild).href,
    n = d.querySelectorAll(v).length,
    start_index, end_index, increment, index = 0;
    console.log(pid, p);
    if (!p) return;
    switch(location.hostname.match(/[^\.]+\.[^\.]+$/)[0]) {
      case "gelbooru.com": case "rule34.xxx":
        start_index = pid ? pid[2] / n : 0;
        increment = n;
        end_index = p.match(r)[2] / n;
        index = 1;
        break;
      case "booru.org":
        start_index = pid ? pid[2] / n : 0;
        increment = n;
        end_index = p.match(r)[2] / n;
        index = 1;
        break;
      case "e621.net":
        start_index = pid ? +pid[2] : 2;
        increment = 1;
        end_index = +p.match(r)[2];
    }
    console.log(start_index, end_index);
    while(start_index < end_index) {
      list.push({
        href: p.replace(r, "$1" + (++start_index * increment)),
        index: start_index + index
      });
    }
    total = end_index + index;
  }
  d.addEventListener("gelbooru-slide", _ => {
    if (list.length > 0)
      events();
  }, false);
  d.addEventListener("gelbooru-slide-next", _ => {
    if (list.length > 0)
      events();
  }, false);
  events();
  if (vis()) process();
}())