Greasy Fork

Greasy Fork is available in English.

洛谷动态个签

在个签中添加最后活跃时间

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         洛谷动态个签
// @namespace    http://greasyfork.icu/zh-CN/users/1338222-lzm0107
// @version      3.0.0
// @license      GPLv3
// @description  在个签中添加最后活跃时间
// @author       lzm0107
// @run-at       document-idle
// @match        https://www.luogu.com/*
// @match        https://www.luogu.com.cn/*
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    let INTERVAL = 1000 * 60;
    let REPLACE, SLOGAN_TEXT, TOKEN, USER_NAME;

    function checkSettings(){
        if(GM_getValue('replaceSlogan') === undefined){
            let replaceSlogan = confirm('覆盖原有个签,而不是在原个签末尾添加?');
            GM_setValue('replaceSlogan', replaceSlogan);
            let sloganText = prompt('请输入个签内容,具体参考 greasyfork 上的使用说明:', replaceSlogan ? '最后在线时间: {date1} {time1}' : ' | 最后在线时间: {date1} {time1}');
            while(sloganText === null){
                sloganText = prompt('请输入一个有效的字符串。', replaceSlogan ? '最后在线时间: {date1} {time1}' : ' | 最后在线时间: {date1} {time1}');
            }
            GM_setValue('sloganText', sloganText);
        }
    }

    function getDate(date, format = 1){
        let year = date.getFullYear().toString();
        let year2 = year.slice(-2);
        let month = (date.getMonth() + 1).toString();
        let month0 = ('0' + month).slice(-2);
        let day = date.getDate().toString();
        let day0 = ('0' + (date.getDate()).toString()).slice(-2);
        switch(format){
            case 1: return `${year}/${month}/${day}`;
            case 2: return `${year}-${month}-${day}`;
            case 3: return `${year}.${month}.${day}`;
            case 4: return `${year}/${month0}/${day0}`;
            case 5: return `${year}-${month0}-${day0}`;
            case 6: return `${year}.${month0}.${day0}`;
            case 7: return `${year2}/${month}/${day}`;
            case 8: return `${year2}-${month}-${day}`;
            case 9: return `${year2}.${month}.${day}`;
            case 10: return `${year2}/${month0}/${day0}`;
        }
    }
    
    function getTime(date, format = 1){
        let hours = date.getHours().toString();
        let hours0 = ('0' + hours).slice(-2);
        let am_pm = (date.getHours() >= 12 ? '下午' : '上午');
        let hours12 = ((date.getHours() % 12 + 11) % 12 + 1).toString();
        let hours12_0 = ('0' + hours12).slice(-2);
        let minutes = ('0' + (date.getMinutes()).toString()).slice(-2);
        switch(format){
            case 1: return `${hours}:${minutes}`;
            case 2: return `${hours0}:${minutes}`;
            case 3: return `${am_pm}${hours12}:${minutes}`;
            case 4: return `${am_pm}${hours12_0}:${minutes}`;
        }
    }

    function sendSlogan(slogan){
        fetch('/api/user/updateSlogan', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-Token': TOKEN
            },
            body: JSON.stringify({
                slogan: slogan
            })
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }

    function updateSlogan(){
        let date = new Date();
        let slogan;
        slogan = SLOGAN_TEXT;
        for(let i = 1; i <= 10; i ++ ){
            slogan = slogan.replace('{date' + i.toString() + '}', getDate(date, i));
        }
        for(let i = 1; i <= 4; i ++ ){
            slogan = slogan.replace('{time' + i.toString() + '}', getTime(date, i));
        }
        if(!REPLACE){
            fetch(`/api/user/search?keyword=${USER_NAME}`)
                .then(response => {
                    if(!response.ok){
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    let old = data.users[0].slogan;
                    if(old.indexOf('\u200B') != -1){
                        old = old.substring(0, old.indexOf('\u200B'));
                    }
                    slogan = old + '\u200B' + slogan;
                    sendSlogan(slogan);
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
        else{
            sendSlogan(slogan);
        }
    }

    setTimeout(function(){
        checkSettings();
        REPLACE = GM_getValue('replaceSlogan');
        SLOGAN_TEXT = GM_getValue('sloganText');
        TOKEN = document.querySelector('meta[name="csrf-token"]').content;
        USER_NAME = document.querySelector('img[class=avatar]').alt;
        updateSlogan();
        setInterval(updateSlogan, INTERVAL);
    }, 3000);
})();