正念浏览控制 (Ctrl+M)

提醒你保持正念,控制浏览网站的时间。按Ctrl+M查看设置。

当前为 2025-02-04 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         正念浏览控制 (Ctrl+M)
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  提醒你保持正念,控制浏览网站的时间。按Ctrl+M查看设置。
// @author       KQ yang
// @match        *://*/*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 默认配置
    const DEFAULT_CONFIG = {
        startHour: 12,
        endHour: 14,
        restrictedSites: {},
        messageStyle: "background-color: white; font-size:48px; color: #333; text-align: center; padding-top: 30vh;"
    };

    let CONFIG = JSON.parse(localStorage.getItem('mindfulnessConfig')) || DEFAULT_CONFIG;

    // 添加样式
    function addStyles() {
        const styleSheet = document.createElement("style");
        styleSheet.textContent = `
            .mindfulness-panel {
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background: white;
                padding: 20px;
                border-radius: 10px;
                box-shadow: 0 10px 25px rgba(0,0,0,0.2);
                z-index: 999999;
                min-width: 300px;
                font-family: sans-serif;
            }

            .mindfulness-panel h2 {
                font-size: 24px;
                text-align: center;
                margin-bottom: 20px;
                color: #333;
            }

            .mindfulness-panel input[type="number"] {
                width: 50px;
                padding: 8px;
                margin-top: 10px;
                border: 1px solid #ddd;
                border-radius: 5px;
                font-size: 16px;
            }

            .mindfulness-panel .button-group {
                display: flex;
                justify-content: space-evenly;
                margin-top: 20px;
            }

            .mindfulness-panel button {
                padding: 8px 15px;
                border: none;
                border-radius: 5px;
                background-color: #4CAF50;
                color: white;
                font-size: 14px;
                cursor: pointer;
            }

            .mindfulness-panel button.cancel {
                background-color: #f44336;
            }

            .mindfulness-panel button:hover {
                opacity: 0.8;
            }
        `;
        document.head.appendChild(styleSheet);
    }

    // 创建配置面板
    function createConfigPanel() {
        const panel = document.createElement('div');
        panel.className = 'mindfulness-panel';
        const currentHost = window.location.hostname;
        const isRestricted = CONFIG.restrictedSites[currentHost];

        panel.innerHTML = `
            <h2>正念设置</h2>
            <div>当前网站: ${currentHost}</div>
            <div>
                <label>限制网站: 
                    <input type="checkbox" id="restrictSite" ${isRestricted ? 'checked' : ''}>
                </label>
            </div>
            <div>
                <label>访问时间:</label>
                <input type="number" id="startHour" value="${CONFIG.startHour}" min="0" max="23"> - 
                <input type="number" id="endHour" value="${CONFIG.endHour}" min="0" max="23">
            </div>
            <div class="button-group">
                <button id="saveConfig">保存</button>
                <button class="cancel" id="cancelConfig">取消</button>
            </div>
        `;

        panel.querySelector('#saveConfig').addEventListener('click', saveConfiguration);
        panel.querySelector('#cancelConfig').addEventListener('click', () => panel.remove());
        document.body.appendChild(panel);
    }

    // 保存配置
    function saveConfiguration() {
        const panel = document.querySelector('.mindfulness-panel');
        const currentHost = window.location.hostname;

        CONFIG.startHour = parseInt(panel.querySelector('#startHour').value);
        CONFIG.endHour = parseInt(panel.querySelector('#endHour').value);
        CONFIG.restrictedSites[currentHost] = panel.querySelector('#restrictSite').checked;

        localStorage.setItem('mindfulnessConfig', JSON.stringify(CONFIG));
        panel.remove();

        checkAndApplyRestriction();
    }

    // 检查并应用访问限制
    function checkAndApplyRestriction() {
        const currentHost = window.location.hostname;

        if (!CONFIG.restrictedSites[currentHost]) {
            return;
        }

        const now = new Date();
        const hours = now.getHours();
        if (hours < CONFIG.startHour || hours >= CONFIG.endHour) {
            document.body.innerHTML = `
                <div style="${CONFIG.messageStyle}">
                    <h1>亲爱的自己</h1>
                    <p>请回归正念,暂时离开此页面。</p>
                    <p>该站点仅可在 ${CONFIG.startHour}:00 - ${CONFIG.endHour}:00 之间访问。</p>
                </div>
            `;
        }
    }

    // 监听快捷键
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.key.toLowerCase() === 'm') {
            e.preventDefault();
            createConfigPanel();
        }
    });

    // 初始化
    addStyles();
    checkAndApplyRestriction();
})();