Mangalib Infinite Scroll

Infinite scroll on chat mangalib.me

As of 2019-06-11. See the latest version.

// ==UserScript==
// @name         Mangalib Infinite Scroll
// @version      0.6.1
// @description  Infinite scroll on chat mangalib.me
// @author       reiwsan
// @match        https://mangalib.me/*
// @match        https://ranobelib.me/*
// @namespace    https://greatest.deepsurf.us/users/221048
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const preloadPos = 753;

    const historyAutoload = function(chatItems) {
        const chatMore = chatItems.querySelector('.chat__more');
        let historyLoad = true;

        const chatMoreClick = function(chatMore) {
            chatMore.click();
            return false;
        }

        chatItems.addEventListener('scroll', _ => {
            let scrollPos = (chatItems.scrollHeight - chatItems.scrollTop);
            let scrollPreload = (scrollPos <= preloadPos);

            historyLoad = (scrollPreload && historyLoad) ? chatMoreClick(chatMore) :
                (scrollPreload) ? false : true;
        });
    }

    const getChatInstance = function() {
        const chatWrap = _CHAT_INSTANCE.$children[0];
        return chatWrap.$children[1] ? chatWrap.$children[1] : chatWrap.$children[0];
    }

    if (typeof _CHAT_INSTANCE !== 'undefined') {
        const CHAT_INSTANCE = getChatInstance();

        const chatInitInterval = setInterval(() => {
            if (CHAT_INSTANCE._isMounted) {
                clearInterval(chatInitInterval);

                const chatItems = document.querySelector('.chat__items');
                historyAutoload(chatItems);
            }
        }, 50);
    }
})();