AtCoder Editorial for Typical90

AtCoder「競プロ典型 90 問」に解説タブを追加し、E869120さんがGitHubで公開されている問題の解説・想定ソースコードなどのリンクを表示します。

Tính đến 28-06-2021. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name AtCoder Editorial for Typical90
  3. // @namespace https://github.com/KATO-Hiro
  4. // @version 0.3.5
  5. // @description AtCoder「競プロ典型 90 問」に解説タブを追加し、E869120さんがGitHubで公開されている問題の解説・想定ソースコードなどのリンクを表示します。
  6. // @match https://atcoder.jp/contests/typical90*
  7. // @require https://code.jquery.com/jquery-3.6.0.min.js
  8. // @require https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.10.5/dayjs.min.js
  9. // @author hiro_hiro
  10. // @license CC0
  11. // @downloadURL
  12. // @updateURL
  13. // @homepage https://github.com/KATO-Hiro/AtCoder-Editorial-for-Typical90
  14. // @supportURL https://github.com/KATO-Hiro/AtCoder-Editorial-for-Typical90/issues
  15. // @grant GM_addStyle
  16. // ==/UserScript==
  17.  
  18. (async function () {
  19. "use strict";
  20.  
  21. addTabs();
  22.  
  23. addLatestTaskPage();
  24.  
  25. const tasks = await fetchTasks(); // TODO: Use cache to reduce access to AtCoder.
  26. addEditorialPage(tasks);
  27.  
  28. $(".nav-tabs a").click(function () {
  29. changeTab($(this));
  30. hideContentsOfPreviousPage();
  31.  
  32. return false;
  33. });
  34.  
  35. // TODO: 「解説」ボタンをクリックしたら、該当する問題のリンクを表示できるようにする
  36. })();
  37.  
  38. function addTabs() {
  39. addTabContentStyles();
  40. addTabContents();
  41. addLatestTaskTab();
  42. addEditorialTab();
  43. }
  44.  
  45. function addTabContentStyles() {
  46. const tabContentStyles = `
  47. .tab-content {
  48. display: none;
  49. }
  50. .tab-content.active {
  51. display: block;
  52. }
  53. `;
  54.  
  55. GM_addStyle(tabContentStyles);
  56. }
  57.  
  58. function addTabContents() {
  59. const contestNavTabsId = document.getElementById("contest-nav-tabs");
  60.  
  61. // See:
  62. // https://stackoverflow.com/questions/268490/jquery-document-createelement-equivalent
  63. // https://blog.toshimaru.net/jqueryhidden-inputjquery/
  64. const idNames = [
  65. "latest-task-created-by-userscript",
  66. "editorial-created-by-userscript"
  67. ];
  68.  
  69. for (const idName of idNames) {
  70. $("<div>", {
  71. class: "tab-content",
  72. id: idName,
  73. }).appendTo(contestNavTabsId);
  74. }
  75. }
  76.  
  77. // FIXME: Hard code is not good.
  78. function addLatestTaskTab() {
  79. const parentTag = document.getElementsByClassName("nav nav-tabs")[0];
  80. const liNode = document.createElement("li");
  81. parentTag.insertBefore(liNode, parentTag.children[1]);
  82.  
  83. const idName = "#latest-task-created-by-userscript";
  84. const aClass = "latest-task-a";
  85.  
  86. $("<a>", {
  87. class: aClass,
  88. href: idName,
  89. }).appendTo(liNode);
  90.  
  91. $("<span>", {
  92. class: "glyphicon glyphicon-star",
  93. text: "新着",
  94. style: "margin-right:4px;",
  95. "aria-hidden": true,
  96. }).appendTo(`.${aClass}`);
  97. }
  98.  
  99. // FIXME: Hard coding is not good.
  100. function addEditorialTab() {
  101. // See:
  102. // https://api.jquery.com/before/
  103. $("li.pull-right").before("<li><a href='#editorial-created-by-userscript'><span class='glyphicon glyphicon-book' style='margin-right:4px;' aria-hidden='true'></span>解説</a></li>");
  104. }
  105.  
  106. function addLatestTaskPage() {
  107. const latestTaskId = "#latest-task-created-by-userscript";
  108.  
  109. showHeader("latest-task-header", "新着", latestTaskId);
  110. addHorizontalRule(latestTaskId);
  111.  
  112. const message = `注: コンテスト開催期間の平日と土曜日の08:00(日本標準時)に更新されます。`;
  113. addNote("latest-task-message", message, latestTaskId);
  114.  
  115. const taskId = getTodayTaskId();
  116. const taskIdPaddingZero = padZero(taskId);
  117. addLatestTaskHeader(taskIdPaddingZero, latestTaskId);
  118.  
  119. const ulName = "latest-task-ul";
  120.  
  121. $("<ul>", {
  122. class: ulName,
  123. text: ""
  124. }).appendTo(latestTaskId);
  125.  
  126. const githubRepoUrl = getGitHubRepoUrl();
  127. const latestTaskWithImageUrl = githubRepoUrl + "problem/" + taskIdPaddingZero + ".jpg";
  128. const latestTaskUrl = githubRepoUrl + "problem-txt/" + taskIdPaddingZero + ".txt";
  129. const latestTaskSampleUrl = githubRepoUrl + "sample/" + taskIdPaddingZero + ".txt";
  130. addLatestTask(latestTaskWithImageUrl, latestTaskUrl, ulName);
  131. addSample(latestTaskSampleUrl, ulName);
  132. }
  133.  
  134. function addLatestTaskHeader(taskId, parentTag) {
  135. addHeader(
  136. "<h3>", // heading_tag
  137. "latest-task", // className
  138. `問題 ${taskId}`, // text
  139. parentTag
  140. );
  141. }
  142.  
  143. function padZero(taskId) {
  144. // See:
  145. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
  146. return String(taskId).padStart(3, '0');
  147. }
  148.  
  149. function addLatestTask(latestTaskWithImageUrl, latestTaskUrl, parentTag) {
  150. const taskWithImage = "latest-task-with-image-li";
  151.  
  152. $("<li>", {
  153. class: taskWithImage,
  154. text: ""
  155. }).appendTo(`.${parentTag}`);
  156.  
  157. $("<a>", {
  158. class: "latest-task-image-url",
  159. href: latestTaskWithImageUrl,
  160. text: "問題文 + 画像",
  161. target: "_blank",
  162. rel: "noopener",
  163. }).appendTo(`.${taskWithImage}`);
  164.  
  165. const task = "latest-task-li";
  166.  
  167. $("<li>", {
  168. class: task,
  169. text: ""
  170. }).appendTo(`.${parentTag}`);
  171. $("<a>", {
  172. class: "latest-task-text-url",
  173. href: latestTaskUrl,
  174. text: "問題文のみ",
  175. target: "_blank",
  176. rel: "noopener",
  177. }).appendTo(`.${task}`);
  178. }
  179.  
  180. function addSample(url, parentTag) {
  181. const liName = "latest-task-sample-li";
  182.  
  183. $("<li>", {
  184. class: liName,
  185. text: ""
  186. }).appendTo(`.${parentTag}`);
  187.  
  188. $("<a>", {
  189. class: "latest-task-sample-url",
  190. href: url,
  191. text: "サンプル(入力形式、入出力例)",
  192. target: "_blank",
  193. rel: "noopener",
  194. }).appendTo(`.${liName}`);
  195. }
  196.  
  197. function getTodayTaskId() {
  198. // See:
  199. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
  200. const today = getToday();
  201. const startDay = getStartDay();
  202. let taskCount = today.diff(startDay, "day") + 1;
  203. taskCount -= countSunday(today);
  204.  
  205. if (!isSunday(today) && (isBeforeAmEight(today))) {
  206. taskCount -= 1
  207. }
  208.  
  209. const maxTaskId = 90;
  210. taskCount = Math.min(taskCount, maxTaskId);
  211.  
  212. return taskCount;
  213. }
  214.  
  215. // See:
  216. // https://day.js.org/en/
  217. function getToday() {
  218. const today = dayjs();
  219.  
  220. return today;
  221. }
  222.  
  223. function getStartDay() {
  224. const startDay = dayjs("2021-03-30");
  225.  
  226. return startDay;
  227. }
  228.  
  229. function getEndDay() {
  230. const endDay = dayjs("2021-07-12");
  231.  
  232. return endDay;
  233. }
  234.  
  235. function countSunday(today) {
  236. const sundays = getSundays();
  237. let count = 0;
  238.  
  239. for (const sunday of sundays) {
  240. if (today.isAfter(sunday)) {
  241. count += 1;
  242. }
  243. }
  244.  
  245. return count;
  246. }
  247.  
  248. function getSundays() {
  249. const sundays = [
  250. dayjs("2021-04-04"),
  251. dayjs("2021-04-11"),
  252. dayjs("2021-04-18"),
  253. dayjs("2021-04-25"),
  254. dayjs("2021-05-02"),
  255. dayjs("2021-05-09"),
  256. dayjs("2021-05-16"),
  257. dayjs("2021-05-23"),
  258. dayjs("2021-05-30"),
  259. dayjs("2021-06-06"),
  260. dayjs("2021-06-13"),
  261. dayjs("2021-06-20"),
  262. dayjs("2021-06-27"),
  263. dayjs("2021-07-04"),
  264. dayjs("2021-07-11"),
  265. ];
  266.  
  267. return sundays;
  268. }
  269.  
  270. function isSunday(today) {
  271. const sunday = 0;
  272.  
  273. if (today.day() == sunday) {
  274. return true;
  275. } else {
  276. return false
  277. }
  278. }
  279.  
  280. function isBeforeAmEight(today) {
  281. const todayHour = today.hour();
  282. const todayMinute = today.minute();
  283.  
  284. const year = today.year();
  285. const month = today.month() + 1; // 0-indexed.
  286. const date = today.date();
  287. const amEight = dayjs(`${year}-${month}-${date}T08:00`);
  288.  
  289. if (today.isBefore(amEight)) {
  290. return true
  291. } else {
  292. false
  293. }
  294. }
  295.  
  296. // TODO: キャッシュを利用して、本家へのアクセスを少なくなるようにする
  297. async function fetchTasks() {
  298. const tbodies = await fetchTaskPage();
  299. const tasks = new Object();
  300. let taskCount = 1;
  301.  
  302. for (const [index, aTag] of Object.entries($(tbodies).find("a"))) {
  303. // Ignore a-tags including task-id and "Submit".
  304. if (index % 3 == 1) {
  305. const taskId = String(taskCount).padStart(3, "0");
  306. tasks[taskId] = [aTag.text, aTag.href];
  307. taskCount += 1;
  308. }
  309. }
  310.  
  311. return tasks;
  312. }
  313.  
  314. async function fetchTaskPage() {
  315. // See:
  316. // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
  317. // https://developer.mozilla.org/en-US/docs/Web/API/Body/text
  318. // https://developer.mozilla.org/ja/docs/Web/API/DOMParser
  319. // https://api.jquery.com/each/
  320. // http://dyn-web.com/tutorials/object-literal/properties.php#:~:text=Add%20a%20Property%20to%20an%20Existing%20Object%20Literal&text=myObject.,if%20it%20is%20a%20string).
  321. const tbodies = await fetch("https://atcoder.jp/contests/typical90/tasks", {
  322. method: "GET"
  323. })
  324. .then(response => {
  325. return response.text()
  326. })
  327. .then(html => {
  328. const parser = new DOMParser();
  329. const doc = parser.parseFromString(html, "text/html");
  330. const messages = doc.querySelector("#main-container > div.row > div:nth-child(2) > div > table > tbody");
  331.  
  332. return messages;
  333. })
  334. .catch(error => {
  335. console.warn('Something went wrong.', error);
  336. });
  337.  
  338. return tbodies;
  339. }
  340.  
  341. function addEditorialPage(tasks) {
  342. const editorialId = "#editorial-created-by-userscript";
  343.  
  344. showHeader("editorial-header", "解説", editorialId);
  345. addHorizontalRule(editorialId);
  346. showDifficultyVotingAndUserCodes(editorialId);
  347.  
  348. let taskEditorialsDiv = addDiv("task-editorials", editorialId);
  349. taskEditorialsDiv = "." + taskEditorialsDiv;
  350. addEditorials(tasks, taskEditorialsDiv);
  351. }
  352.  
  353. function showHeader(className, text, tag) {
  354. addHeader(
  355. "<h2>", // heading_tag
  356. className, // className
  357. text, // text
  358. tag // parent_tag
  359. );
  360. }
  361.  
  362. function addHeader(heading_tag, className, text, parent_tag) {
  363. $(heading_tag, {
  364. class: className,
  365. text: text,
  366. }).appendTo(parent_tag);
  367. }
  368.  
  369. function addHorizontalRule(tag) {
  370. // See:
  371. // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr
  372. $("<hr>", {
  373. class: "",
  374. }).appendTo(tag);
  375. }
  376.  
  377. function showDifficultyVotingAndUserCodes(tag) {
  378. addHeader(
  379. "<h3>", // heading_tag
  380. "difficulty-voting-and-user-codes", // className
  381. "問題の難易度を投票する・ソースコードを共有する", // text
  382. tag // parent_tag
  383. );
  384.  
  385. $("<ul>", {
  386. class: "spread-sheets-ul",
  387. text: ""
  388. }).appendTo(tag);
  389.  
  390. const spreadSheetUrl = "https://docs.google.com/spreadsheets/d/1GG4Higis4n4GJBViVltjcbuNfyr31PzUY_ZY1zh2GuI/edit#gid=";
  391.  
  392. const homeID = "0";
  393. addSpreadSheetHomeURL(spreadSheetUrl + homeID);
  394.  
  395. const difficultyVotingID = "1593175261";
  396. addDifficultyVotingURL(spreadSheetUrl + difficultyVotingID);
  397.  
  398. const taskGroups = [
  399. ["001", "023", spreadSheetUrl + "105162261"], // task start, task end, spread sheet id.
  400. ["024", "047", spreadSheetUrl + "1671161250"],
  401. ["048", "071", spreadSheetUrl + "671876031"],
  402. ["072", "090", spreadSheetUrl + "428850451"]
  403. ];
  404.  
  405. // See:
  406. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
  407. taskGroups.forEach(
  408. taskGroup => {
  409. const taskStart = taskGroup[0];
  410. const taskEnd = taskGroup[1];
  411. const url = taskGroup[2];
  412.  
  413. addUserCodesURL(
  414. taskStart,
  415. taskEnd,
  416. url
  417. );
  418. }
  419. );
  420. }
  421.  
  422. function addSpreadSheetHomeURL(url) {
  423. $("<li>", {
  424. class: "spread-sheet-home-li",
  425. text: ""
  426. }).appendTo(".spread-sheets-ul");
  427.  
  428. $("<a>", {
  429. class: "spread-sheet-home-url",
  430. href: url,
  431. text: "目的",
  432. target: "_blank",
  433. rel: "noopener",
  434. }).appendTo(".spread-sheet-home-li");
  435. }
  436.  
  437. function addDifficultyVotingURL(url) {
  438. $("<li>", {
  439. class: "difficulty-voting-li",
  440. text: ""
  441. }).appendTo(".spread-sheets-ul");
  442.  
  443. $("<a>", {
  444. class: "difficulty-voting-url",
  445. href: url,
  446. text: "問題の難易度を投票する",
  447. target: "_blank",
  448. rel: "noopener",
  449. }).appendTo(".difficulty-voting-li");
  450. }
  451.  
  452. function addUserCodesURL(taskStart, taskEnd, url) {
  453. // See:
  454. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Template_literals
  455. $("<li>", {
  456. class: `user-codes-${taskStart}-${taskEnd}-li`,
  457. text: ""
  458. }).appendTo(".spread-sheets-ul");
  459.  
  460. $("<a>", {
  461. class: `user-codes-${taskStart}-${taskEnd}-url`,
  462. href: url,
  463. text: `ソースコード(${taskStart}〜${taskEnd})を見る・共有する`,
  464. target: "_blank",
  465. rel: "noopener",
  466. }).appendTo(`.user-codes-${taskStart}-${taskEnd}-li`);
  467. }
  468.  
  469. function addDiv(tagName, parentTag) {
  470. $("<div>", {
  471. class: tagName,
  472. }).appendTo(parentTag);
  473.  
  474. return tagName;
  475. }
  476.  
  477. function addEditorials(tasks, parentTag) {
  478. const githubRepoUrl = getGitHubRepoUrl();
  479. const editorialsUrl = githubRepoUrl + "editorial/";
  480. const codesUrl = githubRepoUrl + "sol/";
  481.  
  482. // See:
  483. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
  484. const latestTaskId = Object.keys(tasks).slice(-1)[0];
  485.  
  486. // HACK: 公開当日分の問題についてはリンク切れを回避するため、解説・ソースコードの一覧を示すことで応急的に対処
  487. // HACK: 問題によっては、複数の解説とソースコードが公開される日もある
  488. // getMultipleEditorialUrlsIfNeeds()とgetMultipleCodeUrls()で、アドホック的に対処している
  489. for (const [taskId, [taskName, taskUrl]] of Object.entries(tasks)) {
  490. let taskEditorialDiv = addDiv(`task-${taskId}-editorial`, parentTag);
  491. taskEditorialDiv = "." + taskEditorialDiv;
  492.  
  493. // See:
  494. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
  495. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
  496. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/split
  497. showTaskName(taskId, `${taskId} - ${taskName}`, taskUrl, taskEditorialDiv);
  498.  
  499. if (taskId == latestTaskId) {
  500. const message = "注: 閲覧する時間帯によっては、公式解説・想定ソースコードが公開されているかもしれません。しばらくお待ちください。";
  501. const additionalUrl = "(一覧)";
  502. addNote("no-editorial", message, taskEditorialDiv);
  503. showEditorial(taskId, editorialsUrl, additionalUrl, taskEditorialDiv);
  504. showCode(taskId, codesUrl, additionalUrl, taskEditorialDiv);
  505. } else {
  506. const additionalUrls = getMultipleEditorialUrlsIfNeeds(taskId);
  507.  
  508. // TODO: AtCoderの解説ページで図を表示できるようにする
  509. for (const [index, additionalUrl] of Object.entries(additionalUrls)) {
  510. const editorialUrl = editorialsUrl + taskId + additionalUrl + ".jpg";
  511. showEditorial(taskId + additionalUrl, editorialUrl, additionalUrl, taskEditorialDiv);
  512. }
  513.  
  514. const codeUrls = getMultipleCodeUrls(taskId);
  515.  
  516. // TODO: ソースコードをフォーマットされた状態で表示する
  517. for (const [index, codeUrl] of Object.entries(codeUrls)) {
  518. const editorialCodelUrl = codesUrl + taskId + codeUrl;
  519. const [additionalUrl, language] = codeUrl.split(".");
  520. showCode(taskId + additionalUrl, editorialCodelUrl, codeUrl, taskEditorialDiv);
  521. }
  522. }
  523. }
  524. }
  525.  
  526. function getGitHubRepoUrl() {
  527. const url = "https://github.com/E869120/kyopro_educational_90/blob/main/";
  528.  
  529. return url;
  530. }
  531.  
  532. function showTaskName(taskId, taskName, taskUrl, tag) {
  533. const taskIdClass = `task-${taskId}`;
  534.  
  535. addHeader(
  536. "<h3>", // heading_tag
  537. taskIdClass, // className
  538. taskName, // text
  539. tag // parent_tag
  540. );
  541.  
  542. $("<a>", {
  543. class: `${`task-${taskId}-url`} small glyphicon glyphicon-new-window`,
  544. href: taskUrl,
  545. target: "_blank",
  546. }).appendTo(`.${taskIdClass}`);
  547. }
  548.  
  549. // TODO: 複数の解説資料がアップロードされた日があれば更新する
  550. function getMultipleEditorialUrlsIfNeeds(taskId) {
  551. // See:
  552. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Working_with_Objects
  553. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Property_Accessors
  554. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/in
  555.  
  556. // タスク名: 解説ファイルの番号
  557. // 0xx-yyy.jpgの0xxをキーに、-yyyを値としている
  558. const multipleEditorialUrls = {
  559. "005": ["-01", "-02", "-03"],
  560. "011": ["-01", "-02"],
  561. "017": ["-01", "-02", "-03"],
  562. "023": ["-01", "-02", "-03", "-04"],
  563. "029": ["-01", "-02"],
  564. "035": ["-01", "-02", "-03"],
  565. "041": ["-01", "-02", "-03"],
  566. "047": ["-01", "-02"],
  567. "053": ["-01", "-02", "-03", "-04"],
  568. "059": ["-01", "-02", "-03"],
  569. "065": ["-01", "-02", "-03"],
  570. "071": ["-01", "-02", "-03"],
  571. "077": ["-01", "-02", "-03"],
  572. };
  573.  
  574. if (taskId in multipleEditorialUrls) {
  575. return multipleEditorialUrls[taskId];
  576. } else {
  577. return [""]; // dummy
  578. }
  579. }
  580.  
  581. // TODO: 複数の想定コードがアップロードされた日があれば更新する
  582. function getMultipleCodeUrls(taskId) {
  583. // タスク名: ソースコードの番号と拡張子
  584. // 0xx-yyy.langの0xxをキーに、-yyy.langを値としている
  585. const multipleCodeUrls = {
  586. "005": ["-01.cpp", "-02.cpp", "-03.cpp"],
  587. "011": ["-01.cpp", "-02.cpp", "-03.cpp"],
  588. "017": ["-01.cpp", "-02.cpp", "-03.cpp"],
  589. "023": ["-01.cpp", "-02.cpp", "-03.cpp", "-04a.cpp", "-04b.cpp"],
  590. "029": ["-01.cpp", "-02.cpp", "-03.cpp"],
  591. "035": ["-01.cpp", "-02.cpp", "-03.cpp", "-04.cpp"],
  592. "041": ["-01a.cpp", "-01b.cpp", "-02.cpp", "-03.cpp"],
  593. "047": ["-01.cpp", "-02.cpp"],
  594. "053": ["-01.cpp", "-02.cpp", "-03.cpp", "-04.cpp"],
  595. "055": [".cpp", "-02.py", "-03.py"],
  596. "059": ["-01.cpp", "-02.cpp"],
  597. "061": ["-01.cpp", "-02.cpp"],
  598. "065": ["-01.cpp", "-02.cpp", "-03.cpp"],
  599. "066": ["a.cpp", "b.cpp"],
  600. "068": ["a.cpp", "b.cpp"],
  601. "071": ["-02.cpp", "-03.cpp"],
  602. "077": ["-01.cpp", "-02.cpp", "-03.cpp", "-04a.cpp", "-04b.cpp"],
  603. };
  604.  
  605. if (taskId in multipleCodeUrls) {
  606. return multipleCodeUrls[taskId];
  607. } else {
  608. return [".cpp"];
  609. }
  610. }
  611.  
  612. function addNote(className, message, parent_tag) {
  613. $("<p>", {
  614. class: className,
  615. text: message,
  616. }).appendTo(parent_tag);
  617. }
  618.  
  619. function showEditorial(taskId, url, additionalUrl, tag) {
  620. const ulClass = `editorial-${taskId}-ul`;
  621. const liClass = `editorial-${taskId}-li`;
  622.  
  623. $("<ul>", {
  624. class: ulClass,
  625. text: ""
  626. }).appendTo(tag);
  627.  
  628. $("<li>", {
  629. class: liClass,
  630. text: ""
  631. }).appendTo(`.${ulClass}`);
  632.  
  633. $("<a>", {
  634. class: `editorial-${taskId}-url`,
  635. href: url,
  636. text: `公式解説${additionalUrl}`,
  637. target: "_blank",
  638. rel: "noopener",
  639. }).appendTo(`.${liClass}`);
  640. }
  641.  
  642. function showCode(taskId, url, additionalUrl, tag) {
  643. const ulClass = `editorial-${taskId}-code-ul`;
  644. const liClass = `editorial-${taskId}-code-li`;
  645.  
  646. $("<ul>", {
  647. class: ulClass,
  648. text: ""
  649. }).appendTo(tag);
  650.  
  651. $("<li>", {
  652. class: liClass,
  653. text: ""
  654. }).appendTo(`.${ulClass}`);
  655.  
  656. $("<a>", {
  657. class: `editorial-${taskId}-code-url`,
  658. href: url,
  659. text: `想定ソースコード${additionalUrl}`,
  660. target: "_blank",
  661. rel: "noopener",
  662. }).appendTo(`.${liClass}`);
  663. }
  664.  
  665. function addEditorialButtonToTaskPage() {
  666. // See:
  667. // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
  668. // https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
  669. const editorialButton = document.createElement("a");
  670. editorialButton.classList.add("btn", "btn-default", "btn-sm");
  671. editorialButton.textContent = "解説";
  672.  
  673. const taskTitle = document.querySelector(".row > div > .h2");
  674.  
  675. if (taskTitle) {
  676. taskTitle.appendChild(editorialButton);
  677. return editorialButton;
  678. } else {
  679. return;
  680. }
  681. }
  682.  
  683. function changeTab(this_object) {
  684. // See:
  685. // https://api.jquery.com/parent/
  686. // https://api.jquery.com/addClass/#addClass-className
  687. // https://api.jquery.com/siblings/#siblings-selector
  688. // https://api.jquery.com/removeClass/#removeClass-className
  689. // https://www.design-memo.com/coding/jquery-tab-change
  690. this_object.parent().addClass("active").siblings(".active").removeClass("active");
  691. const tabContentsUrl = this_object.attr("href");
  692. $(tabContentsUrl).addClass("active").siblings(".active").removeClass("active");
  693. }
  694.  
  695. function hideContentsOfPreviousPage() {
  696. // See:
  697. // https://api.jquery.com/length/
  698. // https://api.jquery.com/hide/
  699. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
  700. // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String
  701. const tagCount = $(".col-sm-12").length;
  702.  
  703. for (let index = 0; index < tagCount; index++) {
  704. if (index != 0) {
  705. $("#main-container > div.row > div:nth-child(" + String(index + 1) + ")").hide();
  706. }
  707. }
  708. }