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
function header(input?: Request | string | undefined): string {
let value = "";
if (typeof input === "string") {
value = input;
}
return `<div style="width:100%;background-color:#fff;display:flex;align-items:center;justify-content:space-between;margin:4px;">
<div style="display:flex;flex-grow:1;margin-right:16px;">
<input value="${value}" style="font-size:1.2rem;padding:8px;border-radius:8px;border:1px solid #ccc;flex-grow:1;margin-right:8px;" type="text" id="inputField" placeholder="Input">
<button style="font-size:1.2rem;padding:8px 16px;border-radius:8px;border:none;background-color:#000;color:white;cursor:pointer;white-space:nowrap;" onclick="go()">Go</button>
</div>
<div style="text-align:right;background-color:#000;padding:4px;border-radius:4px;">
<a href="https://www.substrate.run" target="_blank" style="font-size:0.8rem;color:#fff;text-decoration:none;display:block;">made with Substrate</a>
<a href="#" onclick="window.open(getSourceUrl(), '_blank')" style="font-size:0.8rem;color:#fff;text-decoration:none;display:block;">view source on val.town</a>
</div>
<script>
function getSourceUrl() {
const currentUrl = window.location.href;
const match = currentUrl.match(/^https:\\/\\/([\\w-]+)\\.web\\.val\\.run/);
if (match) {
const username = match[1].split('-')[0];
const appName = match[1].split('-')[1];
return \`https://www.val.town/v/\${username}/\${appName}\`;
}
return '#';
}
function go() {
const input = document.getElementById('inputField').value;
const encodedInput = encodeURIComponent(input);
const link = \`/?input=\${encodedInput}\`;
window.location.href = link;
}
document.getElementById('inputField').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
go();
}
});
</script>
</div>`;
}
function handler(req: Request): Response {
return new Response(header(), {
headers: {
"Content-Type": "text/html",
},
});
}
export default function htmlOrResponse(args?: Request | string | undefined): string {
if (args === undefined || typeof args === "string") return header(args);
// @ts-ignore handle Request for development, but used as a string in practice
if (args instanceof Request) return handler(args);
throw new Error("bad args");
}