From 9d34135c25385b7a79acbcde5b9ba7f76a62471d Mon Sep 17 00:00:00 2001
From: Runxi Yu <me@runxiyu.org>
Date: Mon, 24 Mar 2025 21:13:52 +0800
Subject: Separate main.js out of the HTML

---
 static/main.js | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 static/main.js

(limited to 'static/main.js')

diff --git a/static/main.js b/static/main.js
new file mode 100644
index 0000000..73d98df
--- /dev/null
+++ b/static/main.js
@@ -0,0 +1,25 @@
+document.addEventListener("DOMContentLoaded", async function() {
+	let challenge_b64 = document.body.dataset.identifier;
+	let difficulty = parseInt(document.body.dataset.difficulty);
+	let form = document.querySelector("form");
+	let field = form.querySelector("input[name='powxy']");
+	let status_el = document.getElementById("solver_status");
+
+	let identifier_bytes = Uint8Array.from(
+		atob(challenge_b64),
+		ch => ch.charCodeAt(0)
+	);
+
+	status_el.textContent = "Starting WebAssembly solver as a worker...";
+
+	let worker = new Worker("/.powxy/static/solver.js");
+
+	worker.onmessage = function(e) {
+		let { nonce, nonce_bytes } = e.data;
+		let nonce_str = String.fromCharCode(...nonce_bytes);
+		field.value = btoa(nonce_str);
+		status_el.textContent = "Challenge solved automatically in " + nonce + " iterations";
+	};
+
+	worker.postMessage({ identifier_bytes, difficulty });
+});
-- 
cgit v1.2.3