洛谷个签“最后在线时间”

· · 科技·工程

经常看见有些人的个签有一个“最后在线时间” 像我一样

但是你们不知道应该怎么弄,今天我来教你们。

1.在浏览器下载一个篡改猴插件,不管用什么渠道下载的,只要可以正常使用就行了。

2.点击一下篡改猴图标,点击添加新脚本,将下面这段代码复制粘贴进去。

// ==UserScript==
// @name         洛谷动态个签
// @namespace    https://greasyfork.org/zh-CN/users/1338222-lzm0107
// @version      2.2.0
// @license      GPLv3
// @description  最后在线时间
// @author       lzm0107
// @run-at       document-end
// @match        https://www.luogu.com/*
// @match        https://www.luogu.com.cn/*
// @icon         https://www.luogu.com.cn/favicon.ico
// @grant        none
// @downloadURL https://update.greasyfork.org/scripts/501498/%E6%B4%9B%E8%B0%B7%E5%8A%A8%E6%80%81%E4%B8%AA%E7%AD%BE.user.js
// @updateURL https://update.greasyfork.org/scripts/501498/%E6%B4%9B%E8%B0%B7%E5%8A%A8%E6%80%81%E4%B8%AA%E7%AD%BE.meta.js
// ==/UserScript==

(function() {
    'use strict';

    const text = '最后在线时间: {date} {time}'; // 个签模板,使用 {date}表示日期,{time} 表示时间,可添加数字选择格式。
    // 以2017年4月5日9时40分为例
    // {date}: 2017/4/5
    // {date1}: 2017/4/5
    // {date2}: 2017-4-5
    // {date3}: 2017.4.5
    // {date4}: 2017/04/05
    // {date5}: 2017-04-05
    // {date6}: 2017.04.05
    // {date7}: 17/4/5
    // {date8}: 17-4-5
    // {date9}: 17.4.5
    // {date10}: 17/04/05

    // {time}: 9:40
    // {time1}: 9:40
    // {time2}: 09:40
    // {time3}: 上午9:40
    // {time4}: 上午09:40

    const interval = 60; // 更新个签的时间间隔,单位为秒。
    let token, URLHostName = window.location.hostname;

    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 updateSlogan(){
        let date = new Date();
        let currentText = text;
        currentText = currentText.replace('{date}', getDate(date));
        currentText = currentText.replace('{time}', getTime(date));
        for(let i = 1; i <= 10; i ++ ){
            currentText = currentText.replace('{date' + i.toString() + '}', getDate(date, i));
        }
        for(let i = 1; i <= 4; i ++ ){
            currentText = currentText.replace('{time' + i.toString() + '}', getTime(date, i));
        }
        let data = {
            slogan: currentText
        };
        let xhr = new XMLHttpRequest();
        xhr.open('POST', `https://${URLHostName}/api/user/updateSlogan`, true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.setRequestHeader('X-CSRF-TOKEN', token);
        xhr.send(JSON.stringify(data));
    }

    token = document.querySelector('meta[name=csrf-token]').content;
    updateSlogan();
    setInterval(updateSlogan, interval * 1000);
})();

想要在个签添加其他内容的,可以在 20 行修改双引号内的文字。