Atcoder Title Copy

Atcoderの問題のページに問題タイトルをクリップボードにコピーするボタンを追加します

Stan na 17-10-2021. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Atcoder Title Copy
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Atcoderの問題のページに問題タイトルをクリップボードにコピーするボタンを追加します
//               拡張子を問題タイトルの最後につけることも可能です
// @author       sin471
// @match        https://atcoder.jp/contests/*/tasks/*
// @grant        none
// @license      MIT
// 

// ==/UserScript==

/* ユーザー設定項目 */
    /*
    A - AtCoder → A - AtCoder.js
    のように、デフォルトで拡張子をつける場合は、
    ダブルクオテーションの中に拡張子を入力
    */
const extension = "";//ex: ".js"

    /*
    Copied!を表示する時間を変更
    デフォルトでは1500ms(1.5秒)
    */
const sleepTime = 1500
/* 設定項目終わり */


function copy() {
    var title = document.getElementsByClassName("h2")[0];
    //改行文字を削除
    var text = title.firstChild.textContent.trim();
    //ユーザーの設定した拡張子を追加
    text += extension;
    navigator.clipboard.writeText(text);
};

// ミリ秒間待機する
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function notifyCopied(a) {
    a = this.name;
    a.textContent = "Copied!";
    await sleep(1500);
    a.textContent = "Copy";
};

function create_button() {
    var parent = document.getElementsByClassName("h2");
    var a = document.createElement("a");
    a.textContent = "Copy";
    //AtcoderのCopyボタンと同じCSSを適用
    a.setAttribute("class", "btn btn-default btn-sm");
    parent[0].appendChild(a);
    a.addEventListener('click', copy, false);
    //ボタン内のテキスト内容を一定時間"Copied!"に書き換え
    a.addEventListener('click', { name: a, handleEvent: notifyCopied });
};
create_button();