AtCoder TestCase Extension

AtCoderテストケースへのリンクを追加します

2019-10-13 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name AtCoder TestCase Extension
  3. // @namespace tatsumack
  4. // @version 1.0.8
  5. // @description AtCoderテストケースへのリンクを追加します
  6. // @author tatsumack
  7. // @license MIT
  8. // @supportURL https://github.com/tatsumack/atcoder-testcase-extension/issues
  9. // @match https://atcoder.jp/contests/*/submissions/*
  10. // ==/UserScript==
  11.  
  12. (function (callback) {
  13. var script = document.createElement("script");
  14. script.setAttribute("src", "//code.jquery.com/jquery-3.3.1.min.js");
  15. script.addEventListener('load', function() {
  16. var script = document.createElement("script");
  17. script.textContent = "(" + callback.toString() + ")(jQuery.noConflict(true));";
  18. document.body.appendChild(script);
  19. }, false);
  20. document.body.appendChild(script);
  21. })(function ($) {
  22. const url = location.href;
  23. const contestName = getContestName();
  24. const cacheDataKey = "atcoder-testcase-" + contestName;
  25. const cacheFetchedAtKey = "atcoder-test-case-last-fetched-at-" + contestName;
  26. const cacheMin = 10;
  27.  
  28. function onSuccess(data) {
  29. if (data.status != "success") return;
  30. localStorage.setItem(cacheDataKey, JSON.stringify(data));
  31.  
  32. var problemId = getProbremId();
  33. var content = JSON.parse(data.content);
  34. draw(content[problemId].in, content[problemId].out, content[problemId].ext);
  35. }
  36.  
  37. function draw(inUrl, outUrl, ext) {
  38. if (!inUrl || !outUrl) return;
  39.  
  40. $("#main-container > div.row > div:nth-child(2) > div:nth-last-child(1) > table > tbody > tr > td:nth-child(1)").each(function () {
  41. var testCaseName = $(this).text();
  42. var fileName = getFileName(testCaseName, ext);
  43. $(this).append(" [ <a href='" + inUrl + "?preview=" + fileName.in + "'>in</a> / <a href='" + outUrl + "?preview=" + fileName.out + "'>out</a> ]");
  44. });
  45. }
  46.  
  47. function getFileName(testCaseName, ext) {
  48. let inFile = testCaseName;
  49. let outFile = testCaseName;
  50. if (testCaseName.indexOf(".txt") === -1 && ext !== 'none') {
  51. inFile += ext === '' ? ".in" : '.' + ext;
  52. outFile += ext === '' ? ".out" : '.' + ext;
  53. }
  54. return {in: inFile, out: outFile};
  55. }
  56.  
  57. function getContestName() {
  58. return url.split("/")[4];
  59. }
  60.  
  61. function getProbremId() {
  62. return $("#main-container > div.row > div:nth-child(2) > div:nth-child(9) > table > tbody > tr:nth-child(2) > td > a").first().text().slice(0, 1).toUpperCase();
  63. }
  64.  
  65. function main() {
  66. const data = localStorage.getItem(cacheDataKey);
  67. const lastFetchedAt = localStorage.getItem(cacheFetchedAtKey);
  68. if (data && lastFetchedAt && new Date().getTime() < Number(lastFetchedAt) + cacheMin * 60 * 1000) {
  69. onSuccess(JSON.parse(data));
  70. return;
  71. }
  72.  
  73. $.ajax({
  74. url: "https://script.google.com/macros/s/AKfycbyUlYoF05ux7M1jBRnXwYkV9SjJIL9MNlHbWiB_eFiE93_91Hs/exec?contest=" + contestName,
  75. dataType: "json",
  76. type: "get",
  77. crossDomain: true,
  78. success: onSuccess
  79. });
  80. localStorage.setItem(cacheFetchedAtKey, new Date().getTime().toString());
  81. }
  82.  
  83. main();
  84.  
  85. });