油猴脚本-自动刷新网页

// ==UserScript==
// @name         Userscript_reload
// @namespace    http://tampermonkey.net/
// @version      2025-09-29
// @description  每x分钟自动刷新当前页面,可手动暂停/继续,支持窗口拖动
// @author       You
// @match        https://www.baidu.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 刷新间隔:x分钟 +(1--10)随机秒
    const REFRESH_INTERVAL =(5*60+Math.floor(Math.random() * 11))*1000;

    let timer = null;
    let remainingTime = REFRESH_INTERVAL;
    let isRunning = true;
    let isDragging = false;
    let offsetX, offsetY;

    // 创建控制界面
    function createControlPanel() {
        const panel = document.createElement('div');
        panel.id = 'autoRefreshPanel';
        panel.style.position = 'fixed';
        panel.style.bottom = '20px';
        panel.style.right = '20px';
        panel.style.backgroundColor = 'rgba(0,0,0,0.7)';
        panel.style.color = 'white';
        panel.style.padding = '10px 15px';
        panel.style.borderRadius = '5px';
        panel.style.zIndex = '999999';
        panel.style.fontFamily = 'Arial, sans-serif';
        panel.style.fontSize = '14px';
        panel.style.cursor = 'move'; // 提示可拖动

        // 添加标题栏(可拖动区域)
        const header = document.createElement('div');
        header.style.marginBottom = '8px';
        header.style.fontWeight = 'bold';
        header.style.userSelect = 'none'; // 防止拖动时选中文本
        header.textContent = '自动刷新控制';

        // 状态显示
        const statusDiv = document.createElement('div');
        statusDiv.id = 'autoRefreshStatus';
        statusDiv.textContent = `自动刷新: 启用 (剩余: ${formatTime(remainingTime)})`;
        statusDiv.style.marginBottom = '8px';
        statusDiv.style.cursor = 'default'; // 状态区恢复默认光标

        // 控制按钮
        const toggleBtn = document.createElement('button');
        toggleBtn.textContent = '暂停';
        toggleBtn.style.marginLeft = '10px';
        toggleBtn.style.padding = '3px 8px';
        toggleBtn.style.cursor = 'pointer';
        toggleBtn.style.border = 'none';
        toggleBtn.style.borderRadius = '3px';
        toggleBtn.style.backgroundColor = '#ff4444';
        toggleBtn.style.color = 'white';
        toggleBtn.style.cursor = 'default'; // 按钮恢复默认光标

        toggleBtn.addEventListener('click', toggleRefresh);

        // 组装面板
        panel.appendChild(header);
        panel.appendChild(statusDiv);
        panel.appendChild(toggleBtn);
        document.body.appendChild(panel);

        // 添加拖动事件监听
        addDragEvents(panel);
    }

    // 添加拖动功能
    function addDragEvents(element) {
        // 鼠标按下时开始拖动
        element.addEventListener('mousedown', (e) => {
            // 只有点击标题栏才允许拖动
            if (e.target === element || e.target === element.firstChild) {
                isDragging = true;
                // 计算鼠标相对于面板的位置
                const rect = element.getBoundingClientRect();
                offsetX = e.clientX - rect.left;
                offsetY = e.clientY - rect.top;
                element.style.transition = 'none'; // 关闭动画使拖动更流畅
            }
        });

        // 鼠标移动时更新面板位置
        document.addEventListener('mousemove', (e) => {
            if (!isDragging) return;

            const panel = document.getElementById('autoRefreshPanel');
            // 计算新位置(确保面板不会超出视口)
            const newX = e.clientX - offsetX;
            const newY = e.clientY - offsetY;

            // 限制在窗口内
            const maxX = window.innerWidth - panel.offsetWidth;
            const maxY = window.innerHeight - panel.offsetHeight;
            const constrainedX = Math.max(0, Math.min(newX, maxX));
            const constrainedY = Math.max(0, Math.min(newY, maxY));

            // 设置新位置
            panel.style.left = `${constrainedX}px`;
            panel.style.top = `${constrainedY}px`;
            // 清除原来的bottom和right属性,改用left和top
            panel.style.bottom = 'auto';
            panel.style.right = 'auto';
        });

        // 鼠标释放时停止拖动
        document.addEventListener('mouseup', () => {
            if (isDragging) {
                isDragging = false;
                const panel = document.getElementById('autoRefreshPanel');
                panel.style.transition = 'all 0.1s ease'; // 恢复轻微动画
            }
        });

        // 鼠标离开窗口时停止拖动
        document.addEventListener('mouseleave', () => {
            isDragging = false;
            const panel = document.getElementById('autoRefreshPanel');
            panel.style.transition = 'all 0.1s ease';
        });
    }

    // 格式化时间显示 (毫秒 -> 分:秒)
    function formatTime(ms) {
        const totalSeconds = Math.floor(ms / 1000);
        const minutes = Math.floor(totalSeconds / 60);
        const seconds = totalSeconds % 60;
        return `${minutes}:${seconds.toString().padStart(2, '0')}`;
    }

    // 更新倒计时显示
    function updateDisplay() {
        const statusDiv = document.getElementById('autoRefreshStatus');
        if (statusDiv) {
            statusDiv.textContent = `自动刷新: ${isRunning ? '启用' : '暂停'} (剩余: ${formatTime(remainingTime)})`;
        }
    }

    // 切换刷新状态 (暂停/继续)
    function toggleRefresh() {
        isRunning = !isRunning;
        const btn = document.querySelector('#autoRefreshPanel button');

        if (isRunning) {
            btn.textContent = '暂停';
            btn.style.backgroundColor = '#ff4444';
            startTimer();
        } else {
            btn.textContent = '继续';
            btn.style.backgroundColor = '#00C851';
            clearInterval(timer);
        }
        updateDisplay();
    }

    // 开始倒计时
    function startTimer() {
        clearInterval(timer);
        timer = setInterval(() => {
            if (isRunning) {
                remainingTime -= 1000;
                updateDisplay();

                if (remainingTime <= 0) {
                    // 刷新页面
                    location.reload();
                }
            }
        }, 1000);
    }

    // 初始化
    createControlPanel();
    startTimer();

    // 页面卸载时清除定时器
    window.addEventListener('beforeunload', () => {
        clearInterval(timer);
    });
})();

  

posted @ 2025-09-29 11:03  wsh3166Sir  阅读(29)  评论(0)    收藏  举报