Hydro-cph

Hydro题目传送至cph

  1. // ==UserScript==
  2. // @name Hydro-cph
  3. // @namespace https://github.com/LYkcul/HydroCPH
  4. // @version 1.0.1
  5. // @description Hydro题目传送至cph
  6. // @author LYkcul
  7. // @match *://*/*
  8. // @license AGPL-3.0 license
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. var url = window.location.href;
  15.  
  16. async function cph() {
  17. const pid = /\/p\/([^/]+)/.exec(url)[1];
  18. const test = [...document.querySelectorAll('pre > code')];
  19. let resTest = [];
  20. for (let i = 0; i < test.length - 1; i += 2) {
  21. resTest.push({
  22. input: test[i].textContent,
  23. output: test[i + 1].textContent
  24. })
  25. }
  26. const tmpTime = document.querySelector('.icon-stopwatch').textContent;
  27. const timeLimit = parseInt(/(\d+)ms/.exec(tmpTime)[1], 10);
  28. const tmpMemory = document.querySelector('.icon-comparison').textContent;
  29. const memoryLimit = parseInt(/(\d+)MiB/.exec(tmpMemory)[1], 10);
  30.  
  31. GM_xmlhttpRequest({
  32. url: "http://localhost:27121/",
  33. method: "POST",
  34. data: JSON.stringify({
  35. batch: {
  36. id: "hydroCPH",
  37. size: 1
  38. },
  39. name: `Hydro_${pid}`,
  40. group: "Hydro",
  41. url: url,
  42. interactive: "false",
  43. memoryLimit: memoryLimit,
  44. timeLimit: timeLimit,
  45. tests: resTest,
  46. input: {
  47. type: "stdin"
  48. },
  49. output: {
  50. type: "stdout"
  51. },
  52. language: {
  53. java: {
  54. mainClass: "Main",
  55. taskClass: pid
  56. }
  57. },
  58. testType: "single"
  59. }),
  60. onload(f) {
  61. f.status === 502 && alert('cph 传送失败')
  62. },
  63. onerror() {
  64. alert('cph 传送失败')
  65. }
  66. })
  67. }
  68.  
  69. async function HydroCPH() {
  70. const cphL = document.createElement('li');
  71. cphL.className = 'menu__item nojs--hide';
  72. const cphA = document.createElement('a');
  73. cphA.className = 'menu__link';
  74. cphA.setAttribute('name', 'problem-cph');
  75. const cphS = document.createElement('span');
  76. cphS.className = 'icon icon-send';
  77. const text = document.createTextNode('传送至 cph');
  78.  
  79. cphA.appendChild(cphS);
  80. cphA.appendChild(text);
  81. cphL.appendChild(cphA);
  82.  
  83. const pos = document.querySelector('.menu');
  84. //console.log(pos.textContent);
  85. pos.appendChild(cphL);
  86.  
  87. cphL.addEventListener('click', async function () {
  88. await cph();
  89. });
  90. }
  91.  
  92. function checkUiContext() {
  93. const allScript = document.querySelectorAll('script');
  94. for (const node of allScript) {
  95. if (node.textContent.includes('UiContext')) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101.  
  102. const pageInfo = document.querySelector('html');
  103. if (pageInfo) {
  104. const dataPage = pageInfo.getAttribute('data-page');
  105. if (dataPage && checkUiContext() && (dataPage === "problem_detail" || dataPage.includes("detail_problem"))) {
  106. window.addEventListener('load', HydroCPH);
  107. }
  108. }
  109. })();