humour-webpage/templates/index.html
Nathan Woodburn a26a7938ad
All checks were successful
Build Docker / BuildImage (push) Successful in 31s
feat: Add backup api for cross site blocks
2024-12-30 19:38:31 +11:00

100 lines
3.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Humour | Nathan.Woodburn/</title>
<link rel="icon" href="/assets/img/favicon.png" type="image/png">
<link rel="stylesheet" href="/assets/css/index.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<script>
function toggleDarkMode() {
const body = document.body;
const icon = document.getElementById('darkModeIcon');
body.classList.toggle('light-mode');
const isLightMode = body.classList.contains('light-mode');
// Toggle icons
icon.classList.toggle('fa-moon', !isLightMode);
icon.classList.toggle('fa-sun', isLightMode);
// Save mode to localStorage
localStorage.setItem('theme', isLightMode ? 'light' : 'dark');
}
window.onload = () => {
const savedTheme = localStorage.getItem('theme');
const body = document.body;
const icon = document.getElementById('darkModeIcon');
// Apply saved theme or default to dark mode
if (savedTheme === 'light') {
body.classList.add('light-mode');
icon.classList.add('fa-sun');
icon.classList.remove('fa-moon');
} else {
body.classList.remove('light-mode');
icon.classList.add('fa-moon');
icon.classList.remove('fa-sun');
}
};
async function fetchJoke() {
try {
let response;
// Attempt to fetch the external API first
try {
response = await fetch('https://icanhazdadjoke.com/', {
headers: {
'Accept': 'application/json'
}
});
// If the external API response is not ok, fallback to local API
if (!response.ok) {
throw new Error('External API failed');
}
} catch (error) {
console.warn('External API failed:', error);
// Fall back to local API after external API failure
response = await fetch('/api/joke');
// If local API also fails, throw an error
if (!response.ok) {
throw new Error('Both external and local APIs failed');
}
}
// Parse and display the joke
const data = await response.json();
document.getElementById('joke').innerText = data.joke;
} catch (error) {
console.error('Failed to fetch joke:', error);
document.getElementById('joke').innerText = "Oops! Couldn't fetch a joke right now.";
}
}
</script>
</head>
<body>
<button id="darkModeToggle" onclick="toggleDarkMode()">
<i id="darkModeIcon" class="fas fa-moon"></i>
</button>
<div class="spacer"></div>
<div class="centre">
<h1>.Humour</h1>
<p id="joke"></p>
<button onclick="fetchJoke()">Get a joke</button>
<script>
fetchJoke();
</script>
</div>
</body>
</html>