跳到正文

个人记录

犇犇自动刷新

Forever_Rain约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年5月6日
收录
2026年5月6日
更新
2026年8月20日
篇幅
约 11 分钟

版本

  1. v1当前

可以在左下角开启或关闭功能。

可以更改 REFRESH_INTERVAL_MS 常量的值来改变自动刷新犇犇速度,以ms为单位。

原理:模拟点击。

初始默认为 15s,用户自行更改所导致的高频请求该插件不负责任。

JavaScript
// ==UserScript==
// @name         犇犇自动刷新
// @version      1.0
// @description  每15秒自动刷新犇犇
// @author       Forever_Rain
// @match        https://www.luogu.com.cn/
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const REFRESH_INTERVAL_MS = 15000;// 15秒
    const STORAGE_KEY = 'luoguAutoRefreshWatching';
    let intervalId = null;
    let isEnabled = true;
    let switchBtn = null;
    let lastPathname = '';
    function loadPreference() {
        const stored = localStorage.getItem(STORAGE_KEY);
        if (stored === null) return true;
        return stored === 'true';
    }
    function savePreference(value) {
        localStorage.setItem(STORAGE_KEY, String(value));
    }
    function updateButtonUI() {
        if (!switchBtn) return;
        if (isEnabled) {
            switchBtn.textContent = '--- 自动刷新:开';
            switchBtn.style.background = 'rgba(0, 0, 0, 0.75)';
            switchBtn.style.borderLeft = '3px solid #2ecc71';
        } else {
            switchBtn.textContent = '--- 自动刷新:关';
            switchBtn.style.background = 'rgba(0, 0, 0, 0.55)';
            switchBtn.style.borderLeft = '3px solid #e74c3c';
        }
    }
    function clickWatchingTab() {
        const watchingLi = document.querySelector('.feed-selector[data-mode="watching"]');
        if (!watchingLi) {
            console.warn('[自动刷新] 未找到“我关注的”标签,可能DOM未加载完成或页面结构变化');
            return;
        }
        const clickTarget = watchingLi.querySelector('a') || watchingLi;
        clickTarget.click();
        console.log('succeed');
    }
    function stopAutoRefresh() {
        if (intervalId !== null) {
            clearInterval(intervalId);
            intervalId = null;
            console.log('stop');
        }
    }
    function startAutoRefreshIfNeeded() {
        stopAutoRefresh();
        if (isEnabled && location.pathname === '/') {
            intervalId = setInterval(() => {
                clickWatchingTab();
            }, REFRESH_INTERVAL_MS);
            console.log('on');
        }
    }
    function toggleAutoRefresh() {
        isEnabled = !isEnabled;
        savePreference(isEnabled);
        updateButtonUI();
        if (isEnabled) {
            startAutoRefreshIfNeeded();
            if (location.pathname === '/') {
                clickWatchingTab();
            }
        } else {
            stopAutoRefresh();
        }
    }
    function onRouteChange() {
        const isHomePage = (location.pathname === '/');
        if (isHomePage) {
            if (switchBtn) switchBtn.style.display = 'flex';
            const storedPref = loadPreference();
            if (storedPref !== isEnabled) {
                isEnabled = storedPref;
                updateButtonUI();
            }
            startAutoRefreshIfNeeded();
        } else {
            if (switchBtn) switchBtn.style.display = 'none';
            stopAutoRefresh();
        }
    }
    function createSwitchButton() {
        const btn = document.createElement('div');
        btn.id = 'luogu-auto-refresh-watching-switch';
        btn.textContent = '';
        btn.style.cssText = `
            position: fixed;
            bottom: 20px;
            left: 20px;
            z-index: 9999;
            background: rgba(0, 0, 0, 0.75);
            backdrop-filter: blur(4px);
            color: white;
            padding: 8px 16px;
            border-radius: 40px;
            font-size: 14px;
            font-family: system-ui, -apple-system, 'Open Sans', sans-serif;
            font-weight: 500;
            cursor: pointer;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            transition: all 0.2s ease;
            border: none;
            border-left: 3px solid #2ecc71;
            user-select: none;
        `;
        btn.addEventListener('click', toggleAutoRefresh);
        document.body.appendChild(btn);
        switchBtn = btn;
        updateButtonUI();
    }
    function watchRouteChange() {
        const originalPushState = history.pushState;
        const originalReplaceState = history.replaceState;
        history.pushState = function(...args) {
            originalPushState.apply(this, args);
            onRouteChange();
        };
        history.replaceState = function(...args) {
            originalReplaceState.apply(this, args);
            onRouteChange();
        };
        window.addEventListener('popstate', onRouteChange);
        window.addEventListener('hashchange', onRouteChange);
    }
    function init() {
        isEnabled = loadPreference();
        createSwitchButton();
        watchRouteChange();
        onRouteChange();
        setInterval(() => {
            if (location.pathname === '/' && isEnabled && intervalId === null) {
                console.log('Null');
                startAutoRefreshIfNeeded();
            }
            if (switchBtn) {
                const shouldShow = (location.pathname === '/');
                if (shouldShow && switchBtn.style.display === 'none') switchBtn.style.display = 'flex';
                if (!shouldShow && switchBtn.style.display !== 'none') switchBtn.style.display = 'none';
            }
        }, 5000);
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
原文