express: WIP
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
5c1b0ce6ff
commit
d5c43d0624
5 changed files with 175 additions and 17 deletions
|
@ -7,10 +7,15 @@
|
|||
<body>
|
||||
<h1>Random Animal Generator</h1>
|
||||
<p id="animal-name">Click a button to get a random animal:</p>
|
||||
<button id="button1">Button 1</button>
|
||||
<button id="button2">Button 2</button>
|
||||
<button id="isCritterButton">Is Critter</button>
|
||||
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
|
||||
|
||||
<script>
|
||||
// Function to generate a random session ID
|
||||
function generateSessionId() {
|
||||
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
|
||||
// Function to fetch a random animal name from the server
|
||||
async function getRandomAnimal() {
|
||||
try {
|
||||
|
@ -22,24 +27,35 @@
|
|||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Add click event listeners to the buttons
|
||||
document.getElementById('button1').addEventListener('click', () => {
|
||||
recordButtonClick('Button 1');
|
||||
document.getElementById('isCritterButton').addEventListener('click', () => {
|
||||
recordButtonClick('is critter', getSessionId());
|
||||
});
|
||||
|
||||
document.getElementById('button2').addEventListener('click', () => {
|
||||
recordButtonClick('Button 2');
|
||||
document.getElementById('isNotCritterButton').addEventListener('click', () => {
|
||||
recordButtonClick('is not critter', getSessionId());
|
||||
});
|
||||
|
||||
// Function to record button clicks on the server
|
||||
async function recordButtonClick(buttonName) {
|
||||
async function recordButtonClick(buttonName, sessionId) {
|
||||
try {
|
||||
await fetch('/recordButtonClick', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ buttonName }),
|
||||
body: JSON.stringify({ buttonName, sessionId }),
|
||||
});
|
||||
getRandomAnimal(); // Load another random animal
|
||||
} catch (error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue