Site Blocker, to be more focused

屏蔽分心网站,显示全屏提醒以保持专注

当前为 2025-07-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         Site Blocker, to be more focused
// @namespace    https://gf.qytechs.cn/users/1111205-geekfox
// @version      2.0
// @description  屏蔽分心网站,显示全屏提醒以保持专注
// @author       GeekFox
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // ======= 配置区域 =======
    const DEFAULT_BLOCKED_SITES = [
        "jandan.net",
        "zhihu.com"
    ];
    const WARNING_TEXT = "Be Focused!"; // 警告文本
    const BACKGROUND_COLOR = "#ff0000"; // 背景颜色(红色)
    const TEXT_COLOR = "#ffffff";       // 文字颜色(白色)
    // ======================

    // 获取存储的屏蔽网站列表
    function getBlockedSites() {
        const storedSites = GM_getValue('blockedSites');
        return storedSites !== undefined ? storedSites : DEFAULT_BLOCKED_SITES;
    }

    // 保存屏蔽网站列表
    function saveBlockedSites(sites) {
        GM_setValue('blockedSites', sites);
    }

    // 检查当前网站是否在屏蔽列表中
    function isBlockedSite() {
        const hostname = window.location.hostname.toLowerCase();
        return getBlockedSites().some(site => {
            const domain = site.toLowerCase().replace(/^www\./, '');
            return (
                hostname === domain ||
                hostname.endsWith(`.${domain}`)
            );
        });
    }

    // 创建全屏警告
    function createFullscreenWarning() {
        const overlay = document.createElement('div');
        overlay.id = 'focusBlockerOverlay';
        overlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background: ${BACKGROUND_COLOR};
            z-index: 999999;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            cursor: default;
        `;

        const textElement = document.createElement('div');
        textElement.textContent = WARNING_TEXT;
        textElement.style.cssText = `
            color: ${TEXT_COLOR};
            font-size: 10vw;
            font-weight: bold;
            text-align: center;
            text-shadow: 0 0 10px rgba(0,0,0,0.5);
            user-select: none;
            max-width: 90vw;
            line-height: 1.2;
        `;

        const hintElement = document.createElement('div');
        hintElement.textContent = "Press ESC to temporarily access (5 mins)";
        hintElement.style.cssText = `
            color: rgba(255,255,255,0.7);
            font-size: 2vw;
            margin-top: 40px;
            text-align: center;
            user-select: none;
        `;

        overlay.appendChild(textElement);
        overlay.appendChild(hintElement);
        document.documentElement.appendChild(overlay);
        return overlay;
    }

    // 管理屏蔽列表
    function manageBlockedSites() {
        const currentHost = window.location.hostname;
        const blockedSites = getBlockedSites();
        const isBlocked = blockedSites.includes(currentHost);

        if (isBlocked) {
            if (confirm(`"${currentHost}" 已在屏蔽列表中,是否移除?`)) {
                const newSites = blockedSites.filter(site => site !== currentHost);
                saveBlockedSites(newSites);
                alert(`已移除: ${currentHost}\n\n新屏蔽列表:\n${newSites.join('\n')}`);
                location.reload();
            }
        } else {
            if (confirm(`是否将 "${currentHost}" 添加到屏蔽列表?`)) {
                blockedSites.push(currentHost);
                saveBlockedSites(blockedSites);
                alert(`已添加: ${currentHost}\n\n新屏蔽列表:\n${blockedSites.join('\n')}`);
                location.reload();
            }
        }
    }

    // 查看屏蔽列表
    function viewBlockedSites() {
        const blockedSites = getBlockedSites();
        alert(`当前屏蔽的网站 (${blockedSites.length}个):\n\n${blockedSites.join('\n')}`);
    }

    // 清除所有屏蔽网站
    function clearAllSites() {
        if (confirm("确定要清除所有屏蔽网站吗?")) {
            saveBlockedSites([]);
            alert("已清除所有屏蔽网站!");
            location.reload();
        }
    }

    // 主函数
    function main() {
        if (isBlockedSite()) {
            const overlay = createFullscreenWarning();
            let temporaryAccess = false;
            
            document.addEventListener('keydown', (e) => {
                if (e.key === 'Escape') {
                    overlay.style.display = 'none';
                    temporaryAccess = true;
                    
                    setTimeout(() => {
                        if (temporaryAccess) {
                            overlay.style.display = 'flex';
                            temporaryAccess = false;
                        }
                    }, 5 * 60 * 1000);
                }
            });
        }

        // 注册(不可用)菜单命令
        GM_registerMenuCommand("管理当前网站屏蔽状态", manageBlockedSites);
        GM_registerMenuCommand("查看屏蔽网站列表", viewBlockedSites);
        GM_registerMenuCommand("清除所有屏蔽网站", clearAllSites);
    }

    // 确保在DOM加载前执行
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', main);
    } else {
        main();
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址