linux.do刷帖

模拟浏览帖子

目前为 2024-03-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         linux.do刷帖
// @namespace    
// @version      2024-03-12
// @description  模拟浏览帖子
// @author       
// @match        https://linux.do/*
// @icon         https://linux.do/uploads/default/optimized/1X/3a18b4b0da3e8cf96f7eea15241c3d251f28a39b_2_180x180.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function clickRandomTitle() {
        const titles = document.getElementsByClassName('title raw-link raw-topic-link');
        if (titles.length > 0) {
            const randomIndex = Math.floor(Math.random() * titles.length);
            const randomTitle = titles[randomIndex];
            randomTitle.click();
        } else {
            console.log('No elements found with the specified class names.');

        }
    }

    function scrollToBottom() {
        const isBottom = window.innerHeight + window.scrollY >= document.body.offsetHeight;
        if (!isBottom) {
            window.scrollBy(0, 2000);
            setTimeout(scrollToBottom, 2000); // 控制滚动间隔
        } else {

            var dd='抱歉,我们无法加载该话题,可能是由于连接问题。请重试。如果问题仍然存在,请告诉我们。';
            if(document.body.textContent.includes(dd)){window.location.reload();}else{clickRandomTitle();
            console.log("Reached the bottom of the page.");};

        }
    }

    function debounce(func, wait) {
        let timeout;
        return function(...args) {
            const context = this;
            clearTimeout(timeout);
            timeout = setTimeout(function() {
                func.apply(context, args);
            }, wait);
        };
    }

    function hasPageUpdated(mutations) {
        return mutations.some(mutation => mutation.addedNodes.length > 0);
    }

    // 使用防抖函数来控制scrollToBottom的调用
    const debouncedScrollToBottom = debounce(scrollToBottom, 200); // 2秒后执行

    // 监听滚动事件,并在用户停止滚动后执行debouncedScrollToBottom
    let scrollTimeout;
    window.addEventListener('scroll', () => {
        clearTimeout(scrollTimeout);
        scrollTimeout = setTimeout(debouncedScrollToBottom, 1000); // 2秒后执行
    });

    // 监听DOM变化
    const observer = new MutationObserver(mutations => {
        if (hasPageUpdated(mutations)) {
            debouncedScrollToBottom();
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // 等待页面加载完毕
    document.addEventListener('DOMContentLoaded', () => {
        debouncedScrollToBottom();
        // 不要立即执行scrollToBottom,而是在用户滚动时触发
    });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址