油猴防沉迷脚本

偷自https://linux.do/t/topic/41130

// ==UserScript==
// @name         XJTU MEN 防沉迷系统
// @namespace    http://tampermonkey.net/
// @license MIT
// @version      0.2
// @description  总浏览 XJTU MEN 超过一定时间,弹出提示,提醒休息
// @author       caoayu
// @match        *://xjtu.app/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let startTime = null;
    let endTime = null;
    const storageKey = 'websiteTimeTracker';

    function startTimer() {
        startTime = new Date().getTime() / 1000;
        let currentTotal = localStorage.getItem(storageKey) || 0;
        if (currentTotal > 1 * 60) {
            const result = confirm('啊喂,你已沉迷超过 1 分钟,休息一下吧?');
            // 别看了,休息一下吧,老弟
            if (result) {
                window.close();
            } else {
                window.close();
            }
        }
    }

    function stopTimer() {
        endTime = new Date().getTime() / 1000;
        const duration = (endTime - startTime); // Convert to seconds
        let currentTotal = localStorage.getItem(storageKey) || 0;
        currentTotal = parseFloat(currentTotal) + duration;
        localStorage.setItem(storageKey, currentTotal);

    }

    window.addEventListener('load', startTimer);
    window.addEventListener('beforeunload', stopTimer);
})();

4 Likes

交大门这内容更新频率感觉不是很有这个必要啊 :joy_cat:

2 Likes