Greasy Fork

Greasy Fork is available in English.

定时刷新

如题

当前为 2024-11-20 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         定时刷新
// @namespace    https://czm.cool
// @version      1.0
// @description  如题
// @author       czm
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==
// 修改自http://greasyfork.icu/zh-CN/scripts/39506-%E8%87%AA%E5%8A%A8%E5%88%B7%E6%96%B0%E9%A1%B5%E9%9D%A2
(function() {
    'use strict';

    var title, time = parseInt(sessionStorage.refreshTime), originTitle = document.title, lastSetTime = parseInt(sessionStorage.lastSetTime);

    // 循环时间
    function loop() {
        if (isNaN(time)) return;
        document.title = "[" + formatTime(time) + "] " + title;
        if (time <= 0) {
            location.reload();
            return;
        }
        time--;
        setTimeout(loop, 1000);
    }

    // 格式化时间
    function formatTime(t) {
        if (isNaN(t)) return "";
        var s = "";
        var h = parseInt(t / 3600);
        s += (pad(h) + ":");
        t -= (3600 * h);
        var m = parseInt(t / 60);
        s += (pad(m) + ":");
        t -= (60 * m);
        s += pad(t);
        return s;
    }

    // 补零
    function pad(n) {
        return ("00" + n).slice(-2);
    }

    // 创建按钮
    var button = document.createElement('button');
    button.textContent = '⏱️';
    button.style.position = 'fixed';
    button.style.bottom = '10px';
    button.style.right = '10px';
    button.style.zIndex = '9999';
    button.style.width = '50px';
    button.style.height = '50px';
    button.style.borderRadius = '50%';
    button.style.backgroundColor = '#007bff';
    button.style.border = 'none';
    button.style.outline = 'none';
    button.style.color = 'white';
    button.style.fontSize = '24px';
    button.style.cursor = 'pointer';
    button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)';

    // 创建配置面板
    var panel = document.createElement('div');
    panel.style.position = 'fixed';
    panel.style.bottom = '70px';
    panel.style.right = '10px';
    panel.style.display = 'none';
    panel.style.zIndex = '9999';
    panel.style.padding = '15px';
    panel.style.backgroundColor = '#fff';
    panel.style.borderRadius = '8px';
    panel.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.2)';
    panel.style.transition = 'all 0.3s ease';

    // 输入框
    var refreshInput = document.createElement('input');
    refreshInput.type = 'number';
    refreshInput.value = isNaN(lastSetTime) ? 60 : lastSetTime;
    refreshInput.style.width = '50px';
    refreshInput.style.marginRight = '10px';
    refreshInput.style.padding = '5px';
    refreshInput.style.borderRadius = '4px';
    refreshInput.style.border = '1px solid #ddd';

    // 单位标签
    var secondsLabel = document.createElement('span');
    secondsLabel.textContent = ' 秒';
    secondsLabel.style.marginLeft = '5px';
    secondsLabel.style.marginRight = '5px';

    // 设置按钮
    var setButton = document.createElement('button');
    setButton.textContent = '设置';
    setButton.style.padding = '5px 10px';
    setButton.style.borderRadius = '4px';
    setButton.style.border = 'none';
    setButton.style.backgroundColor = '#28a745';
    setButton.style.color = 'white';
    setButton.style.cursor = 'pointer';
    setButton.style.outline = 'none';
    setButton.style.boxShadow = 'inset 0 -2px 0 rgba(0, 0, 0, 0.2)';

    // 开始刷新按钮
    var startButton = document.createElement('button');
    startButton.textContent = '开始';
    startButton.style.padding = '5px 10px';
    startButton.style.borderRadius = '4px';
    startButton.style.border = 'none';
    startButton.style.backgroundColor = '#17a2b8';
    startButton.style.color = 'white';
    startButton.style.cursor = 'pointer';
    startButton.style.outline = 'none';
    startButton.style.boxShadow = 'inset 0 -2px 0 rgba(0, 0, 0, 0.2)';
    startButton.style.marginRight = '10px';

    // 关闭刷新按钮
    var stopButton = document.createElement('button');
    stopButton.textContent = '结束';
    stopButton.style.padding = '5px 10px';
    stopButton.style.borderRadius = '4px';
    stopButton.style.border = 'none';
    stopButton.style.backgroundColor = '#dc3545';
    stopButton.style.color = 'white';
    stopButton.style.cursor = 'pointer';
    stopButton.style.outline = 'none';
    stopButton.style.boxShadow = 'inset 0 -2px 0 rgba(0, 0, 0, 0.2)';

    // 第一行
    var row1 = document.createElement('div');
    row1.appendChild(refreshInput);
    row1.appendChild(secondsLabel);
    row1.appendChild(setButton);

    // 第二行
    var row2 = document.createElement('div');
    row2.style.marginTop = '10px';
    row2.appendChild(startButton);
    row2.appendChild(stopButton);

    // 将行添加到面板
    panel.appendChild(row1);
    panel.appendChild(row2);

    // 将按钮和面板添加到页面
    document.body.appendChild(button);
    document.body.appendChild(panel);

    // 按钮点击事件
    button.onclick = function() {
        panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
    };

    // 设置刷新时间
    setButton.onclick = function() {
        console.log(refreshInput.value);
        sessionStorage.refreshTime = refreshInput.value;
        sessionStorage.lastSetTime = refreshInput.value;
        time = refreshInput.value;
    }

    // 开始刷新
    startButton.onclick = function() {
        console.log('click start')
        sessionStorage.refreshTime = refreshInput.value;
        time = refreshInput.value;
        loop()
    };

    // 暂停刷新
    stopButton.onclick = function() {
        console.log('click pause')
        sessionStorage.refreshTime = undefined;
        time = undefined;
        document.title = originTitle;
    }

    // 刷新后开始新一轮计时
    loop()

})();