AtCoder Formatter

AtCoder の解説コードなどをフォーマットできるようにします.

目前為 2022-11-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           AtCoder Formatter
// @name:en        AtCoder Formatter
// @namespace
// @version        1.2
// @description    AtCoder の解説コードなどをフォーマットできるようにします.
// @description:en Add formatting buttons to source codes on AtCoder.
// @author         kichi2004
// @match          https://atcoder.jp/contests/*
// @grant          none
// @namespace      kichi2004.jp
// @license        MIT
// ==/UserScript==

'use strict';

(async function () {

    const formatCode = async (event, pre, id, lang) => {
        const removeModal = () => {
            $(`#modal-${id}-format-warning`).modal('hide')
        }

        const formatInner = async (modal = false) => {
            if (modal) {
                removeModal()
            }

            event.target.disabled = true
            const data = document.getElementById(id)
            const res = await fetch(`https://formatter.api.kichi2004.jp/format?lang=${lang}`, {
                body: data.innerText,
                method: 'POST'
            })
            event.target.disabled = false
            if (!res.ok) {
                alert('Formatting Request Failed!')
                return
            }
            const json = await res.json()
            if (json['status'] === 'error') {
                alert('Formatting Error!\n' + json['error'])
                return
            }
            const result = json['result']
            const nextPre = document.createElement('pre')
            nextPre.textContent = result
            nextPre.classList.add('prettyprint', `lang-${lang}`, 'linenums')
            pre.before(nextPre)
            pre.remove()

            data.textContent = result
            PR.prettyPrint()
        }

        const finished = endTime.toDate() < new Date()
        if (finished) {
            await formatInner()
            return
        }

        document.body.insertAdjacentHTML('afterbegin', `
<div id="modal-${id}-format-warning" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">フォーマットの注意</h4>
            </div>
            <div class="modal-body">
                <p>このコンテストはまだ終了していません。</p>
                <p>フォーマットを行うとソースコードが外部に送信され、スクリプトの作成者が閲覧可能な状態になります。</p>
                <p>まだ公開されていないコンテストではフォーマットを行わないでください。</p>
            </div>
            <div class="modal-footer">
                <button class="btn btn-warning" id="${id}-force-format">フォーマットする</button>
                <button class="btn btn-success" id="${id}-cancel">キャンセル</button>
            </div>
        </div>
    </div>
</div>
        `)

        $(`#modal-${id}-format-warning`).modal('show')
        document.getElementById(`${id}-force-format`).addEventListener('click', async () => await formatInner(true))
        document.getElementById(`${id}-cancel`).addEventListener('click', removeModal)
    }

    for (const pre of document.getElementsByClassName('prettyprint')) {
        const next = pre.nextElementSibling
        if (next.className !== 'source-code-for-copy') continue
        const id = next.id

        let adding = pre.previousElementSibling
        while (adding.className === 'div-btn-copy')
            adding = adding.previousElementSibling

        const buttonClass = endTime.toDate() < new Date() ? 'btn-info' : 'btn-danger'

        adding.insertAdjacentHTML(
            'afterend',
            `
<div class="btn-group" role="group">
    <button type="button" class="btn ${buttonClass} btn-sm" >
        C++
    </button>
    <button type="button" class="btn ${buttonClass} btn-sm" id="${id}-fmt-cpp">
        C++
    </button>
    <button type="button" class="btn ${buttonClass} btn-sm" id="${id}-fmt-py">
        Python
    </button>
    <button type="button" class="btn ${buttonClass} btn-sm" id="${id}-fmt-cs">
        C#
    </button>
</div>`
        )
        for (const lang of ['cpp', 'py', 'cs']) {
            document.getElementById(`${id}-fmt-${lang}`)
                .addEventListener('click', async (e) => await formatCode(e, pre, id, lang))
        }
    }
})()