定时关闭网页

页面加载弹出滑块设置倒计时,小球显示秒级倒计时+环形进度条,支持拖拽、点击修改时间、暂停/继续

目前為 2025-08-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         定时关闭网页
// @namespace    https://github.com/yingchen6
// @version      1.6
// @description  页面加载弹出滑块设置倒计时,小球显示秒级倒计时+环形进度条,支持拖拽、点击修改时间、暂停/继续
// @author       yingchen6
// @match        *://*/*
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let timerId = null;
    let remainingSeconds = 0;

    function formatTime(seconds) {
        const m = Math.floor(seconds / 60);
        const s = seconds % 60;
        return `${m}:${s.toString().padStart(2,'0')}`;
    }

    function startCountdown(minutes) {
        remainingSeconds = minutes * 60;

        // 创建秒级倒计时显示
        let countdownDisplay = document.getElementById("countdown-display");
        if(!countdownDisplay){
            countdownDisplay = document.createElement("div");
            countdownDisplay.id = "countdown-display";
            countdownDisplay.style.position = "fixed";
            countdownDisplay.style.top = "10px";
            countdownDisplay.style.right = "10px";
            countdownDisplay.style.background = "rgba(0,0,0,0.6)";
            countdownDisplay.style.color = "#fff";
            countdownDisplay.style.fontSize = "18px";
            countdownDisplay.style.padding = "8px 12px";
            countdownDisplay.style.borderRadius = "8px";
            countdownDisplay.style.zIndex = "999999";
            countdownDisplay.style.fontFamily = "sans-serif";
            document.body.appendChild(countdownDisplay);
        }

        if(timerId) clearInterval(timerId);

        timerId = setInterval(()=>{
            remainingSeconds--;
            countdownDisplay.textContent = "剩余: " + formatTime(remainingSeconds);
            if(remainingSeconds <= 0){
                clearInterval(timerId);
                window.location.href = "about:blank";
            }
        },1000);
    }

    function showTimeSetting() {
        const wrapper = document.createElement("div");
        wrapper.style.position = "fixed";
        wrapper.style.top = "50%";
        wrapper.style.left = "50%";
        wrapper.style.transform = "translate(-50%, -50%)";
        wrapper.style.background = "#1e1e1e";
        wrapper.style.color = "#fff";
        wrapper.style.padding = "20px";
        wrapper.style.borderRadius = "12px";
        wrapper.style.boxShadow = "0 0 15px rgba(0,0,0,0.5)";
        wrapper.style.display = "flex";
        wrapper.style.flexDirection = "column";
        wrapper.style.alignItems = "center";
        wrapper.style.zIndex = "999999";
        wrapper.style.cursor = "move";

        wrapper.innerHTML = `
            <h3>请选择倒计时</h3>
            <input id="timeRange" type="range" min="1" max="180" step="1" value="30" style="width:200px;">
            <span id="timeLabel">30 分钟</span>
            <div style="margin-top:10px;">
                <button id="confirmBtn">确定</button>
                <button id="cancelBtn">取消</button>
            </div>
        `;
        document.body.appendChild(wrapper);

        const range = wrapper.querySelector("#timeRange");
        const label = wrapper.querySelector("#timeLabel");
        const confirmBtn = wrapper.querySelector("#confirmBtn");
        const cancelBtn = wrapper.querySelector("#cancelBtn");

        range.addEventListener("input", ()=>{
            label.textContent = range.value + " 分钟";
        });

        confirmBtn.addEventListener("click", ()=>{
            const minutes = parseInt(range.value);
            document.body.removeChild(wrapper);
            startCountdown(minutes);
        });

        cancelBtn.addEventListener("click", ()=>{
            document.body.removeChild(wrapper);
        });

        // 拖拽功能
        let isDown = false, offsetX = 0, offsetY = 0;
        wrapper.addEventListener("mousedown", e=>{
            isDown = true;
            offsetX = e.clientX - wrapper.offsetLeft;
            offsetY = e.clientY - wrapper.offsetTop;
        });
        document.addEventListener("mousemove", e=>{
            if(isDown){
                wrapper.style.left = (e.clientX - offsetX) + "px";
                wrapper.style.top = (e.clientY - offsetY) + "px";
            }
        });
        document.addEventListener("mouseup", ()=>{ isDown = false; });
    }

    // 页面加载立即弹出设置
    window.addEventListener("load", ()=>{
        showTimeSetting();
    });

    // Tampermonkey 菜单
    if (typeof GM_registerMenuCommand !== "undefined") {
        GM_registerMenuCommand("开始计时", ()=>{
            showTimeSetting();
        });
    }

})();