moth/theme/puzzle.html

103 lines
3.0 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Puzzle</title>
<link rel="stylesheet" href="basic.css">
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<script src="devel.js"></script>
<script>
function init() {
let params = new URLSearchParams(window.location.search);
let categoryName = params.get("cat");
let points = params.get("points");
let puzzleId = params.get("pid");
let puzzle = document.getElementById("puzzle");
let base = "content/" + categoryName + "/" + puzzleId + "/";
fetch(base + "puzzle.json")
.then(resp => {
return resp.json();
})
.then(obj => {
// Populate authors
document.getElementById("authors").textContent = obj.authors.join(", ");
// If answers are provided, this is the devel server
if (obj.answers) {
devel_addin(obj, document.getElementById("devel"));
}
// Load scripts
for (let script of obj.scripts) {
let st = document.createElement("script");
document.head.appendChild(st);
st.src = base + script;
}
// List associated files
for (let fn of obj.files) {
let li = document.createElement("li");
let a = document.createElement("a");
a.href = base + fn;
a.innerText = fn;
li.appendChild(a);
document.getElementById("files").appendChild(li);
}
// Prefix `base` to relative URLs in the puzzle body
let doc = new DOMParser().parseFromString(obj.body, "text/html");
for (let se of doc.querySelectorAll("[src],[href]")) {
se.outerHTML = se.outerHTML.replace(/(src|href)="([^/]+)"/i, "$1=\"" + base + "$2\"");
}
// Replace puzzle children with what's in `doc`
Array.from(puzzle.childNodes).map(e => e.remove());
Array.from(doc.body.childNodes).map(e => puzzle.appendChild(e));
})
.catch(err => {
// Show error to the user
Array.from(puzzle.childNodes).map(e => e.remove());
let p = document.createElement("p");
puzzle.appendChild(p);
p.classList.add("Error");
p.textContent = err;
});
document.title = categoryName + " " + points;
document.querySelector("body > h1").innerText = document.title;
document.querySelector("input[name=cat]").value = categoryName;
document.querySelector("input[name=points]").value = points;
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
</script>
</head>
<body>
<h1>Puzzle</h1>
<section>
<div id="puzzle">Loading...</div>
<ul id="files"></ul>
<p>Puzzle by <span id="authors"></span></p>
</section>
<form action="answer" method="post">
<input type="hidden" name="cat">
<input type="hidden" name="points">
Team ID: <input type="text" name="id"> <br>
Answer: <input type="text" name="answer" id="answer"> <br>
<input type="submit" value="Submit">
</form>
<div id="devel"></div>
<nav>
<ul>
<li><a href="puzzle-list.html">Puzzles</a></li>
<li><a href="scoreboard.html">Scoreboard</a></li>
</ul>
</nav>
</body>
</html>