Improved アルゴ式

提出とか実行を楽にしたいな

当前为 2021-12-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Improved アルゴ式
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  提出とか実行を楽にしたいな
// @author       eggplants
// @homepage     https://github.com/eggplants
// @match        https://algo-method.com/tasks/*
// @run-at       document-idle
// @license      MIT

// ==/UserScript==

/*jshint esversion: 6 */

const addOpt = (i, t, target) => {
  opt = document.createElement("option");
  opt.value = i;
  opt.text = t;
  target.appendChild(opt);
};

const checkCorrect = () => {
  var v = document.getElementById("exSelect").value;
  var a = document.querySelector(".EditorWrapper_codeTestOutputArea__7Ty3v");
  var ans_show = document.getElementById("ansShow");
  if (v < 1) {
    a.style.backgroundColor = "#d6d6d0";
    ans_show.value = "";
  } else {
    ans = pre[v * 2].innerText.replace(/\nCOPY$/, "\n");
    ans_show.value = ans;
    a.style.backgroundColor = ans === a.value ? "#98fb98" : "#ffffa8";
  }
};

const addShortcutRun = (evt) => {
  evt.preventDefault();
  if (evt.metaKey || evt.ctrlKey) {
    if (evt.keyCode == 13) {
      document.querySelectorAll(".Button_button__HxzDS")[5].click();
    }
  }
};

window.onload = function () {
  // set style
  ed = document.querySelector("section");
  ed_wrap = document.querySelector(".Task_component__pgBHU");
  ed.addEventListener("keydown", addShortcutRun);
  ed.style.height = "10vh";

  // insert textarea for checking answer
  ans_show = document.createElement("div");
  ans_show.innerHTML =
    "<div>出力例:</div>" +
    '<textarea class="EditorWrapper_codeTestErrorArea__KClBp" ' +
    'id="ansShow" disabled=""></textarea>';
  ans_show.classList.add("EditorWrapper_codeTestFlex__q5EsJ");
  document
    .querySelectorAll(".EditorWrapper_codeTestFlex__q5EsJ")[2]
    .after(ans_show);

  // create select menu
  pre = document.querySelectorAll("pre:not([class])");
  select_ex = document.createElement("select");
  select_ex.id = "exSelect";
  select_ex.classList.add("EditorWrapper_languageChanger___Fl02");
  select_ex.style.marginLeft = "5px";
  select_ex.style.textAlignLast = "center";
  addOpt(0, `---`, select_ex);
  for (var i = 1; i <= (pre.length - 1) / 2; i++)
    addOpt(i, `例 ${i}`, select_ex);
  select_ex.onchange = () => {
    var v = document.getElementById("exSelect").value;
    var ipt = document.querySelector(".EditorWrapper_codeTestInputArea__fxPTR");
    if (v == 0) {
      ipt.value = "";
    } else {
      ipt.value = pre[(v - 1) * 2 + 1].innerText.replace(/\nCOPY$/, "\n");
    }
  };

  // insert select menu
  document
    .querySelector(".EditorWrapper_codeTestHeader__SEnNi")
    .after(select_ex);

  // add timer of checking answer
  setInterval(checkCorrect, 100);
  // document.querySelector(".EditorWrapper_codeTestOutputArea__7Ty3v").addEventListener("inputs", checkCorrect);
  Array.from(document.getElementsByTagName("textarea"))
    .slice(1)
    .forEach((e) => (e.style.height = "20vh"));

  // add shortcut
  var ipt = document.querySelector(".EditorWrapper_codeTestInputArea__fxPTR");
  ipt.placeholder = "Run: (ctrl||cmd) + Enter";
  ipt.addEventListener("keydown", addShortcutRun);
};