/*
* @title GCJ の解法をページ内に表示する
* @description 解法をダウンロードすることなくページ内に表示できます。Google Code Jam 公式ページのスコアボード、Code Jam Language Stats の参加者別ページで利用できます。
* @include http://code.google.com/codejam/contest/scoreboard?*
* @include http://www.go-hero.net/jam/*/name/*
* @license MIT License
* @require
*/
function prepareToShow() {
// prepare for google-code-prettify
if (!window.prettyPrint) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js";
document.body.appendChild(script);
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css"
link.href = "http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css";
document.body.appendChild(link);
var style = document.createElement("style");
style.type = "text/css"
style.appendChild(document.createTextNode("pre { word-wrap: break-word; }"));
document.body.appendChild(style);
}
// prepare for zip.js
if (!window.Zip) {
var zipScript = document.createElement("script");
zipScript.type = "text/javascript";
zipScript.src = "https://github.com/hinassan/zipjs/raw/master/zip.js";
document.body.appendChild(zipScript);
var files = document.createElement("ul");
files.id = "files";
files.innerHTML = "<li>(files)</li>";
if (window.GCJ) {
document.getElementById("scb-rank-div").appendChild(files);
} else {
document.getElementById("content").appendChild(files);
}
}
if (window.GCJ) {
var elems = goog$dom$getElementsByTagNameAndClass("img", "scb-views-file", document);
for (var i = 0, elem; elem = elems[i]; i++) {
var td = elem.parentNode;
var tdIndex = getChildIndex(td);
var username = goog$dom$getElementsByTagNameAndClass("td", "scb-player-name", td.parentNode)[0].innerText;
var url =
GCJ.base_url +
"/scoreboard/do?cmd=GetSourceCode&contest=" + GCJ.contestId +
"&problem=" + GCJ.problems[(tdIndex - 5) / 2 | 0].id +
"&io_set_id=" + ((tdIndex - 5) % 2) +
"&username=" + username;
appendShowLink(elem, url);
}
} else {
var elems = document.getElementsByTagName("a");
for (var i = 0, elem; elem = elems[i]; i++) {
if (elem.href.match(/GetSourceCode/)) {
appendShowLink(elem, elem.href);
}
}
}
}
function getChildIndex(target) {
for (var i = 0, elem; elem = target.parentNode.childNodes[i]; i++) {
if (elem == target) {
return i;
}
}
}
function appendShowLink(elem, url) {
var showLink = document.createElement("a");
showLink.href = "#";
showLink.innerHTML = "Show";
showLink.setAttribute("onclick", "showContents(this.url); return false;");
showLink.url = url;
elem.parentNode.appendChild(document.createTextNode(" - "));
elem.parentNode.appendChild(showLink);
}
function showContents(orgUrl) {
var url = isSameOrigin(document.URL, orgUrl)
? orgUrl
: "http://allow-any-origin.appspot.com/" + orgUrl;
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function() {
var bin = xhr.responseText;
var bytes = [];
for (i = 0; i < bin.length; i++) {
bytes[i] = bin.charCodeAt(i) & 0xff;
}
var zip = Zip.inflate(bytes);
var files = document.getElementById("files");
files.innerHTML = "";
// extract inner zip files
for (var i in zip.files) {
var file = zip.files[i];
if (file.name.match(/\.zip$/)) {
bytes = [];
for (var j = 0; j < file.data.length; j++) {
bytes[j] = file.data.charCodeAt(j) & 0xff;
}
var innerZip = Zip.inflate(bytes);
for (var j in innerZip.files) {
zip.files[i + "-" + j] = innerZip.files[j];
}
delete zip.files[i];
}
}
for (var i in zip.files) {
var file = zip.files[i];
var item = document.createElement("li");
item.innerHTML =
"<h2>" + file.name + "</h2>" +
"<pre class='prettyprint'>" +
file.data.replace(/</g, "<") +
"</pre>";
files.appendChild(item);
}
prettyPrint();
};
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.send(null);
}
function isSameOrigin(docUrl, targetUrl) {
if (targetUrl.match(RegExp("^/"))) {
return true;
}
return getOrigin(docUrl) == getOrigin(targetUrl);
}
function getOrigin(url) {
return url.match(RegExp("^https?://[^/]+")).shift();
}
prepareToShow();