HWM_InfiniteScrollForProtocolPages

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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);
    }
}