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.

  1. // ==UserScript==
  2. // @name Load List In Next Reddit Page In-Place
  3. // @namespace LoadListInNextRedditPageInPlace
  4. // @description Load the list in the next Reddit page and append it into the current list without leaving the current page. Like YouTube does.
  5. // @version 1.0.1
  6. // @author jcunews
  7. // @include https://*.reddit.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. var eleList = document.getElementById("siteTable"), eleNext, ele, ele2, xhr = new XMLHttpRequest();
  13.  
  14. function loadError() {
  15. if ((xhr.readyState === 4) && (xhr.status === 0)) {
  16. alert("Failed to load more items.\nCheck network connection or adblocker.");
  17. } else alert("Failed to load more items.\nHTTP code " + xhr.status + ". " + xhr.statusText);
  18. eleNext.style.cssText = "";
  19. }
  20.  
  21. function loadMore() {
  22. if (this.style.cssText) return false;
  23. if (!navigator.onLine) {
  24. alert("Can not load more items.\nWeb browser is offline.");
  25. return false;
  26. }
  27. this.style.cssText = "color:#aaa;cursor:wait";
  28. xhr.open("GET", this.href, true);
  29. xhr.responseType = "document";
  30. xhr.send();
  31. return false;
  32. }
  33.  
  34. function processPage() {
  35. var list = xhr.response.getElementById("siteTable").children, i;
  36. eleList.removeChild(eleList.lastElementChild);
  37. for (i = 0; i < list.length; i++) {
  38. eleList.appendChild(list[i]);
  39. }
  40. setupLoadMoreButton();
  41. }
  42.  
  43. function setupLoadMoreButton() {
  44. var ele, ele2;
  45. eleNext = document.querySelector("#siteTable>.nav-buttons .next-button>a");
  46. if (eleNext) {
  47. eleNext.textContent = "Load more...";
  48. eleNext.onclick = loadMore;
  49. eleNext.parentNode.className = "loadmore-button";
  50. eleNext.parentNode.style.cssText = "display:block;text-align:center";
  51. ele = eleNext.parentNode.previousSibling;
  52. while (ele) {
  53. ele2 = ele.previousSibling;
  54. ele.parentNode.removeChild(ele);
  55. ele = ele2;
  56. }
  57. } else {
  58. ele = document.querySelector("#siteTable>.nav-buttons");
  59. if (ele) ele.parentNode.removeChild(ele);
  60. }
  61. }
  62.  
  63. if (!eleList) return;
  64. xhr.onerror = loadError;
  65. xhr.onload = processPage;
  66. setupLoadMoreButton();
  67. })();