Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// ==UserScript==
// @name AtCoder Code Prettify
// @namespace
// @include*/submissions/*
// @version 3
// @grant none
// @run-at document-start
// ==/UserScript==
console.debug("AtCoder Code Prettify is running");
const getElementByXPath = function getElementByXPath(x,root) {
let r = document.evaluate(x,root||document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
return r.snapshotItem(0);
const INCLUDED_BY_DEFAULT = ["c", "cpp", "java", "cs", "m", "sh", "py", "pl", "rb", "js"];
const NAME_TO_EXT = {
// Non-default languages:
"C": "c",
"C++": "cpp",
"Java": "java",
"Python": "py",
"Bash": "sh",
"bc": null,
"Awk": null,
"C#": "cs",
"Clojure": "clj",
"Crystal": null,
"D": null,
"Dart": "dart",
"dc": null,
"Erlang": "erlang",
"Elixir": "ex",
"F#": "ml.fs",
"Forth": null,
"Fortran": null,
"Go": "go",
"Haskell": "hs",
"Haxe": null,
"JavaScript": "js",
"Julia": null,
"Kotlin": "kotlin",
"Lua": "lua",
"Dash": "sh",
"Nim": null,
"Common Lisp": "cl",
"Objective-C": "m",
"OCaml": "ml",
"Octave": null,
"Pascal": "pascal",
"Perl": "pl",
"Raku": null,
"PHP": null,
"Prolog": null,
"PyPy2": "py",
"PyPy3": "py",
"Racket": "lisp.rkt",
"Ruby": "rb",
"Rust": "rust",
"Scala": "scala",
"Scheme": "lisp.scm",
"Standard ML": "", // Re-using OCaml
"Swift": "swift",
"Text": null, // not needed
"TypeScript": "js",
"Visual Basic": "vb",
"Zsh": "sh",
"COBOL - Fixed": null,
"COBOL - Free": null,
"Brainfuck": null,
"Ada2012": null,
"Unlambda": null,
"Cython": "py",
"Sed": null,
"Vim": null,
const main = () => {
let submissionCode = document.getElementById("submission-code");
if (!submissionCode) {
try {
let languageTd = getElementByXPath("/html/body/div[3]/div/div[1]/div[2]/div[3]/table/tbody/tr[4]/td");
if (!languageTd) {
console.log("<td> not found");
let languageAndCompiler = languageTd.firstChild ? languageTd.firstChild.textContent : null;
if (!languageAndCompiler) {
console.log("language and compiler not found");
let m = languageAndCompiler.match(/([\w+#\- ]+) \(/);
if (!m) {
console.log("regexp for language did not match");
let language = m[1];
let mext = NAME_TO_EXT[language];
if (mext) {
let mod_ext = mext.match(/^\w+\.\w+$/);
let mod, ext;
if (mod_ext) {
mod = mod_ext[1];
ext = mod_ext[2];
} else {
mod = ext = mext;
submissionCode.classList.add("lang-" + ext);
if (INCLUDED_BY_DEFAULT.indexOf(ext) === -1) {
let script = document.createElement("script");
script.src = `${PRETTIFY_BASE_URL}lang-${mod}.js`;
console.log("AtCoder Prettify: Loading ", script.src);
} else {
console.log(`AtCoder Prettify: ${language} is not supported.`);
} catch (e) {
console.error("AtCoder Prettify:", e);
if (document.readyState === "loading") {
console.debug("AtCoder Code Prettify: Setting DOMContentLoaded");
window.addEventListener("DOMContentLoaded", main);
} else {
console.debug("AtCoder Code Prettify: Running immediately");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.