AtCoder Formatter

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

Verze ze dne 13. 11. 2022. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name           AtCoder Formatter
// @name:en        AtCoder Formatter
// @namespace
// @version        1.0
// @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" id="${id}-fmt-cpp">
        フォーマット (C++)
    </button>
    <button type="button" class="btn ${buttonClass} btn-sm" id="${id}-fmt-py">
        フォーマット (Python)
    </button>
</div>`
        )
        document.getElementById(`${id}-fmt-cpp`)
            .addEventListener('click', async (e) => await formatCode(e, pre, id, 'cpp'))
        document.getElementById(`${id}-fmt-py`)
            .addEventListener('click', async (e) => await formatCode(e, pre, id, 'py'))
    }
})()