Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
const title = "Text Transformer";
const html = (url: string) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
textarea {
width: 100%;
height: 300px;
margin-top: 10px;
}
button {
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>${title}</h1>
<textarea id="inputText"></textarea>
<button id="transformButton">Transform</button>
<script>
async function transform() {
const textarea = document.getElementById('inputText');
const text = textarea.value;
try {
const response = await fetch('${url}', {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: text
});
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const data = await response.text();
textarea.value = data;
} catch (error) {
console.error('Error during fetch:', error);
alert('There was an error processing your request.');
}
}
window.addEventListener("keydown", (e) => {
if (e.metaKey && e.key === "Enter") {
e.preventDefault();
transform()
}
});
document.getElementById('transformButton').addEventListener('click', transform);
</script>
</body>
</html>
`;
export default async function(req: Request): Promise<Response> {
if (req.method === "POST") {
const input = await req.text();
return new Response(input.toUpperCase(), {
headers: { "Content-Type": "text/plain" },
});
}
return new Response(html(req.url), {
headers: { "Content-Type": "text/html" },
});
}
chet-texttransformer.web.val.run
June 25, 2024