2023-09-15 00:38:32 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>Random Animal Generator</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Random Animal Generator</h1>
|
|
|
|
<p id="animal-name">Click a button to get a random animal:</p>
|
2023-09-15 01:35:41 +00:00
|
|
|
<button id="isCritterButton">Is Critter</button>
|
|
|
|
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
|
2023-09-15 00:38:32 +00:00
|
|
|
|
|
|
|
<script>
|
2023-09-15 01:35:41 +00:00
|
|
|
// Function to generate a random session ID
|
|
|
|
function generateSessionId() {
|
|
|
|
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
|
|
}
|
|
|
|
|
2023-09-15 00:38:32 +00:00
|
|
|
// Function to fetch a random animal name from the server
|
|
|
|
async function getRandomAnimal() {
|
|
|
|
try {
|
|
|
|
const response = await fetch('/getRandomAnimal');
|
|
|
|
const data = await response.json();
|
|
|
|
document.getElementById('animal-name').textContent = data.animalName;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching data:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:35:41 +00:00
|
|
|
// Function to set or retrieve the session ID cookie
|
|
|
|
function getSessionId() {
|
|
|
|
const sessionId = document.cookie.replace(/(?:(?:^|.*;\s*)sessionId\s*=\s*([^;]*).*$)|^.*$/, '$1');
|
|
|
|
if (!sessionId) {
|
|
|
|
const newSessionId = generateSessionId();
|
|
|
|
document.cookie = `sessionId=${newSessionId}`;
|
|
|
|
return newSessionId;
|
|
|
|
}
|
|
|
|
return sessionId;
|
|
|
|
}
|
|
|
|
|
2023-09-15 00:38:32 +00:00
|
|
|
// Add click event listeners to the buttons
|
2023-09-15 01:35:41 +00:00
|
|
|
document.getElementById('isCritterButton').addEventListener('click', () => {
|
|
|
|
recordButtonClick('is critter', getSessionId());
|
2023-09-15 00:38:32 +00:00
|
|
|
});
|
|
|
|
|
2023-09-15 01:35:41 +00:00
|
|
|
document.getElementById('isNotCritterButton').addEventListener('click', () => {
|
|
|
|
recordButtonClick('is not critter', getSessionId());
|
2023-09-15 00:38:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Function to record button clicks on the server
|
2023-09-15 01:35:41 +00:00
|
|
|
async function recordButtonClick(buttonName, sessionId) {
|
2023-09-15 00:38:32 +00:00
|
|
|
try {
|
|
|
|
await fetch('/recordButtonClick', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2023-09-15 01:35:41 +00:00
|
|
|
body: JSON.stringify({ buttonName, sessionId }),
|
2023-09-15 00:38:32 +00:00
|
|
|
});
|
|
|
|
getRandomAnimal(); // Load another random animal
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error recording button click:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial random animal load
|
|
|
|
getRandomAnimal();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|