AtCoder TestCase Extension

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

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

  1. // ==UserScript==
  2. // @name AtCoder TestCase Extension
  3. // @namespace tatsumack
  4. // @version 1.0.1
  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 testCaseName = $(this).text();
  44. var fileName = getFileName(testCaseName);
  45. $(this).append(" [ <a href='" + inUrl + "?preview=" + fileName.in + "'>in</a> / <a href='" + outUrl + "?preview=" + fileName.out + "'>out</a> ]");
  46. });
  47. }
  48.  
  49. function getFileName(testCaseName) {
  50. let inFile = testCaseName;
  51. let outFile = testCaseName;
  52. const exceptionList = ['arc096', 'abc095'];
  53. if (testCaseName.indexOf(".txt") === -1 && exceptionList.indexOf(contestName) === -1) {
  54. inFile += ".in";
  55. outFile += ".out";
  56. }
  57. return {in: inFile, out: outFile};
  58. }
  59.  
  60. function getContestName() {
  61. return isBeta ? url.split("/")[4] : url.split("/")[2].split(".")[0];
  62. }
  63.  
  64. function getProbremId() {
  65. return $("td a").first().text().slice(0, 1).toUpperCase();
  66. }
  67.  
  68. function main() {
  69. if (contestName.indexOf("abc") == -1 && contestName.indexOf("arc") == -1 && contestName.indexOf("agc") == -1) return;
  70.  
  71. const data = localStorage.getItem(cacheDataKey);
  72. const lastFetchedAt = localStorage.getItem(cacheFetchedAtKey);
  73. if (data && lastFetchedAt && new Date().getTime() < Number(lastFetchedAt) + cacheMin * 60 * 1000) {
  74. onSuccess(JSON.parse(data));
  75. return;
  76. }
  77.  
  78. $.ajax({
  79. url: "https://script.google.com/macros/s/AKfycbyUlYoF05ux7M1jBRnXwYkV9SjJIL9MNlHbWiB_eFiE93_91Hs/exec?contest=" + contestName,
  80. dataType: "json",
  81. type: "get",
  82. crossDomain: true,
  83. success: onSuccess
  84. });
  85. localStorage.setItem(cacheFetchedAtKey, new Date().getTime().toString());
  86. }
  87.  
  88. main();
  89.  
  90. });