Facebook Access Controller

Enforces 15-minute cooldown between Facebook visits and limits each visit to 5 minutes

目前為 2025-02-28 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Facebook Access Controller
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Enforces 15-minute cooldown between Facebook visits and limits each visit to 5 minutes
// @author       luudanmatcuoi
// @license      MIT
// @match        *://*.facebook.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';
    const allow_duration = 5;
    const block_duration = 20;

    // Get the last visit time from storage
    const lastVisit = GM_getValue('lastFacebookVisit', null);
    const lastAllow = GM_getValue('lastFacebookAllow', null);
    const currentTime = new Date().getTime();

    // Nếu trước đó có lastVisit
    if (lastVisit) {
        var timeSinceLastVisit = (currentTime - parseInt(lastVisit)) / (1000 * 60); // in minutes
        var timeSinceLastAllow = (currentTime - parseInt(lastAllow)) / (1000 * 60); // in minutes

        // Nếu đang trong khoảng allow --> Chạy tiếp
        if (timeSinceLastAllow < allow_duration) {
            GM_setValue('stage_fb', true);
        // Nếu trong khoảng block --> Block và return
        } else if (timeSinceLastVisit < block_duration ) {
            const minutesAgo = Math.floor(timeSinceLastVisit);
            const secondsAgo = Math.floor((timeSinceLastVisit - minutesAgo) * 60);
            const waitTime = Math.ceil(block_duration - timeSinceLastVisit);

            // Create HTML for the block page
            const blockHTML = `
            <html>
            <head>
                <title>Facebook Break Time</title>
                <style>
                    body {
                        font-family: Arial, sans-serif;
                        text-align: center;
                        padding-top: 100px;
                        background-color: #f0f2f5;
                    }
                    .container {
                        max-width: 600px;
                        margin: 0 auto;
                        padding: 20px;
                        background-color: white;
                        border-radius: 8px;
                        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                    }
                    h1 {
                        color: #1877f2;
                    }
                    .timer {
                        font-size: 24px;
                        font-weight: bold;
                        margin: 20px 0;
                    }
                    .info {
                        margin-bottom: 20px;
                        color: #555;
                    }
                </style>
            </head>
            <body>
                <div class="container">
                    <h1>Facebook Break Time</h1>
                    <div class="info">
                        You visited Facebook ${minutesAgo} min ${secondsAgo} sec ago.<br>
                        Please take a break before returning.
                    </div>
                    <div class="timer">
                        Wait time: ${waitTime} minutes
                    </div>
                    <p>This page will automatically redirect you back when your break is complete.</p>
                    <div id="countdown"></div>
                </div>

                <script>
                    // Set the time we're counting down to
                    const redirectTime = new Date().getTime() + (${waitTime} * 60 * 1000);

                    // Update the countdown every 1 second
                    const countdownElement = document.getElementById("countdown");
                    const countdownTimer = setInterval(function() {
                        // Get current time
                        const now = new Date().getTime();

                        // Find the distance between now and the countdown time
                        const distance = redirectTime - now;

                        // Time calculations for minutes and seconds
                        const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                        const seconds = Math.floor((distance % (1000 * 60)) / 1000);

                        // Display the countdown
                        countdownElement.innerHTML = minutes + "m " + seconds + "s";

                        // If the countdown is finished, redirect back to Facebook
                        if (distance < 0) {
                            clearInterval(countdownTimer);
                            window.location.href = "${window.location.href}";
                        }
                    }, 1000);
                </script>
            </body>
            </html>
            `;
            document.open();
            document.write(blockHTML);
            document.close();
            return;

        // Nếu ngoài khoảng block --> Ghi lại lần chạy này và chạy tiếp
        } else {
            GM_setValue('stage_fb', false);
            GM_setValue('lastFacebookVisit', currentTime.toString());
            GM_setValue('lastFacebookAllow', currentTime.toString());
        }
    }

    // Create container for the timer
    const timerContainer = document.createElement('div');
    timerContainer.style.position = 'fixed';
    timerContainer.style.bottom = '10px';
    timerContainer.style.left = '10px';
    timerContainer.style.backgroundColor = '#12224f';
    timerContainer.style.color = 'white';
    timerContainer.style.padding = '5px 5px';
    timerContainer.style.borderRadius = '5px';
    timerContainer.style.zIndex = '9999';
    timerContainer.style.boxShadow = '0 2px 4px rgba(0,0,0,0.2)';
    timerContainer.style.fontSize = '16px';
    timerContainer.style.fontWeight = 'bold';

    if (lastVisit) {
        const lastVisitDate = new Date(parseInt(lastVisit));
        const timeSinceLastVisit = Math.floor((currentTime - parseInt(lastVisit)) / (1000 * 60));
        // notification.textContent = `Visit allowed. Last visit was ${timeSinceLastVisit} minutes ago (${lastVisitDate.toLocaleTimeString()}).`;
    } else {
        var aha = 3;
    }

    // Add notification and timer to page when body is available
    const addElementsToPage = () => {
        document.body.appendChild(timerContainer);
        // Start 5-minute countdown for this session
        let stage = GM_getValue('stage_fb', false);
        let secondsLeft = allow_duration * 60;
        if (stage===true) {
            //GM_setValue('secondsLeft', allow_duration * 60 - parseInt(timeSinceLastAllow * 60));
            secondsLeft = allow_duration * 60 - parseInt(timeSinceLastAllow * 60);
        }
        const updateTimer = () => {
            const minutes = Math.floor(secondsLeft / 60);
            const seconds = secondsLeft % 60;
            timerContainer.textContent = `Time remaining: ${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;

            if (secondsLeft <= 0) {
                // Time's up, redirect to a "session ended" page
                const sessionEndedHTML = `
                <html>
                <head>
                    <title>Facebook Session Ended</title>
                    <style>
                        body {
                            font-family: Arial, sans-serif;
                            text-align: center;
                            padding-top: 100px;
                            background-color: #f0f2f5;
                        }
                        .container {
                            max-width: 600px;
                            margin: 0 auto;
                            padding: 20px;
                            background-color: white;
                            border-radius: 8px;
                            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                        }
                        h1 {
                            color: #1877f2;
                        }
                        .message {
                            margin: 20px 0;
                            font-size: 18px;
                        }
                        .info {
                            margin-bottom: 20px;
                            color: #555;
                        }
                    </style>
                </head>
                <body>
                    <div class="container">
                        <h1>Facebook Session Ended</h1>
                        <div class="message">
                            You've used your 5-minute Facebook allowance.
                        </div>
                        <div class="info">
                            You can return after a 10-minute break.
                        </div>
                    </div>
                </body>
                </html>
                `;

                document.open();
                document.write(sessionEndedHTML);
                document.close();
                return;
            }

            secondsLeft--;
            setTimeout(updateTimer, 1000);
        };

        updateTimer();

    };

    if (document.body) {
        addElementsToPage();
    } else {
        window.addEventListener('DOMContentLoaded', addElementsToPage);
    }
})();