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
export default async function handler(request: Request) {
if (request.method === "GET") {
return new Response(
`<!DOCTYPE html>
<html>
<head>
<title>Dream Interpreter</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.0.3/dist/tailwind.min.css" rel="stylesheet">
<script>
function handleSubmit(event) {
event.preventDefault();
var dreamInput = document.getElementById('dreamInput');
var dream = dreamInput.value;
var interpretationDiv = document.getElementById('interpretation');
var loadingDiv = document.getElementById('loading');
interpretationDiv.textContent = '';
loadingDiv.style.display = 'block';
fetch(window.location.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ dream }),
})
.then(response => response.json())
.then(data => {
interpretationDiv.textContent = data.dream;
})
.catch(error => {
console.error('Error:', error);
})
.finally(() => {
loadingDiv.style.display = 'none';
});
}
</script>
<style>
body {
background-color: #1a1a2e;
color: #e0e1dd;
}
.bg-purple-500 {
background-color: #6a4c93;
}
#loading {
display: none;
}
input{
color: black;
}
</style>
</head>
<body class="p-4">
<h1>Dream Interpreter</h1>
<form onsubmit="handleSubmit(event)">
<input id="dreamInput" type="text" placeholder="Enter your dream" class="border-2 border-gray-200 p-2 rounded-md w-full" />
<button type="submit" class="bg-purple-500 text-white p-2 rounded-md mt-2">Interpret Dream</button>
</form>
<div id="loading" class="mt-4">Loading...</div>
<div id="interpretation" class="mt-4"></div>
</body>
</html>`,
{
headers: {
"Content-Type": "text/html",
"Cache-Control": "public, max-age=10, stale-while-revalidate=0",
},
},
);
} else if (request.method === "POST") {
try {
const body = await request.json();
const dream = body.dream;
// Generate a random UUID
const randomUuid = () => {
let uuid = "";
for (let i = 0; i < 32; i++) {
const random = Math.random() * 16 | 0;
if (i === 8 || i === 12 || i === 16 || i === 20) {
uuid += "-";
}
uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16);
}
return uuid;
};
// Create the request payload
const payload = {
dream,
lat: null,
long: null,
};
// Send the POST request
const response = await fetch("https://dreaminterpreteraibackend.com/dream", {
method: "POST",
headers: {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
March 24, 2024
feel free to fork and play around :)