AtCoderDevotionScript

Write contestID and problem to the clipboard

As of 2020-11-15. See the latest version.

  1. // ==UserScript==
  2. // @name AtCoderDevotionScript
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Write contestID and problem to the clipboard
  6. // @author imomo
  7. // @include https://atcoder.jp/contests/*/tasks/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /*ユーザー設定項目*/
  12.  
  13. //クリップボードにコピーするものの設定 all以外:[コンテストID] / all:[スクリプト名] [コンテストID] [問題名(a,b...,f)]
  14. var copyTarget = "";
  15. //ショートカットに設定するキーを設定 デフォルトはQです(大文字で入力して下さい)
  16. var shotcutKey = 'Q';
  17. //コピーできたか通知するかどうか onの場合のみ通知を行います
  18. var notification = "on";
  19.  
  20. //(※必要な方のみ)シェルスクリプト、もしくはシェルスクリプトを呼び出すコマンドを設定
  21. var callScripts = "";
  22.  
  23. /*設定項目終わり*/
  24. var copystr;
  25. onkeydown = function(){
  26. if((event.ctrlKey || event.metaKey)&&event.keyCode==shotcutKey.charCodeAt()){
  27. //問題ページのURLを取得
  28. var contestUrl = location.href;
  29. //パス毎に分割
  30. var problemPass = contestUrl.split("/");
  31. //contestID及び問題種別を格納
  32. var contestID = problemPass[problemPass.length - 3];
  33. var problem =contestUrl.substr(-1);
  34. if(!isNaN(problem))problem = String.fromCharCode(96 + Number(problem));
  35.  
  36. if(copyTarget == "all")copystr = callScripts + " " + contestID + " " + problem;
  37. else copystr = contestID;
  38. // 空div 生成
  39. var tmp = document.createElement("div");
  40. // 選択用のタグ生成
  41. var pre = document.createElement('pre');
  42.  
  43. // 親要素のCSSで user-select: none だとコピーできないので書き換える
  44. pre.style.webkitUserSelect = 'auto';
  45. pre.style.userSelect = 'auto';
  46. tmp.appendChild(pre).textContent = copystr;
  47.  
  48. // 要素を画面外へ
  49. var s = tmp.style;
  50. s.position = 'fixed';
  51. s.right = '200%';
  52.  
  53. // body に追加
  54. document.body.appendChild(tmp);
  55. // 要素を選択
  56. document.getSelection().selectAllChildren(tmp);
  57.  
  58. // クリップボードにコピー
  59. document.execCommand("copy");
  60.  
  61. // 要素削除
  62. document.body.removeChild(tmp);
  63. //通知
  64. if(notification == "on"){
  65. if(copyTarget == "all")alert(callScripts + " " + contestID + " " + problem+" copied!!");
  66. else alert(contestID + " copied!!");
  67. }
  68. }
  69. }