atcoder_collect_all_examples

入出力例をまとめた項目を生成

  1. // ==UserScript==
  2. // @name atcoder_collect_all_examples
  3. // @namespace https://github.com/Haar-you
  4. // @version 1.1.0
  5. // @description 入出力例をまとめた項目を生成
  6. // @author Haar-you
  7. // @match https://atcoder.jp/contests/*/tasks/*
  8. // @grant none
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js
  10. // ==/UserScript==
  11.  
  12. $(function(){
  13. 'use strict';
  14. this.$ = this.jQuery = jQuery.noConflict(true);
  15.  
  16. let examples_input = [];
  17. let examples_output = [];
  18.  
  19. const navbar = $("#contest-nav-tabs");
  20. navbar.append(
  21. $("<ul></ul>", {"class": "nav nav-tabs"}).append(
  22. $("<li></li>").append(
  23. $("<button></button>", {
  24. text: "Examples",
  25. "class": "btn btn-link",
  26. on: {
  27. click: function(){
  28. const win = window.open("", "_blank", "width=800, height=600");
  29. constructWindow(win, examples_input, examples_output);
  30. }
  31. }
  32. })
  33. )
  34. )
  35. );
  36.  
  37.  
  38. getExamples(examples_input, examples_output);
  39.  
  40. createExampleItem(examples_input, examples_output);
  41. });
  42.  
  43. function createExampleItem(examples_input, examples_output){
  44. const pre_input =
  45. $("<pre></pre>", {
  46. style: "margin: 5px",
  47. text: examples_input.join("\n")
  48. });
  49.  
  50. const pre_output =
  51. $("<pre></pre>", {
  52. style: "margin: 5px",
  53. text: examples_output.join("\n")
  54. });
  55.  
  56. const part_iostyle = $($("#task-statement .io-style")[0]);
  57.  
  58. part_iostyle.after(
  59. $("<div></div>", {
  60. style: "background-color: #dd9999;",
  61. "class": "part"
  62. }).append(
  63. $("<section></section>").append(
  64. $("<h3></h3>", {text: "全入出力例 "}).append(
  65. $("<span></span>", {
  66. "class": "btn btn-default btn-sm",
  67. text: "Copy input",
  68. "data-toggle": "tooltip",
  69. "data-trigger": "manual",
  70. "data-title": "Copied!",
  71. on:{
  72. click: function(){
  73. copyExample2.call(this, window, pre_input.get(0));
  74. }
  75. }
  76. }),
  77. $("<span></span>", {
  78. "class": "btn btn-default btn-sm",
  79. text: "Copy output",
  80. "data-toggle": "tooltip",
  81. "data-trigger": "manual",
  82. "data-title": "Copied!",
  83. on:{
  84. click: function(){
  85. copyExample2.call(this, window, pre_output.get(0));
  86. }
  87. }
  88. })
  89. ),
  90. pre_input,
  91. pre_output
  92. )
  93. )
  94. );
  95. }
  96.  
  97.  
  98. function constructWindow(win, examples_input, examples_output){
  99. const style_pre = "display: block; margin: 0 0 10px; font-size: 13px; line-height: 1.42857143; word-break: break-all; word-wrap: break-word; color: #333; background-color: #f5f5f5; border: 1px solid #ccc; border-radius: 3px;";
  100.  
  101. const style_copy_button = "";
  102.  
  103. const pre_input =
  104. $("<pre></pre>", {
  105. style: style_pre,
  106. text: examples_input.join("\n")
  107. });
  108.  
  109. const pre_output =
  110. $("<pre></pre>", {
  111. style: style_pre,
  112. text: examples_output.join("\n")
  113. });
  114.  
  115. pre_input.css({
  116. "width": "100%",
  117. "white-space": "pre-wrap"
  118. });
  119.  
  120. pre_output.css({
  121. "width": "100%",
  122. "white-space": "pre-wrap"
  123. });
  124.  
  125. const problem_title = win.opener.document.title;
  126. win.document.title = problem_title;
  127. $(win.document.body).append(
  128. $("<div></div>", {
  129. "class": "part"
  130. }).append(
  131. $("<button></button", {
  132. text: "close (ESC)",
  133. on: {
  134. click: function(){
  135. win.close();
  136. }
  137. }
  138. }),
  139. $("<section></section>").append(
  140. $("<h3></h3>", {text: problem_title}),
  141. $("<div></div>", {
  142. style: "width: 100%;"
  143. }).append(
  144. $("<div></div>", {style: "display: inline-block; width: 45%;"}).append(
  145. $("<button></button>", {
  146. text: "Copy input",
  147. style: style_copy_button,
  148. on: {
  149. click: function(){
  150. copyExample(win, pre_input.get(0));
  151. }
  152. }
  153. }),
  154. pre_input
  155. ),
  156. $("<div></div>", {style: "display: inline-block; width: 45%; float: right;"}).append(
  157. $("<button></button>", {
  158. text: "Copy output",
  159. style: style_copy_button,
  160. on: {
  161. click: function(){
  162. copyExample(win, pre_output.get(0));
  163. }
  164. }
  165. }),
  166. pre_output
  167. )
  168. )
  169. )
  170. )
  171. );
  172.  
  173. $(win).keydown((e) => {
  174. if(e.keyCode == 27){
  175. win.close();
  176. }
  177. });
  178. }
  179.  
  180. function copyExample(win, elem){
  181. win.getSelection().removeAllRanges();
  182. const range = win.document.createRange();
  183. range.selectNode(elem);
  184. win.getSelection().addRange(range);
  185. win.document.execCommand('copy');
  186. win.getSelection().removeAllRanges();
  187. }
  188.  
  189. function copyExample2(win, elem){
  190. win.getSelection().removeAllRanges();
  191. const range = win.document.createRange();
  192. range.selectNode(elem);
  193. win.getSelection().addRange(range);
  194. win.document.execCommand('copy');
  195.  
  196. $(this).tooltip("show");
  197. var _this = this;
  198. setTimeout(function() {
  199. $(_this).tooltip('hide');
  200. }, 800);
  201. win.getSelection().removeAllRanges();
  202. }
  203.  
  204.  
  205. function getExamples(examples_input, examples_output){
  206. const part_example = $("#task-statement .part");
  207.  
  208. part_example
  209. .filter((i,elem) => {
  210. const s = $($(elem).find("h3")[0]).text();
  211. return /入力例/.test(s);
  212. })
  213. .each((i,elem) => {examples_input.push($(elem).find("pre")[0].innerText);});
  214.  
  215. part_example
  216. .filter(function(i,elem){
  217. const s = $($(elem).find("h3")[0]).text();
  218. return /出力例/.test(s);
  219. })
  220. .each((i,elem) => {examples_output.push($(elem).find("pre")[0].innerText);});
  221. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址