Greasy Fork is available in English.

自动续页

免去手动翻页的麻烦,适配更多网站

// ==UserScript==
// @name         自动续页
// @namespace    http://danielnav.wuaze.com/nav
// @version      1.0.0
// @description  免去手动翻页的麻烦,适配更多网站
// @author       DANIEL|Sagehub
// @icon         https://www.appinn.com/wp-content/uploads/Appinn-icon-32.jpg
// @match        *://*/*
// @contributionURL   https://scriptcat.org/api/v2/resource/image/rtYcOQbW7tLKaAJ6

// ==/UserScript==

(function() {
    'use strict';
    let isObserving = false;
    let hasClicked = false;

    // 监视页面上的变化,并在适当的时候加载下一页
    function startObserving() {
        if (!isObserving) {
            const observer = new MutationObserver(mutations => {
                mutations.forEach(mutation => {
                    if (mutation.addedNodes.length > 0) {
                        // 检查新加载的内容是否包含分页元素
                        if (document.querySelectorAll('.pagination, .pager, .page-numbers').length > 0) {
                            // 如果存在分页元素,检查是否需要加载下一页
                            if (shouldLoadNextPage()) {
                                loadNextPage();
                            }
                        }
                    }
                });
            });

            // 根据实际需要调整配置
            const config = {
                childList: true,
                subtree: true,
                attributes: false,
                characterData: false
            };

            observer.observe(document.body, config);
            isObserving = true;
        }
    }

    // 检查是否需要加载下一页
    function shouldLoadNextPage() {
        // 判断是否存在 "下一页" 或者 "尾页" 链接
        const nextLink = document.querySelector('.pagination .next, .pager .next, .page-numbers.dots + .page-numbers');
        if (nextLink && !nextLink.classList.contains('disabled') && !nextLink.classList.contains('hide')) {
            // 避免连续点击
            if (!hasClicked) {
                hasClicked = true;
                return true;
            }
        } else {
            // 重置状态
            hasClicked = false;
        }
        return false;
    }

    // 加载下一页
    function loadNextPage() {
        // 触发下一页加载的逻辑,这里假设下一页链接是可点击的
        const nextLink = document.querySelector('.pagination .next, .pager .next, .page-numbers.dots + .page-numbers');
        if (nextLink) {
            simulateClick(nextLink);
        }
    }

    // 模拟点击事件,以兼容那些通过点击事件触发加载的网站
    function simulateClick(element) {
        const event = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window
        });
        element.dispatchEvent(event);
    }

    // 添加一个延迟来确保页面完全加载后再进行操作
    setTimeout(() => {
        startObserving();
        if (shouldLoadNextPage()) {
            loadNextPage();
        }
    }, 100); // 延时0.1秒,可根据实际情况调整
})();