HankLloydright / xenForo - Endless Forum Pages jQuery 2016

// ==UserScript==
// @name         xenForo - Endless Forum Pages jQuery 2016
// @namespace    https://openuserjs.org/users/HankLloydright
// @description  Makes xenForo forum pages infinitely long by adding a 'Load More Posts' button at the top and bottom of every page. Loads 20 more posts 'in-line' without page refresh -- based off of script by https://openuserjs.org/users/ardiman 
// @grant        none
// @author HankLloydright
// @icon         https://raw.githubusercontent.com/ardiman/userscripts/master/scriptlogo.gif
// @include http://www.tivocommunity.com/community/index.php?threads/*
// @include https://www.tivocommunity.com/community/index.php?threads/*
// @include https://teslamotorsclub.com/tmc/threads/*
// @license      CC BY-NC-SA 3.0; https://creativecommons.org/licenses/by-nc-sa/3.0/
// @version      1.3.0
// @date         2016-11-14
// ==/UserScript==

var nextURL = null; // the URL for next, or null for pending/none
var newpagenav=null;

setNextURL(document);
window.addEventListener("load", init, false);

function init() {
  setNextURL(document);
  addMoreButton();
  // pullMore();
}

function addMoreButton() {
    if (nextURL!==null) {
        $(".pageNavHeader").html("<button type='button' style='font-size:16px;' class='loadmoreclass'>Load more posts...</button>");
        $(".loadmoreclass").click( function() {
            pullMore();
        });
    }
}

function pullMore() {

 $(".pageNavHeader").html("<span style='font-size:12pt;font-weight:bold;'>Loading more posts Please Wait...</span>");

  var iframe = document.createElement("iframe");
  iframe.addEventListener("load", whee, false);
  iframe.width = 1;
  iframe.height = 1;
  iframe.id = "childframe";
  iframe.style.visibility = "hidden";
  iframe.src = nextURL;
  document.body.appendChild(iframe);

  // Don't pull this more than once.
  nextURL = null;

  function whee() {
    var iframeDoc = iframe.contentDocument;

    // Update nextURL for when we reach the bottom of the page again.
    setNextURL(iframeDoc);

    // Move posts into the main page's table.
    siphon(iframeDoc);

    // Get rid of the iframe to free memory once it's GCed and so on.
    // Use a setTimeout to work around bug 305471 and to spread out the CPU usage to keep Firefox responsive.
    setTimeout( function() { iframe.parentNode.removeChild(iframe); }, 1500);
  }
}

function findNextLink(doc)
{
 var lnks=doc.links;
 newpagenav=doc.getElementsByClassName("pageNavLinkGroup");
 // console.log(newpagenav[1].innerHTML);
 for (var i = 0, len = lnks.length; i < len; i++) {
     var lnk=lnks[i].href;
     var loc=lnk.indexOf("page-");
     var htm=lnks[i].innerHTML.indexOf("Next &gt;");
     if (loc>0 && htm>=0) {
         // console.log("found next link @"+ i +": Loc:"+loc+"/htm:"+htm+" "+lnk);
         return lnk;
     }
     else {
         docpagenav=document.getElementsByClassName("pageNavLinkGroup")[1];
         $(docpagenav).html(newpagenav[0].innerHTML);
     }
  }
  return null;
}

function setNextURL(doc)
{
   nextURL = findNextLink(doc);
}


function siphon(childWindow)
{
  var currentDiscussionList = $("#messageList");
  $(childWindow).ready(function() {
      var childFrame = $("#childframe").contents().find("#messageList");
      var nodesToAppend = childFrame.children();
      nodesToAppend.appendTo(currentDiscussionList);
   docpagenav=document.getElementsByClassName("pageNavLinkGroup")[1];
   $(docpagenav).html(newpagenav[1].innerHTML);
   addMoreButton();
  });
}