Skip to content

Instantly share code, notes, and snippets.

@0mg
Last active September 24, 2015 11:47
Show Gist options
  • Select an option

  • Save 0mg/743149 to your computer and use it in GitHub Desktop.

Select an option

Save 0mg/743149 to your computer and use it in GitHub Desktop.
0mg's anagolf setting
// ==UserScript==
// @include http://golf.shinh.org/*
// ==/UserScript==
addEventListener("DOMNodeInserted", function fn(e) {
/* set default language */
var lang = document.getElementsByName("ext")[0];
if (lang) {
lang.value = "js";
removeEventListener(e.type, fn);
}
});
addEventListener("DOMContentLoaded", function() {
/* set default my name */
var myname = "0mg";
var namebox = document.getElementsByName("user")[0];
if (namebox) namebox.value = myname;
/* set statistics */
var stat = document.getElementsByName("reveal")[0];
if (stat) stat.checked = true;
});
addEventListener("submit", function(e) {
/* replace CRLF to LF in textarea[name=code] */
var code = document.querySelector("[name=code]");
if (!code || code.value.indexOf("\n") === -1) return;
var xhr = new XMLHttpRequest;
var fd = new FormData;
var qs = document.querySelectorAll("form *[name]");
[].forEach.call(qs, function(e) {
fd.append(e.name,
e.name === "code" ? e.value.replace(/\r\n/g, "\n"): e.value);
});
xhr.open("post", "submit.rb", true);
xhr.addEventListener("load", function() {
document.open();
document.write(xhr.responseText);
document.close();
});
xhr.send(fd);
var timer = setInterval(function() {
if (xhr.readyState < 4) {
if (!confirm("continue?")) {
xhr.abort();
clearInterval(timer);
}
} else {
clearInterval(timer);
}
}, 5000);
e.preventDefault();
});
if (location.href.indexOf("http://golf.shinh.org/l.rb?") === 0) {
/* show only (post mortem) Results by a language */
addEventListener("DOMContentLoaded", function() {
/* add CSS <style> */
var style = document.createElement("style");
style.textContent = ".endless,.active{display:none}";
document.head.appendChild(style);
/* add <button> for toggle CSS */
var btn = document.createElement("button");
btn.dataset.postMortem = 1;
btn.textContent = "post mortem <-> all";
btn.addEventListener("click", function() {
if (btn.dataset.postMortem ^= 1) {
style.textContent = ".endless,.active{display:none}";
} else {
style.textContent = "";
}
});
document.querySelector("#ldata").appendChild(btn);
/* wrap {h2 + table}s with <section>s */
var h2s = document.querySelectorAll("#ldata h2");
[].forEach.call(h2s, function(h2) {
var section = document.createElement("section");
var ranking = h2.nextSibling;
document.querySelector("#ldata").appendChild(section);
section.classList.add(
h2.lastChild.nodeValue === " (endless)" ? "endless" :
h2.lastChild.nodeValue === " (post mortem)" ? "postMortem" : "active"
);
section.appendChild(h2);
section.appendChild(ranking);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment