Created
February 13, 2023 16:41
-
-
Save jcward/f3ff10e97c1786afc8ee83496b8f3270 to your computer and use it in GitHub Desktop.
Put a timer in the corner of nearly any webpage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Click to (re)start, shift-click to increment, ctrl-click to decrement | |
// Customize the initial time (currently 60 seconds), size, and position | |
// Copy-paste into dev tools (CTRL-SHIFT-i) | |
(function() { | |
var init_time = 60, size = 1.5, position = "top right"; | |
var t = init_time; | |
var e = document.createElement('div'); | |
e.style.position = "fixed"; | |
position = position.split(" "); | |
e.style[position[0]] = "5px"; | |
e.style[position[1]] = "5px"; | |
e.style.zIndex = "2147483646"; | |
e.style.backgroundColor = "#294"; | |
e.style.padding = "0.3px 0.5em"; | |
e.style.borderRadius = "4px"; | |
e.style.fontSize = size+"em"; | |
e.style.color = "#fff"; | |
e.style.minWidth = "2em"; | |
e.style.textAlign = "center"; | |
e.style.cursor = "pointer"; | |
document.body.appendChild(e); | |
function redraw() { | |
e.innerHTML = (Math.floor(t/60)+":"+(t%60)).replace(/:(\d)$/, ":0$1"); | |
e.style.backgroundColor = t>=10 ? "#294" : "#932"; | |
} | |
redraw(); | |
var ii = -1; | |
function reset(ev) { | |
if (ev.shiftKey) { | |
t = Math.ceil(t/15+0.1)*15; | |
} else if (ev.ctrlKey) { | |
t = Math.floor(t/15-0.1)*15; | |
} else { | |
t = init_time; | |
} | |
redraw(); | |
if (ii>=0) window.clearInterval(ii); | |
ii = window.setInterval(function() { | |
if (t>0) { | |
t--; | |
} else { | |
window.clearInterval(ii); | |
} | |
redraw(); | |
}, 1000); | |
return false; | |
} | |
e.onmousedown = reset; | |
e.ontouchstart = reset; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment