Greasy Fork

Greasy Fork is available in English.

页面定时刷新

定时刷新页面

当前为 2024-05-15 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         页面定时刷新
// @namespace    http://your-namespace.com
// @version      1.0.0
// @description  定时刷新页面
// @author       ghhgy
// @match        *://*/*
// @grant        none
// @license      GPL
// ==/UserScript==

(function() {
    'use strict';

    // 从本地存储中获取上次保存的状态和刷新间隔
    const isRefreshEnabled = localStorage.getItem('isRefreshEnabled') === 'true';
    const savedRefreshInterval = localStorage.getItem('refreshInterval');

    // 如果启动了定时刷新,且存在刷新间隔,自动启动定时刷新
    if (isRefreshEnabled && savedRefreshInterval) {
        const refreshIntervalInMilliseconds = savedRefreshInterval * 1000;

        function refreshPage() {
            location.reload(true);
        }

        window.refreshIntervalId = setInterval(refreshPage, refreshIntervalInMilliseconds);
    }

    // 创建设置框
    const settingsBox = document.createElement('div');
    settingsBox.style.position = 'fixed';
    settingsBox.style.top = '10px';
    settingsBox.style.right = '10px';
    settingsBox.style.padding = '10px';
    settingsBox.style.backgroundColor = 'white';
    settingsBox.style.border = '1px solid #ccc';
    settingsBox.style.zIndex = '9999'; // 设置z-index确保在最上层
    document.body.appendChild(settingsBox);

    // 添加输入框和按钮
    const inputField = document.createElement('input');
    inputField.type = 'number';
    inputField.placeholder = '刷新间隔(秒)';

    // 如果存在刷新间隔,显示在输入框中
    if (savedRefreshInterval) {
        inputField.value = savedRefreshInterval;
    }

    settingsBox.appendChild(inputField);

    const startButton = document.createElement('button');
    startButton.textContent = isRefreshEnabled ? '停止定时刷新' : '启动定时刷新';
    settingsBox.appendChild(startButton);

    // 启动/停止定时器
    startButton.addEventListener('click', function() {
        const refreshInterval = inputField.value;

        // 保存用户设置到本地存储
        localStorage.setItem('refreshInterval', refreshInterval);

        const refreshIntervalInMilliseconds = refreshInterval * 1000;

        function refreshPage() {
            location.reload(true);
        }

        // 切换启动/停止状态
        if (isRefreshEnabled) {
            clearInterval(window.refreshIntervalId);
            startButton.textContent = '启动定时刷新';
        } else {
            window.refreshIntervalId = setInterval(refreshPage, refreshIntervalInMilliseconds);
            startButton.textContent = '停止定时刷新';
        }

        // 保存状态到本地存储
        localStorage.setItem('isRefreshEnabled', !isRefreshEnabled);
    });
})();