AtCoder TestCase Extension

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

נכון ליום 02-09-2018. ראה הגרסה האחרונה.

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