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      3.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 addSiteManually() {
        const domain = prompt("请输入要屏蔽的网站域名(例如:example.com):", "");
        if (!domain) return;
        
        const cleanDomain = domain.trim().toLowerCase().replace(/^www\./, '');
        if (!cleanDomain) return;
        
        const blockedSites = getBlockedSites();
        if (blockedSites.includes(cleanDomain)) {
            alert(`"${cleanDomain}" 已在屏蔽列表中!`);
            return;
        }
        
        blockedSites.push(cleanDomain);
        saveBlockedSites(blockedSites);
        alert(`已添加: ${cleanDomain}\n\n新屏蔽列表:\n${blockedSites.join('\n')}`);
        location.reload();
    }

    // 从屏蔽列表中删除网站
    function removeSiteManually() {
        const blockedSites = getBlockedSites();
        if (blockedSites.length === 0) {
            alert("屏蔽列表为空!");
            return;
        }
        
        const domain = prompt(`请输入要移除的域名:\n\n当前屏蔽列表:\n${blockedSites.join('\n')}`, "");
        if (!domain) return;
        
        const cleanDomain = domain.trim().toLowerCase().replace(/^www\./, '');
        if (!cleanDomain) return;
        
        if (!blockedSites.includes(cleanDomain)) {
            alert(`"${cleanDomain}" 不在屏蔽列表中!`);
            return;
        }
        
        const newSites = blockedSites.filter(site => site !== cleanDomain);
        saveBlockedSites(newSites);
        alert(`已移除: ${cleanDomain}\n\n新屏蔽列表:\n${newSites.join('\n')}`);
        location.reload();
    }

    // 管理当前网站屏蔽状态
    function manageCurrentSite() {
        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("添加网站到屏蔽列表", addSiteManually);
        GM_registerMenuCommand("从屏蔽列表中删除网站", removeSiteManually);
        GM_registerMenuCommand("管理当前网站屏蔽状态", manageCurrentSite);
        GM_registerMenuCommand("查看屏蔽网站列表", viewBlockedSites);
        GM_registerMenuCommand("清除所有屏蔽网站", clearAllSites);
    }

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

QingJ © 2025

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