simplesites/timer/index.html
2025-11-22 21:53:03 -06:00

267 lines
7.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" />
<title>Countdown Timer</title>
<meta property="og:title" content="Countdown Timer" />
<meta property="og:image" content="http://shots.wayl.one/shot/?url=https://timer.wayl.one/&height=630&width=1200&scaled_width=1200&scaled_height=630&selectors=" />
<meta property="og:description" content="A simple large display countdown timer, its just counts down" />
<meta property="og:url" content="https://timer.wayl.one/" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="Countdown Timer" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="Countdown Timer" />
<meta name="twitter:description" content="A simple large display countdown timer, its just counts down" />
<meta name="twitter:image" content="http://shots.wayl.one/shot/?url=https://timer.wayl.one/&height=630&width=1200&scaled_width=1200&scaled_height=640&selectors=" />
<style>
html, body {
margin: 0;
padding: 0;
background: black;
color: lime;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: monospace;
user-select: none;
}
#title {
font-size: 4vw;
margin-bottom: 2vh;
padding: 2rem 0;
outline: none;
text-align: center;
min-height: 4vw;
width: 100%;
}
#countdown {
font-size: 20vw;
padding: 0 5vw;
text-align: center;
white-space: nowrap;
cursor: pointer;
}
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.75);
display: none;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal {
box-shadow: 0 0 3rem 1rem #00ff0017;
background: black;
border: 2px solid lime;
border-radius: 0.5rem;
padding: 2rem;
max-width: 90%;
color: lime;
text-align: left;
font-size: 1.5rem;
}
.modal h2 {
margin-top: 0;
color: #008e00;
}
.modal ul {
color: #00ae00;
}
</style>
</head>
<body>
<div id="title" contenteditable="true"></div>
<div id="countdown" contenteditable="true">00:05:00</div>
<!-- Modal -->
<dialog id="modal-overlay" class="modal-overlay">
<div class="modal">
<h2>Help</h2>
<p>
It's just a countdown timer, it counts down.
</p>
<p>
Use query parameters to change the time ex. ?hr=5&min=30&sec=0
</p>
<p>
Start typing to give it a title.
</p>
<p>
Copy with <kbd>Ctrl</kbd> + <kbd>c</kbd>
</p>
</div>
</dialog>
<script>
const urlParams = new URLSearchParams(window.location.search);
const title = urlParams.get('title');
const title_el = document.getElementById('title')
if (title) {
title_el.textContent = title;
}
const countdownEl = document.getElementById("countdown");
let endTime = null;
let timerRunning = false;
let wakeLock = null;
function parseTime(text) {
const parts = text.split(":").map(p => parseInt(p, 10));
if (parts.length === 3) {
const [h, m, s] = parts;
return (h * 3600 + m * 60 + s) * 1000;
} else if (parts.length === 2) {
const [m, s] = parts;
return (m * 60 + s) * 1000;
}
return 0;
}
function formatTime(ms) {
let totalSeconds = Math.floor(ms / 1000);
const h = Math.floor(totalSeconds / 3600);
const m = Math.floor((totalSeconds % 3600) / 60);
const s = totalSeconds % 60;
return (
String(h).padStart(2, "0") + ":" +
String(m).padStart(2, "0") + ":" +
String(s).padStart(2, "0")
);
}
function startCountdown(durationMs) {
const now = Date.now();
endTime = now + durationMs;
timerRunning = true;
updateCountdown();
}
function updateCountdown() {
if (!timerRunning) return;
const now = Date.now();
const remaining = endTime - now;
if (remaining <= 0) {
countdownEl.textContent = "00:00:00";
timerRunning = false;
return;
}
countdownEl.textContent = formatTime(remaining);
requestAnimationFrame(updateCountdown);
}
countdownEl.addEventListener("blur", () => {
const text = countdownEl.textContent.trim();
const ms = parseTime(text);
if (ms > 0) {
startCountdown(ms);
}
});
countdownEl.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
countdownEl.blur();
e.preventDefault();
}
});
// ---- Modal logic ----
const overlay = document.getElementById('modal-overlay');
function toggleModal() {
if (overlay.style.display === 'none') {
overlay.style.display = 'flex';
} else {
overlay.style.display = 'none';
}
}
function hideModal() {
overlay.style.display = 'none';
}
document.addEventListener('keydown', e => {
if (e.key === 'c' && e.ctrlKey) {
const time = countdownEl.textContent;
navigator.clipboard.writeText(time);
} else if (e.key === 'Enter' && !(document.activeElement === title_el)) {
// toggleTimer();
} else if (e.key === 'Escape' && document.activeElement === title_el) {
title_el.blur();
} else if (e.key === 'Escape') {
hideModal();
} else if (e.code === 'Space' && !(document.activeElement === title_el)) {
// toggleTimer();
} else if (e.key === '?' && !title_el.isFocused) {
toggleModal();
} else if (e.key === '?') {
} else if (e.keyCode === 191) {
} else if (e.ctrlKey && e.key === 'a'){
title_el.focus();
} else if (e.ctrlKey){
} else if (e.shiftKey){
} else if (overlay.style.display === 'flex') {
} else {
title_el.focus();
}
})
// on change of title update query params
title_el.addEventListener('input', () => {
const new_title = title_el.textContent;
const url = new URL(window.location);
url.searchParams.set('title', new_title);
window.history.replaceState({}, '', url);
});
// Support query params (e.g. ?min=3&sec=30)
function getInitialTimeFromQuery(urlParams) {
const hr = parseInt(urlParams.get("hr") || "0", 10);
const min = parseInt(urlParams.get("min") || "0", 10);
const sec = parseInt(urlParams.get("sec") || "0", 10);
const totalMs = (hr * 3600 + min * 60 + sec) * 1000;
return totalMs > 0 ? totalMs : 5 * 60 * 1000; // default to 5 min
}
// Keep screen awake
async function requestWakeLock() {
try {
if ("wakeLock" in navigator) {
wakeLock = await navigator.wakeLock.request("screen");
console.log("Wake lock acquired");
}
} catch (err) {
console.warn("Wake lock not available:", err);
}
}
document.addEventListener("visibilitychange", () => {
if (wakeLock && document.visibilityState === "visible") {
requestWakeLock();
}
});
const initialMs = getInitialTimeFromQuery(urlParams);
countdownEl.textContent = formatTime(initialMs);
startCountdown(initialMs);
requestWakeLock();
</script>
</body>
</html>