FlexiRefresh

randomized delays auto-refresh

当前为 2025-04-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         FlexiRefresh
// @namespace    https://github.com/RustwuIf
// @version      1.0.1
// @description  randomized delays auto-refresh
// @author       Rustwulf
// @match        <all_urls>
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Core settings
    const storageKey = 'autoRefreshState';
    let isRunning = false;
    let remainingTime = 0;
    let countdownInterval = null;
    let minDelay = 5;
    let maxDelay = 15;

    // Create UI elements
    const mainButton = document.createElement('button');
    mainButton.style.cssText = `
        position: fixed;
        bottom: 5px;
        left: 15px;
        padding: 2px 2px;
        background: #FF6B6B;
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 10px;
        cursor: pointer;
        z-index: 99999;
    `;
    mainButton.textContent = '▶️⇧+T';

    const settingsButton = document.createElement('button');
    settingsButton.style.cssText = `
        position: fixed;
        bottom: 5px;
        left: 55px;
        padding: 2px 2px;
        background: #FFFFFF;
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 10px;
        cursor: pointer;
        z-index: 99999;
    `;
    settingsButton.textContent = '⚙️';

    const countdownDisplay = document.createElement('div');
    countdownDisplay.style.cssText = `
        position: fixed;
        bottom: 23px;
        left: 17px;
        background: rgba(0,0,0,0.2);
        color: white;
        padding: 5px;
        border-radius: 8px;
        font-size: 9px;
        display: none;
    `;

    const settingsModal = document.createElement('div');
    settingsModal.style.cssText = `
        position: fixed;
        Bottom: 20px;
        left: 10px;
        width: 200px;
        background: Black;
        border: 1px solid #ccc;
        padding: 20px;
        border-radius: 8px;
        z-index: 99999;
        display: none;
        box-shadow: 0 0 10px rgba(0,0,0,0.3);
    `;
    settingsModal.innerHTML = `
        <h3 style="margin-bottom: 15px; color: white;">Settings</h3>
        <div style="display: flex; align-items: center; margin-bottom: 10px;">
            <label style="width: 120px; margin-right: 10px; color: white;">Min Delay (s):</label>
            <input type="number" id="minDelay" value="${minDelay}" style="padding: 6px; border: 1px solid #ddd; border-radius: 4px; width: 70px;">
        </div>
        <div style="display: flex; align-items: center; margin-bottom: 20px;">
            <label style="width: 120px; margin-right: 10px; color: white;">Max Delay (s):</label>
            <input type="number" id="maxDelay" value="${maxDelay}" style="padding: 6px; border: 1px solid #ddd; border-radius: 4px; width: 70px;">
        </div>
        <div style="display: flex; justify-content: flex-end;">
            <button id="saveSettings" style="padding: 8px 15px; background: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; margin-right: 10px;">Save</button>
            <button id="closeModal" style="padding: 8px 15px; background: #ff4d4d; color: white; border: none; border-radius: 4px; cursor: pointer;">Close</button>
        </div>
    `;

    // Append to DOM immediately
    document.body.appendChild(mainButton);
    document.body.appendChild(settingsButton);
    document.body.appendChild(countdownDisplay);
    document.body.appendChild(settingsModal);

    // Event listeners
    mainButton.addEventListener('click', () => {
        isRunning = !isRunning;
        localStorage.setItem(storageKey, isRunning);
        mainButton.textContent = isRunning ? '⏸️⇧+T' : '▶️⇧+T';
        mainButton.style.backgroundColor = isRunning ? '#FF4444' : '#FF6B6B';
        if (isRunning) startAutoRefresh();
        else clearInterval(countdownInterval);
    });

    settingsButton.addEventListener('click', () => settingsModal.style.display = 'block');
    document.getElementById('closeModal').addEventListener('click', () => settingsModal.style.display = 'none');
    document.getElementById('saveSettings').addEventListener('click', () => {
        minDelay = document.getElementById('minDelay').value;
        maxDelay = document.getElementById('maxDelay').value;
        settingsModal.style.display = 'none';
    });

    document.addEventListener('keydown', (e) => {
        if (e.shiftKey && e.key === 'T') {
            e.preventDefault();
            mainButton.click();
        }
    });

    // Auto-refresh logic
    function startAutoRefresh() {
        const delay = Math.random() * (maxDelay - minDelay) + minDelay;
        remainingTime = Math.floor(delay);
        countdownDisplay.style.display = 'block';
        countdownInterval = setInterval(() => {
            if (remainingTime <= 0) {
                clearInterval(countdownInterval);
                window.location.reload();
                remainingTime = 0;
            } else {
                remainingTime--;
                countdownDisplay.textContent = `🔄 ${remainingTime}s`;
            }
        }, 1000);
    }

    // Restore state
    window.addEventListener('load', () => {
        const storedState = localStorage.getItem(storageKey);
        if (storedState === 'true') {
            isRunning = true;
            mainButton.textContent = '⏸️⇧+T';
            mainButton.style.backgroundColor = '#FF4444';
            startAutoRefresh();
        }
    });
})();