Greasy Fork

来自缓存

Greasy Fork is available in English.

页面定时刷新带输入控制(持久化版)

添加开始/停止刷新按钮、倒计时显示和自定义时间输入,刷新后继续计时

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         页面定时刷新带输入控制(持久化版)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  添加开始/停止刷新按钮、倒计时显示和自定义时间输入,刷新后继续计时
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let refreshInterval = null;
    let countdownTimer = null;
    let countdown = 0;
    let intervalSeconds = parseInt(localStorage.getItem('refresh_interval')) || 5;
    let isRunning = localStorage.getItem('refresh_running') === 'true';

    // 创建控制面板
    const panel = document.createElement('div');
    panel.style.position = 'fixed';
    panel.style.top = '10px';
    panel.style.right = '10px';
    panel.style.background = 'rgba(0,0,0,0.7)';
    panel.style.color = '#fff';
    panel.style.padding = '10px';
    panel.style.zIndex = '99999';
    panel.style.borderRadius = '5px';
    panel.style.fontSize = '14px';

    const inputBox = document.createElement('input');
    inputBox.type = 'number';
    inputBox.value = intervalSeconds;
    inputBox.style.width = '50px';
    inputBox.style.marginRight = '5px';

    const startBtn = document.createElement('button');
    startBtn.textContent = '开始刷新';
    startBtn.style.marginRight = '5px';

    const stopBtn = document.createElement('button');
    stopBtn.textContent = '停止刷新';

    const countdownDisplay = document.createElement('span');
    countdownDisplay.textContent = '倒计时: 未开始';

    panel.appendChild(document.createTextNode('间隔(s): '));
    panel.appendChild(inputBox);
    panel.appendChild(startBtn);
    panel.appendChild(stopBtn);
    panel.appendChild(document.createElement('br'));
    panel.appendChild(countdownDisplay);
    document.body.appendChild(panel);

    // 启动逻辑
    function startRefresh() {
        intervalSeconds = parseInt(inputBox.value, 10) || 5;
        localStorage.setItem('refresh_interval', intervalSeconds);
        localStorage.setItem('refresh_running', 'true');

        countdown = intervalSeconds;
        countdownDisplay.textContent = `倒计时: ${countdown}s`;

        countdownTimer = setInterval(() => {
            countdown--;
            countdownDisplay.textContent = `倒计时: ${countdown}s`;
            if (countdown <= 0) {
                location.reload();
            }
        }, 1000);

        refreshInterval = setInterval(() => {
            countdown = intervalSeconds;
        }, intervalSeconds * 1000);

        startBtn.disabled = true;
        stopBtn.disabled = false;
        inputBox.disabled = true;
    }

    // 停止逻辑
    function stopRefresh() {
        clearInterval(refreshInterval);
        clearInterval(countdownTimer);
        refreshInterval = null;
        countdownTimer = null;
        countdownDisplay.textContent = '倒计时: 已停止';
        startBtn.disabled = false;
        stopBtn.disabled = true;
        inputBox.disabled = false;
        localStorage.setItem('refresh_running', 'false');
    }

    startBtn.addEventListener('click', startRefresh);
    stopBtn.addEventListener('click', stopRefresh);

    // 页面加载时恢复状态
    if (isRunning) {
        startRefresh();
    } else {
        stopRefresh();
    }
})();