Load List In Next Reddit Page In-Place

Load the list in the next Reddit page and append it into the current list without leaving the current page. Like YouTube does.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Load List In Next Reddit Page In-Place
// @namespace   LoadListInNextRedditPageInPlace
// @description Load the list in the next Reddit page and append it into the current list without leaving the current page. Like YouTube does.
// @version     1.0.1
// @author      jcunews
// @include     https://*.reddit.com/*
// @grant       none
// ==/UserScript==

(function() {
  var eleList = document.getElementById("siteTable"), eleNext, ele, ele2, xhr = new XMLHttpRequest();

  function loadError() {
    if ((xhr.readyState === 4) && (xhr.status === 0)) {
      alert("Failed to load more items.\nCheck network connection or adblocker.");
    } else alert("Failed to load more items.\nHTTP code " + xhr.status + ". " + xhr.statusText);
    eleNext.style.cssText = "";
  }

  function loadMore() {
    if (this.style.cssText) return false;
    if (!navigator.onLine) {
      alert("Can not load more items.\nWeb browser is offline.");
      return false;
    }
    this.style.cssText = "color:#aaa;cursor:wait";
    xhr.open("GET", this.href, true);
    xhr.responseType = "document";
    xhr.send();
    return false;
  }

  function processPage() {
    var list = xhr.response.getElementById("siteTable").children, i;
    eleList.removeChild(eleList.lastElementChild);
    for (i = 0; i < list.length; i++) {
      eleList.appendChild(list[i]);
    }
    setupLoadMoreButton();
  }

  function setupLoadMoreButton() {
    var ele, ele2;
    eleNext = document.querySelector("#siteTable>.nav-buttons .next-button>a");
    if (eleNext) {
      eleNext.textContent = "Load more...";
      eleNext.onclick = loadMore;
      eleNext.parentNode.className = "loadmore-button";
      eleNext.parentNode.style.cssText = "display:block;text-align:center";
      ele = eleNext.parentNode.previousSibling;
      while (ele) {
        ele2 = ele.previousSibling;
        ele.parentNode.removeChild(ele);
        ele = ele2;
      }
    } else {
      ele = document.querySelector("#siteTable>.nav-buttons");
      if (ele) ele.parentNode.removeChild(ele);
    }
  }

  if (!eleList) return;
  xhr.onerror = loadError;
  xhr.onload = processPage;
  setupLoadMoreButton();
})();