AtCoderDifficultyDisplay

display a difficulty of AtCoder Problems.

目前為 2020-03-02 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AtCoderDifficultyDisplay
// @namespace    https://github.com/hotarunx
// @version      0.1
// @description  display a difficulty of AtCoder Problems.
// @author       hotarunx
// @match        https://atcoder.jp/contests/*/tasks/*
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @connect      https://kenkoooo.com/atcoder/resources/*
//
// Copyright(c) 2020 hotarunx
// This software is released under the MIT License, see LICENSE.
//
// ==/UserScript==

(function () {
    // URL of Estimated difficulties of the problems
    const url = "https://kenkoooo.com/atcoder/resources/problem-models.json";

    // get id
    const path = location.pathname.split("/");
    const id = path[path.length - 1];

    // get Element of Problem Status
    let element_status
    const main_container = document.getElementById('main-container');
    const elements_p = main_container.getElementsByTagName("p");
    for (let i = 0; i < elements_p.length; i++) {
        const element = elements_p[i];
        if (element.textContent.match("メモリ制限:") || element.textContent.match("Memory Limit:")) {
            element_status = element;
            break
        }
    }

    // fetch Information API
    this.fetch(url)
        .then(function (data) {
            return data.json();
        })
        .then(function (json) {
            // search problem
            const problem = json[id];

            // if problem exist in json
            if (problem != null) {
                const difficulty = problem.difficulty;

                // if difficulty is exist
                if (difficulty != null) {
                    element_status.textContent += " / ";

                    // add difficulty value
                    let add_text = "Difficulty: ";
                    if (problem.is_experimental) add_text += "🧪";
                    add_text += difficulty.toFixed();

                    // colorize text
                    let color = '#FFFFFF'; // white
                    if (difficulty < 400) color = '#808080'; // gray
                    else if (difficulty < 800) color = '#804000'; // brown
                    else if (difficulty < 1200) color = '#008000'; // green
                    else if (difficulty < 1600) color = '#00C0C0'; // cyan
                    else if (difficulty < 2000) color = '#0000FF'; // blue
                    else if (difficulty < 2400) color = '#C0C000'; // yellow
                    else if (difficulty < 2800) color = '#FF8000'; // orange
                    else if (difficulty < 3200) color = '#FF0000'; // red
                    else if (difficulty < 3600) color = '#E4E4E4'; // silver
                    else /*                  */ color = '#FFD325'; // gold

                    const add_span = "<span style='color: " + color + ";'>" + add_text + "</span>";

                    element_status.innerHTML += add_span;
                }
            }
        })
})();