AtCoder copy button adder

AtCoderの問題文中の文字列にの横にコピーボタンを置きます。

Verze ze dne 21. 01. 2024. 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 copy button adder
// @namespace    https://github.com/H20-DHMO
// @version      1.0
// @description  AtCoderの問題文中の文字列にの横にコピーボタンを置きます。
// @author       H20_dhmo
// @license      MIT
// @match        https://atcoder.jp/contests/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=atcoder.jp
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // copy button
    function copyButton() {
        window.getSelection().removeAllRanges();
        try {
            const targetId = $(this).data('target');
            const text = $('#' + targetId).text();
            navigator.clipboard.writeText(text).then(() => {
                $(this).tooltip('show');
                setTimeout(() => { $(this).tooltip('hide'); }, 800);
            });
        } catch (err) {
            console.log(err);
        }
        window.getSelection().removeAllRanges();
    }

    // UUIDを生成する関数
    function generateUUID() {
        let dt = new Date().getTime();
        const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            const r = (dt + Math.random()*16)%16 | 0;
            dt = Math.floor(dt/16);
            return (c === 'x' ? r : (r&0x3|0x8)).toString(16);
        });
        return uuid;
    }

    function addCopyButton(element) {
        // コピー用のボタン(span要素)を作成
        const uuid = generateUUID();
        element.setAttribute('id', uuid);

        const copyBtn = $(`<span class="btn btn-default btn-sm btn-copy ml-1" tabindex="0" data-toggle="tooltip" data-trigger="manual" title="Copied!" data-target="${uuid}">Copy</span>`);
        $(element).after(copyBtn);
        copyBtn.click(copyButton);
    }

    // 問題内の<code>要素を取得
    const codes = document.querySelectorAll('#task-statement .part code');
    codes.forEach(function(elm) {
        addCopyButton(elm);
    });

})();