Atcoder Nickname

给 Atcoder 用户添加备注

Mint 2024.04.22.. Lásd a legutóbbi verzió

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Atcoder Nickname
// @namespace    https://greasyfork.org/zh-CN/users/1223216-znpdco
// @license      MIT
// @version      2024-04-19
// @description  给 Atcoder 用户添加备注
// @author       ZnPdCo
// @match        https://atcoder.jp/*
// @icon         https://aowuucdn.oss-accelerate.aliyuncs.com/atcoder.png
// @grant        unsafeWindow
// @grant        GM_log
// @grant        GM_openInTab
// @grant        GM_deleteValue
// @grant        GM_xmlhttpRequest
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_setClipboard
// @require      https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
// ==/UserScript==

(function() {
    // 引入sweet alert
    const script = document.createElement('script');
    script.src = 'https://cdn.bootcdn.net/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.js';
    document.head.appendChild(script);
    const css = document.createElement('link');
    css.href = 'https://cdn.bootcdn.net/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.css';
    css.rel="stylesheet";
    document.head.appendChild(css);

    if(GM_getValue("namedata") == undefined) {
        GM_setValue("namedata", "{}");
    }
    var namedata = JSON.parse(GM_getValue("namedata"));
    /*{
        "Devin2022": "黎灏",
        "zy2022": "周祎",
        "Tangninghaha": "唐宁",
        "guanyongcheng": "关永丞",
        "emmoy": "徐浩轩",
        "cuiruilin2023": "崔瑞麟",
        "huangziyao2023": "黄子曜",
        "2026wangyiwen": "王逸文",
        "gmoj": "杨永淇",
        "liyuze": "李雨泽",
        "2021sjz": "宋金哲",
        "pengweixiang": "彭炜翔",
        "zpc_": "周沛岑",
        "lnw143": "胡金勇",
        "ZnPdCo": "黄景梵",
        "oierljb": "罗浚博",
        "OIerLzy": "凌梓亿",
        "eleylyx": "刘宇翔",
        "hjf2023": "胡杰峰",
        "2022_lwx": "林琬馨",
        "xiaofu15191": "付壹宁",
        "Aje453": "庞子诚",
        "chenghaojia": "程浩佳",
        "2022caiminghui": "蔡明辉",
        "ILMC1": "曾中文",
        "2022ljx": "廖健翔",
        "yangjiaxi": "杨佳熹",
        "medicinee": "周荣沛",
        "dengchengyu": "邓承羽",
        "LIUIR": "刘谨瑞",
        "kystar": "石启辰",
        "xglight": "李睿康",
        "zwx_": "郑文羲",
        "tanfuxuan": "谭富炫",
        "zsh2022": "钟尚恒",
        "phk": "彭浩坤",
        "wmd_125": "王铭栋",
        "2022hezixuan": "贺子萱",
        "mofengrui": "莫丰瑞",
        "CabbageK": "宋昊堃",
        "wuchenyu01": "吴辰煜",
        "wtc": "吴同春",
        "CHD": "程怀德",
        "chenyiqing2021": "陈艺清"
    }*/
    $(`<li style="cursor: pointer;"><a id="nickname_set"><span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 管理备注</a></li>`).insertBefore('#navbar-collapse .divider:eq(1)');
    $('#nickname_set').click(function(e) {
        e.preventDefault();
        swal({
            title: "批量设置备注",
            text: "备注 JSON(格式如 <code>{\"atcoder\": \"at\"}</code>):",
            type: "input",
            html: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            showCancelButton: true,
            closeOnConfirm: false,
            animation: "slide-from-top",
            inputPlaceholder: "{\"atcoder\": \"at\"}"
        }, function(json){
            if (json === false) return false;

            var is_json = true;
            try {
                var object = JSON.parse(json);
            } catch (error) {
                is_json = false;
                swal.showInputError("这好像不是一个合法的 JSON!");
            }
            if(!is_json) {
                return false;
            }

            GM_setValue("namedata", json);
            location.reload();
        });
        $(".form-control").val(GM_getValue("namedata"));
    });
    setInterval(function() {
        var ele = $('.username');
        for(let i = 0; i < ele.length; i ++) {
            if($('.username').eq(i).has('.nickname').length == 0) {
                var name = $('.username').eq(i).text().replace(' ', '');
                if(name in namedata) {
                    $('.username').eq(i).append(`<span class="nickname">(${namedata[name]})</span>`);
                    $(`.nickname`).css({'color': 'black',
                                        'opacity': '.15',
                                        'transition': 'all .5s'});
                    $(`.nickname`).unbind('hover').hover(function(e) { // 鼠标悬浮时触发
                        $(e.target).css('opacity', '1')
                    }, function(e) { // 鼠标离开时触发
                        $(e.target).css('opacity', '.15')
                    })
                }
            }
            if($('.username').eq(i).has('.nickname-edit').length == 0) {
                $('.username').eq(i).append(`<span> </span><span class="nickname-edit glyphicon glyphicon-edit" href=""></span>`);
                $(`.nickname-edit`).css({'color': 'black',
                                         'opacity': '.15',
                                         'transition': 'all .5s',
                                         'cursor': 'pointer'});
                $(`.nickname-edit`).unbind('hover').hover(function(e) { // 鼠标悬浮时触发
                    $(e.target).css('opacity', '1')
                }, function(e) { // 鼠标离开时触发
                    $(e.target).css('opacity', '.15')
                })
                $(`.nickname-edit`).unbind('click').click(function(e) { // 鼠标点击时触发
                    e.preventDefault();
                    var username = $(e.target).parent().find('span:eq(0)').text().replace(' ', '');
                    swal({
                        title: "设置 " + username + " 的备注",
                        text: "备注:",
                        type: "input",
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        html: true,
                        showCancelButton: true,
                        closeOnConfirm: false,
                        animation: "slide-from-top",
                        inputPlaceholder:  username + " 的备注",
                    }, function(nickname){
                        if (nickname === false) return false;
                        namedata[username] = nickname;
                        if(nickname == '') {
                            delete namedata[username]
                        }
                        GM_setValue("namedata", JSON.stringify(namedata));
                        location.reload();
                    });
                    if(username in namedata)
                        $(".form-control").val(namedata[username]);
                })
            }
        }
    }, 200);
})();