Last active
June 4, 2023 00:38
-
-
Save minoki/ca14e78062e41112ee3248cf18c9718a to your computer and use it in GitHub Desktop.
AtCoderのシンタックスハイライトをちゃんと言語に応じて動作させるUserScriptです
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name AtCoder Code Prettify | |
// @namespace https://miz-ar.info/ | |
// @include https://atcoder.jp/contests/*/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 PRETTIFY_BASE_URL = "https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/"; | |
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": "ml.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) { | |
return; | |
} | |
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"); | |
return; | |
} | |
let languageAndCompiler = languageTd.firstChild ? languageTd.firstChild.textContent : null; | |
if (!languageAndCompiler) { | |
console.log("language and compiler not found"); | |
return; | |
} | |
let m = languageAndCompiler.match(/([\w+#\- ]+) \(/); | |
if (!m) { | |
console.log("regexp for language did not match"); | |
return; | |
} | |
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); | |
document.body.appendChild(script); | |
} | |
} 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"); | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment