aboutsummaryrefslogtreecommitdiff
path: root/html/wasm.html
blob: 197836ff945d86f2d426cb92a0c023db267b92df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!doctype html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Copyright 2025 Florian Fischer
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
	<!-- <title>Go wasm</title> -->
	<script src="eruda.js"></script>
	<script>eruda.init();</script>
	<script src="js/wasm_exec.js"></script>
	<style>
.loader {
	border: 16px solid #f3f3f3;
	border-top: 16px solid;
	border-radius: 50%;
	width: 120px;
	height: 120px;
	animation: spin 2s linear infinite;
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}

.container {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
}
	</style>
</head>

<body>
	<div style="width:0; height:0; overflow:hidden;">
	<input id="hiddenInput"></input>
	</div>
	<div class="container" id="loader">
		<div class="loader"></div>
		<div id="url"></div>
	</div>

	<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
	WebAssembly.instantiateStreaming = async (resp, importObject) => {
		const source = await (await resp).arrayBuffer();
		return await WebAssembly.instantiate(source, importObject);
	};
}

// Hook the console log to send debug information
// See: https://stackoverflow.com/questions/19846078/how-to-read-from-chromes-console-in-javascript#19846113
console.oldlog = console.log.bind(console);
console.logs = [];
console.log = function(){
	console.logs.push(Array.from(arguments));
	console.oldlog.apply(console, arguments);
}

function mailLogs() {
	let mailtoLink = new URL("mailto:muhqs-game-logs@muhq.space");
	mailtoLink.searchParams.append("subject", encodeURIComponent("muhqs-game crash report"));
	mailtoLink.searchParams.append("body", encodeURIComponent(console.logs));
	console.log(mailtoLink);

	if (window.confirm('Send a bug report and help to improve muhqs-game.')) {
		window.open(mailtoLink, '_system');
	};
}

console.oldwarn = console.warn.bind(console);
console.warn = function() {
	if (arguments[0] === "exit code:" && arguments[1] != 0) {
		mailLogs();
	}
	console.oldwarn.apply(console, arguments);
}

const go = new Go();
let mod, inst;
let url = new URLSearchParams(window.location.search).get("wasm");
if (!url) {
	url = "client.wasm";
}
document.getElementById("url").innerHTML = url;

WebAssembly.instantiateStreaming(fetch(url), go.importObject).then((result) => {
	mod = result.module;
	inst = result.instance;
	let loader = document.getElementById("loader");
	loader.remove();
	go.run(inst);
}).catch((err) => {
	console.error(err);
});
	</script>
</body>
</html>