HWM_InfiniteScrollForProtocolPages

Бесконечная прокрутка страниц протоколов (добавление содержимого следующей страницы при прокрутке вниз)

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        HWM_InfiniteScrollForProtocolPages
// @namespace   Рианти
// @description Бесконечная прокрутка страниц протоколов (добавление содержимого следующей страницы при прокрутке вниз)
// @include     /(heroeswm|178\.248\.235\.15|lordswm)[^\/]+\/(pl_warlog|pl_transfers|pl_cardlog|sklad_log|clan_log)/
// @version     1
// @grant       GM_xmlhttpRequest
// ==/UserScript==

var _timeoutOnAdding = 0; //ms
var _pixelsFromBottomBeforeAdding = 400;

var _isWarLog = document.location.href.includes('pl_warlog.php');
var _isCardLog = document.location.href.includes('pl_cardlog.php');
var _pageURLpattern = document.location.href.match(/[^\&]+/)[0] + '&page=';
var _addingInProgress = false;
var _curPage = 0;

if(document.location.href.includes('&page=')) _curPage = parseInt(document.location.href.match(/&page=(\d+)/)[1]);

window.addEventListener("scroll", checkIfNearBottom);

checkIfNearBottom();

function checkIfNearBottom(){
  if(_addingInProgress) return;
  var curWindowTop = window.pageYOffset;
  var contentTotalHeight = document.documentElement.clientHeight;
  var availToWebPageScreenHeight = window.innerHeight

  if(contentTotalHeight - _pixelsFromBottomBeforeAdding < curWindowTop + availToWebPageScreenHeight){
    _addingInProgress = true;
    getContentFromPageId(++_curPage, addNewContent);
  }
}

function getContentFromPageId(id, callbackFunc){
  requestPage (_pageURLpattern + id, function (dom){
    var t = dom.querySelector('body').innerHTML;
    t = t.substring(t.indexOf('<!-- big table -->') + 19, t.lastIndexOf('<!-- big table -->'));
    t = t.substring(t.indexOf('</center><br>') + 13, t.lastIndexOf('</td></tr></tbody></table>'));
    t = t.replace('</a></center><br>', '</a></center>');

    if(_isWarLog) t = alterContentForWarlogPage(t);
    if(_isCardLog) t = alterContentForCardlogPage(t);
    callbackFunc(t);
  });
}

function alterContentForCardlogPage(content){
  return '<table width="100%"><tr><td> </td></tr></table>' + content;
}

function alterContentForWarlogPage(content){
  return content.replace(/<a href="warlog\.php\?warid=(\d+)">([^<]+)<\/a>/g,
                        '<a href="warlog.php?warid=$1">$2</a><span>&nbsp;[<a href="http://' + document.location.host + '/war.php?lt=-1&amp;warid=$1" target="_blank">#</a>&nbsp;<a href="http://' + document.location.host + '/battlechat.php?warid=$1" target="_blank">chat</a>&nbsp;<a href="http://' + document.location.host + '/war.php?warid=$1" target="_blank">$</a>&nbsp;<a href="http://' + document.location.host + '/battle.php?lastturn=-3&amp;warid=$1" target="_blank">E</a>]</span>');
}

function addNewContent(contentHTML){
  var parent = document.querySelector('table[border="0"][cellpadding="0"][cellspacing="0"][width="95%"] > tbody > tr > td');
  if(!_isCardLog){
    var newChild = document.createElement('span');
    newChild.innerHTML = contentHTML;
    parent.appendChild(newChild);
  } else {
    parent.innerHTML += contentHTML;
  }
  setTimeout(function(){
    _addingInProgress = false;
    checkIfNearBottom();
  }, _timeoutOnAdding);
}

function requestPage (url, onloadHandler){
    console.log('[HWM_InfiniteScrollForProtocolPages] loading: ', url);
    try{
        GM_xmlhttpRequest({
            overrideMimeType: 'text/plain; charset=windows-1251',
            synchronous: false,
            url: url,
            method: "GET",
            onload: function(response){
                onloadHandler(new DOMParser().parseFromString(response.responseText, 'text/html').documentElement);
            },
            onerror: function(){ setTimeout( function() { requestPage (url, onloadHandler) }, 500 ) },
            ontimeout: function(){ requestPage (url, onloadHandler) },
            timeout: 5000
        });
    } catch (e) {
        console.log(e);
    }
}