AtCoder Submission Language Detector

Automatically detects the language used based on the information in the source code comments and selects it as the one to be submitted.

07.08.2022 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         AtCoder Submission Language Detector
// @namespace    https://twitter.com/KakurenboUni
// @version      1.0.1
// @description  Automatically detects the language used based on the information in the source code comments and selects it as the one to be submitted.
// @author       uni-kakurenbo
// @match        https://atcoder.jp/contests/*/tasks/*
// @match        https://atcoder.jp/contests/*/submit*
// @match        https://atcoder.jp/contests/*/custom_test*
// @license      MIT
// @supportURL   https://twitter.com/KakurenboUni
// ==/UserScript==

(async function () {
  "use strict";

  const DETECTION_REG_EXP = /#.*lang(?:uage)?:?(?<args>\s+[^\n\r*/#]+)/;

  await rendered();

  const $editor = $(".editor").data("editor").doc;
  const $plainTextarea = $(".plain-textarea");

  const $selectLanguage = $("#select-lang select");
  const $languageOptions = $selectLanguage[0].querySelectorAll("option");
  const languageOptions = [].map.call($languageOptions, ({ value, label, dataset: { mime } = {} }) => {
    return {
      id: value,
      label: label.toLowerCase() ?? "",
      code: mime?.toLowerCase().replaceAll(/^.+\/x?|src$/g, "") ?? "",
    };
  });

  $editor.on("change", updateLanguageSettings);
  $plainTextarea.on("input", updateLanguageSettings);
  document.addEventListener("paste", updateLanguageSettings);
  $("#input-open-file").on("change", () => { setTimeout(updateLanguageSettings, 0); });

  function updateLanguageSettings() {
    const sourceCode = getSourceCode();

    const languageInfomation = sourceCode.match(DETECTION_REG_EXP);
    if (!languageInfomation || !languageInfomation?.groups?.args) return;

    let languageSelectors = languageInfomation.groups.args?.trim().replace(/\s+/g, " ").split(" ");
    languageSelectors = languageSelectors.map((selector) => selector.toLowerCase());

    const selectedOption = languageOptions.find((option) => {
      return (
        languageSelectors.includes(option.id) ||
        languageSelectors.every((selector) => option.label.includes(selector)) ||
        languageSelectors.every((selector) => option.code.includes(selector))
      );
    });

    if (!selectedOption) return;

    $selectLanguage.val(selectedOption.id).trigger("change");
  }

  async function rendered() {
    let timer;
    await new Promise((resolve) => {
      observer();
      function observer() {
        if (typeof CodeMirror == "function") {
          resolve();
        }
        timer = setTimeout(observer, 10);
      }
    });
    clearTimeout(timer);
  }
})();