AtCoder TestCase Extension

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

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

  1. // ==UserScript==
  2. // @name AtCoder TestCase Extension
  3. // @namespace tatsumack
  4. // @version 1.0.6
  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);
  35. }
  36.  
  37. function draw(inUrl, outUrl) {
  38. if (!inUrl || !outUrl) return;
  39.  
  40. $("#main-container > div.row > div:nth-child(2) > div:nth-child(12) > table > tbody > tr > td:nth-child(1)").each(function () {
  41. var testCaseName = $(this).text();
  42. var fileName = getFileName(testCaseName);
  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) {
  48. let inFile = testCaseName;
  49. let outFile = testCaseName;
  50. const exceptionList = ['arc096', 'abc095', 'abc043', 'arc059', 'nikkei2019-qual', 'abc120'];
  51. if (testCaseName.indexOf(".txt") === -1 && exceptionList.indexOf(contestName) === -1) {
  52. inFile += ".in";
  53. outFile += ".out";
  54. }
  55. return {in: inFile, out: outFile};
  56. }
  57.  
  58. function getContestName() {
  59. return url.split("/")[4];
  60. }
  61.  
  62. function getProbremId() {
  63. 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();
  64. }
  65.  
  66. function main() {
  67. const data = localStorage.getItem(cacheDataKey);
  68. const lastFetchedAt = localStorage.getItem(cacheFetchedAtKey);
  69. if (data && lastFetchedAt && new Date().getTime() < Number(lastFetchedAt) + cacheMin * 60 * 1000) {
  70. onSuccess(JSON.parse(data));
  71. return;
  72. }
  73.  
  74. $.ajax({
  75. url: "https://script.google.com/macros/s/AKfycbyUlYoF05ux7M1jBRnXwYkV9SjJIL9MNlHbWiB_eFiE93_91Hs/exec?contest=" + contestName,
  76. dataType: "json",
  77. type: "get",
  78. crossDomain: true,
  79. success: onSuccess
  80. });
  81. localStorage.setItem(cacheFetchedAtKey, new Date().getTime().toString());
  82. }
  83.  
  84. main();
  85.  
  86. });