Greasy Fork 还支持 简体中文。

Tavriav Lazy Load Scroll Catalog

Lazy load two subsequent pages on Tavriav's catalog pages with 1-second delay, display current page number, and show distance from the bottom

2023-10-20 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

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 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         Tavriav Lazy Load Scroll Catalog
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  Lazy load two subsequent pages on Tavriav's catalog pages with 1-second delay, display current page number, and show distance from the bottom
// @author       You
// @match        https://www.tavriav.ua/catalog/*/?page=*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let currentPage = parseInt(window.location.search.match(/page=(\d+)/)[1], 10) || 1;
    const distanceFromBottom = 4000;
    let loading = false;
    let pagesToLoad = 2;

    // Create and style the display divs
    GM_addStyle(`
        #displayDiv {
            position: fixed;
            top: 10px;
            left: 10px;
            background: rgba(0,0,0,0.7);
            color: #fff;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 14px;
            z-index: 1000;
        }
    `);

    const pageNumberDisplay = document.createElement('div');
    pageNumberDisplay.id = "displayDiv";
    document.body.appendChild(pageNumberDisplay);

    window.addEventListener('scroll', () => {
        const distance = document.body.offsetHeight - (window.innerHeight + window.scrollY);
        pageNumberDisplay.innerHTML = `Page: ${currentPage}<br>Distance from bottom: ${Math.round(distance)}px`;

        if (!loading && distance <= distanceFromBottom) {
            currentPage++;
            loadMultiplePages(currentPage, pagesToLoad);
        }
    });

    function appendContent(text) {
        const parser = new DOMParser();
        const doc = parser.parseFromString(text, 'text/html');
        const newContent = doc.querySelector('.catalog-products__container');

        if (newContent) {
            document.querySelector('.catalog-products__container').appendChild(newContent);
        }
    }

    function fetchPage(pageNum) {
        return fetch(`https://www.tavriav.ua${window.location.pathname}?page=${pageNum}`)
            .then(response => response.text());
    }

    function loadMultiplePages(startPage, count) {
        if (count === 0) {
            loading = false;
            return;
        }

        loading = true;
        fetchPage(startPage).then(text => {
            appendContent(text);
            setTimeout(() => {
                loadMultiplePages(startPage + 1, count - 1);
            }, 1000); // 1-second delay before fetching the next page
        });
    }

    loadMultiplePages(currentPage + 1, pagesToLoad); // Start fetching the next two pages immediately
})();