atcoder-virtual-remaining-time-display

バーチャルコンテストの残り時間を表示します。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         atcoder-virtual-remaining-time-display
// @namespace    https://github.com/ilplrr
// @version      1.0
// @description  バーチャルコンテストの残り時間を表示します。
// @author       ilplrr
// @license      MIT
// @match        https://atcoder.jp/contests/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if (getServerTime() < endTime) return;

    const createTimer = (data) => {
        if (!data.isVirtual) return;
        if (!(data.virtualStartTime && data.virtualEndTime)) return;

        const timer = document.createElement('div');
        timer.id = 'virtual-timer';
        timer.style = `
            position: fixed;
            right: 170px;
            bottom: 0px;
            width: 160px;
            height: 80px;
            margin: 0;
            padding: 20px 0;
            background-image: url("//img.atcoder.jp/assets/contest/digitalclock.png");
            text-align: center;
            line-height: 20px;
            font-size: 15px;
            cursor: pointer;
            z-index: 50;
        `;
        document.body.appendChild(timer);

        let intervalId = null;
        const update = () => {
            const serverTime = getServerTime();
            let s = '';
            if (serverTime.isAfter(data.virtualEndTime)) {
                s += 'バーチャル参加終了<br>'
                s += `<span style="color: #c00;">残り:${durationFormat(0)}</span>`;
                if (intervalId) {
                    clearInterval(intervalId);
                    intervalId = null;
                }
            } else if (serverTime.isAfter(data.virtualStartTime)) {
                s += 'バーチャル参加中<br>'
                s += `残り:${durationFormat(data.virtualEndTime.diff(serverTime))}`;
            } else {
                s += 'バーチャル参加まで<br>'
                s += `残り:${durationFormat(data.virtualStartTime.diff(serverTime))}`;
            }
            timer.innerHTML = s;
        };
        intervalId = setInterval(update, 100);
    };

    const virtualStandingURL = `${location.origin}/contests/${contestScreenName}/standings/virtual`;
    fetch(virtualStandingURL).then((response) => {
        return response.text();
    }).then((text) => {
        const doc = new DOMParser().parseFromString(text, 'text/html');
        const data = {};
        doc.querySelectorAll('script').forEach((e) => {
            const s = e.innerHTML;
            const m = s.match(/(isVirtual|virtual(Start|End)Time)\s*=\s*[^\n]*/g);
            if (m) {
                m.forEach((s) => eval('data.' + s));
            }
        });
        createTimer(data);
    });
})();