Atcoder Title Copy

Atcoderの問題ページにタイトルをコピーするボタンを追加する

As of 2021-11-17. See the latest version.

  1. // ==UserScript==
  2. // @name Atcoder Title Copy
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Atcoderの問題ページにタイトルをコピーするボタンを追加する
  6. // @author sin471
  7. // @match https://atcoder.jp/contests/*/tasks/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. /* ユーザー設定項目 */
  13. /*
  14. 先頭のアルファベットとハイフンの除外
  15. (A - AtCoder → AtCoder)
  16. 除外する場合はtrue
  17. 除かない場合はfalse
  18. */
  19. const is_remove_head = false //true or false
  20.  
  21. /*
  22. 拡張子の追加
  23. 追加する場合はダブルクオテーションの中に拡張子を入力
  24. (A - AtCoder → A - AtCoder.hoge)
  25. */
  26. const extension = "";//ex: .hoge
  27.  
  28. /*
  29. Copied!の表示時間
  30. デフォルトでは1500ms(1.5秒)
  31. */
  32. const sleepTime = 1500
  33. /* 設定項目終了 */
  34.  
  35. function process_text(text) {
  36. //ユーザーが設定した拡張子の追加
  37. text += extension
  38. //先頭のアルファベットとハイフンを除去するか
  39. if (is_remove_head) {
  40. text = text.slice(4)
  41. }
  42. return text
  43. }
  44.  
  45. function copy() {
  46. var title = document.getElementsByClassName("h2")[0];
  47. //改行文字を削除
  48. var text = title.firstChild.textContent.trim();
  49. //ユーザー設定に合わせて加工
  50. text = process_text(text)
  51.  
  52. navigator.clipboard.writeText(text);
  53. };
  54.  
  55. // ミリ秒間待機する
  56. function sleep(ms) {
  57. return new Promise(resolve => setTimeout(resolve, ms));
  58. }
  59.  
  60. async function notifyCopied(a) {
  61. a = this.name;
  62. a.textContent = "Copied!";
  63. await sleep(1500);
  64. a.textContent = "Copy";
  65. };
  66.  
  67. function create_button() {
  68. var parent = document.getElementsByClassName("h2");
  69. var a = document.createElement("a");
  70. a.textContent = "Copy";
  71. //AtcoderのCopyボタンと同じCSSを適用
  72. a.setAttribute("class", "btn btn-default btn-sm");
  73. parent[0].appendChild(a);
  74. a.addEventListener('click', copy, false);
  75. //ボタン内のテキスト内容を一定時間"Copied!"に書き換え
  76. a.addEventListener('click', { name: a, handleEvent: notifyCopied });
  77. };
  78. create_button();