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.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();