pagination-manager2

infinite scroll

As of 06.09.2025. See апошняя версія.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/548609/1656006/pagination-manager2.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         pagination-manager2
// @description  infinite scroll
// @namespace    Violentmonkey Scripts
// @author       smartacephale
// @license      MIT
// @version      1.43
// @match        *://*/*
// @grant        unsafeWindow
// @downloadURL https://update.greasyfork.org/scripts/494205/pagination-manager.user.js
// @updateURL https://update.greasyfork.org/scripts/494205/pagination-manager.meta.js
// ==/UserScript==

class PaginationManager {
    /**
     * @param {Vue.reactive} state
     * @param {Vue.reactive} stateLocale
     * @param {Rules} rules - WEBSITE_RULES class with methods:
     * - URL_DATA { iteratable_url, offset },
     * - PAGINATION_LAST
     * @param {Function} handleHtmlCallback
     * @param {number} delay - milliseconds
     * @param {Function} [alternativeGenerator] - Optional custom generator function
     */
    constructor(state, stateLocale, rules, handleHtmlCallback, delay, alternativeGenerator) {
        const { offset, iteratable_url } = rules.URL_DATA();
        Object.assign(this, { state, stateLocale, rules, handleHtmlCallback, delay });
        Object.assign(this.stateLocale, { pagIndexLast: rules.PAGINATION_LAST, pagIndexCur: offset });

        this.paginationGenerator = alternativeGenerator?.() ??
            PaginationManager.createPaginationGenerator(offset, rules.PAGINATION_LAST, iteratable_url);

        this.createPaginationObserver();
    }

    createPaginationObserver() {
        const observable = this.rules.INTERSECTION_OBSERVABLE || this.rules.PAGINATION;
        unsafeWindow.bhutils.Observer.observeWhile(observable, this.generatorConsumer, this.delay);
    }

    generatorConsumer = async () => {
        if (!this.state.infiniteScrollEnabled) return;
        const { value: { url, offset } = {}, done } = await this.paginationGenerator.next();
        if (!done) {
            const nextPageHTML = await unsafeWindow.bhutils.fetchHtml(url);
            const prevScrollPos = document.documentElement.scrollTop;
            this.handleHtmlCallback(nextPageHTML);
            this.stateLocale.pagIndexCur = offset;
            window.scrollTo(0, prevScrollPos);
        }
        return !done;
    }

    static * createPaginationGenerator(currentPage, totalPages, generateURL) {
        for (let p = currentPage + 1; p <= totalPages; p++) {
            const url = generateURL(p);
            yield { url, offset: p };
        }
    }
}