Greasy Fork is available in English.

AtCoderDifficultyDisplay

display a difficulty of AtCoder Problems.

Устаревшая версия за 18.04.2020. Перейдите к последней версии.

  1. // ==UserScript==
  2. // @name AtCoderDifficultyDisplay
  3. // @namespace https://github.com/hotarunx
  4. // @version 0.2
  5. // @description display a difficulty of AtCoder Problems.
  6. // @description:ja AtCoder ProblemsのDifficultyを表示します。
  7. // @author hotarunx
  8. // @match https://atcoder.jp/contests/*/tasks/*
  9. // @grant none
  10. // @connect https://kenkoooo.com/atcoder/resources/*
  11. // @license MIT
  12. //
  13. // Copyright(c) 2020 hotarunx
  14. // This software is released under the MIT License, see LICENSE or https://github.com/hotarunx/AtCoderMyExtensions/blob/master/LICENSE.
  15. //
  16. // ==/UserScript==
  17.  
  18. function getElementOfProblemStatus() {
  19. let element_status;
  20.  
  21. const main_container = document.getElementById('main-container');
  22. const elements_p = main_container.getElementsByTagName("p");
  23.  
  24. for (let i = 0; i < elements_p.length; i++) {
  25. const element = elements_p[i];
  26. if (element.textContent.match("メモリ制限:") || element.textContent.match("Memory Limit:")) {
  27. element_status = element;
  28. break
  29. }
  30. }
  31.  
  32. return element_status;
  33. }
  34.  
  35. // return rating color
  36. function colorRating(rating) {
  37. let color = '#FFFFFF'; // white
  38. if /**/ (rating < 0400) color = '#808080'; // gray
  39. else if (rating < 0800) color = '#804000'; // brown
  40. else if (rating < 1200) color = '#008000'; // green
  41. else if (rating < 1600) color = '#00C0C0'; // cyan
  42. else if (rating < 2000) color = '#0000FF'; // blue
  43. else if (rating < 2400) color = '#C0C000'; // yellow
  44. else if (rating < 2800) color = '#FF8000'; // orange
  45. else if (rating < 3200) color = '#FF0000'; // red
  46. else if (rating < 3600) color = '#E4E4E4'; // silver
  47. else /* */ color = '#FFD325'; // gold
  48.  
  49. return color;
  50. }
  51.  
  52. // correct rating under 400
  53. // see https://qiita.com/anqooqie/items/92005e337a0d2569bdbd#%E6%80%A7%E8%B3%AA4-%E5%88%9D%E5%BF%83%E8%80%85%E3%81%B8%E3%81%AE%E6%85%88%E6%82%B2
  54. function correctLowerRating(rating) {
  55. if (rating >= 400) return rating;
  56.  
  57. do {
  58. rating = 400 / Math.exp((400 - rating) / 400);
  59.  
  60. } while (rating < 0);
  61.  
  62. return rating;
  63. }
  64.  
  65. function generateDifficultyText(difficulty, is_experimental) {
  66. let text = " / ";
  67.  
  68. difficulty = correctLowerRating(difficulty)
  69.  
  70. // add difficulty value
  71. let colored_text = "Difficulty: ";
  72. if (is_experimental) colored_text += "🧪";
  73. colored_text += difficulty.toFixed();
  74.  
  75. // color difficulty value
  76. const color = colorRating(difficulty);
  77.  
  78. text += "<span style='color: " + color + ";'>" + colored_text + "</span>";
  79.  
  80. return text;
  81. }
  82.  
  83. function addDifficultyText(jsonData) {
  84. let text = "";
  85.  
  86. // get id
  87. const path = location.pathname.split("/");
  88. const id = path[path.length - 1];
  89.  
  90. // get Element of Problem Status
  91. let status = getElementOfProblemStatus();
  92.  
  93. const problem = jsonData[id];
  94. // if problem exist in json
  95. if (problem != null && problem.difficulty != null) {
  96. text += generateDifficultyText(problem.difficulty, problem.is_experimental);
  97. }
  98.  
  99. status.insertAdjacentHTML('beforeend', text);
  100. }
  101.  
  102. (function () {
  103. // URL of Estimated difficulties of the problems
  104. const URL = "https://kenkoooo.com/atcoder/resources/problem-models.json";
  105.  
  106. // fetch Information API
  107. fetch(URL)
  108. .then((response) => response.json())
  109. .then((jsonData) => {
  110. addDifficultyText(jsonData);
  111. });
  112.  
  113. })();