急需一个插件

突然发现每天泡论坛时间太长了,今天不知不觉过了零点都没有发现!!!

有没有脚本可以记录每天泡了多长时间的论坛,或者实时显示在页面上

这个想法有点意思,mark一下

不对,好想是可以的,得翻翻username.json看有没有哦

看阅读时间?

遇事不决,上油猴。。

效果图:

可以自行设置是否显示秒数。可设置文字位置和颜色。

随便写的非常粗糙,原则就是,代码和人有一个能跑就行,当然欢迎佬友改进。

// ==UserScript==
// @name         Linux.do Timer
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  在 Linux.do 论坛网页右上角显示当日的累计总阅读时间
// @author       马克思
// @match        https://linux.do/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=linux.do
// ==/UserScript==

(function() {
    'use strict';

    let lastUpdateTime = Date.now();
    let inactivityTimer;
    const inactivityThreshold = 60000;
    function getCurrentDateAsString() {
        const now = new Date();
        return now.toISOString().split('T')[0]; // 获取格式为"YYYY-MM-DD"的日期字符串
    }
    localStorage.setItem('lastDate', getCurrentDateAsString());

    function updateTime() {
        if (document.visibilityState === 'visible') {
            const now = Date.now();
            const currentDate = getCurrentDateAsString();
            const lastDate = localStorage.getItem('lastDate');
            if (lastDate !== currentDate) {
                localStorage.setItem('accumulatedTime', '0');
                localStorage.setItem('lastDate', currentDate);
            } else {
                const accumulatedTime = parseInt(localStorage.getItem('accumulatedTime') || '0', 10) + (now - lastUpdateTime);
                localStorage.setItem('accumulatedTime', accumulatedTime.toString());
            }
            lastUpdateTime = now;
            //displayTime(accumulatedTime);
            displayTime(parseInt(localStorage.getItem('accumulatedTime'), 10));
        }
    }

    function displayTime(accumulatedTime) {
        const timeElement = document.getElementById('timeSpent') || createTimeDisplay();
        const seconds = Math.floor(accumulatedTime / 1000) % 60;
        const minutes = Math.floor(accumulatedTime / 60000) % 60;
        const hours = Math.floor(accumulatedTime / 3600000);
        const show_senconds = 1 // 默认显示时、分、秒,改成 0 就不显示秒了
        if (show_senconds){
                if (hours) {
                    timeElement.innerHTML = `今日阅读<span style="display:inline-block; min-width:24px; text-align:right;">${hours.toString().padStart(2, ' ')}</span>h<span style="display:inline-block; min-width:18px; text-align:right;">${minutes.toString().padStart(2, ' ')}</span>m<span style="display:inline-block; min-width:18px; text-align:right;">${seconds.toString().padStart(2, ' ')}</span>s`;
                } else if (minutes) {
                    timeElement.innerHTML = `今日阅读<span style="display:inline-block; min-width:24px; text-align:right;">${minutes.toString().padStart(2, ' ')}</span>m<span style="display:inline-block; min-width:18px; text-align:right;">${seconds.toString().padStart(2, ' ')}</span>s`;
                } else {
                    timeElement.innerHTML = `今日阅读<span style="display:inline-block; min-width:24px; text-align:right;">${seconds.toString().padStart(2, ' ')}</span>s`;
                }
        } else {
                if (hours) {
                    timeElement.innerHTML = `今日阅读<span style="display:inline-block; min-width:24px; text-align:right;">${hours.toString().padStart(2, ' ')}</span>h<span style="display:inline-block; min-width:18px; text-align:right;">${minutes.toString().padStart(2, ' ')}</span>m`;
                } else {
                    timeElement.innerHTML = `今日阅读<span style="display:inline-block; min-width:24px; text-align:right;">${minutes.toString().padStart(2, ' ')}</span>m`;
                }
        }
    }

    function createTimeDisplay() {
        const timeElement = document.createElement('div');
        timeElement.id = 'timeSpent';
        timeElement.style.position = 'fixed';
        timeElement.style.right = '25px'; // 这里可以调整位置
        timeElement.style.top = '70px'; // 这里可以调整位置
        timeElement.style.backgroundColor = 'rgba(0,0,0,0)'; // 这里可以调整时间块的背景色
        timeElement.style.color = 'rgba(0,0,0,0.2)'; // 这里调整文字颜色和透明度,默认是黑色,透明度20%
        timeElement.style.padding = '10px';
        timeElement.style.fontSize = '14px';
        document.body.appendChild(timeElement);
        return timeElement;
    }

    function resetInactivityTimer() {
        clearTimeout(inactivityTimer);
        inactivityTimer = setTimeout(() => {
            updateTime();
        }, inactivityThreshold);
    }

    if (document.visibilityState === 'visible') {
        lastUpdateTime = Date.now();
    }
    displayTime(parseInt(localStorage.getItem('accumulatedTime') || '0', 10));
    setInterval(updateTime, 1000);
    resetInactivityTimer();

    document.addEventListener('mousemove', resetInactivityTimer);
    document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'visible') {
            lastUpdateTime = Date.now();
            resetInactivityTimer();
        } else {
            updateTime();
        }
    });

    window.addEventListener('storage', (event) => {
        if (event.key === 'accumulatedTime') {
            displayTime(parseInt(event.newValue, 10));
        }
    });
})();




10 个赞

image
:+1:linuxdo 插件+1

油猴玩出了花

2 个赞

感觉你需要一个提醒 告诉你该休息了 而不是阅读了多久 :grinning:

厉害了,膜拜大佬 :face_holding_back_tears:

image
用上了用上了

1 个赞

有点东西

现在人均 JavaScript 了吗?不过有 oai 嘛

不睡觉的吗

2 个赞

image

image
happy :partying_face: :partying_face: :partying_face:

1 个赞