Created
February 1, 2024 18:56
-
-
Save ryan-nauman/eba62da2e2a2eb854662675463540646 to your computer and use it in GitHub Desktop.
script kit presentation timer
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
// Name: Presentation Timer | |
import '@johnlindquist/kit'; | |
import parse from 'parse-duration'; | |
let timer = await arg( | |
'Enter duration for timer (e.g. 3 [minutes], 90s, 15m30s)', | |
); | |
if (/^\d+$/.test(timer)) { | |
timer += 'm'; | |
} | |
let parsedMs = parse(timer); | |
let COUNTDOWN_ID = 'the-final-countdown'; | |
let js = ` | |
(() => { | |
let countdownMs = ${parsedMs} + 2000; // 2000ms buffer for script to start | |
let iframeElement = document.querySelector('iframe.punch-present-iframe'); | |
let existingCountdown = document.getElementById('${COUNTDOWN_ID}'); | |
if (existingCountdown) { | |
existingCountdown.parentNode.removeChild(existingCountdown); | |
} | |
if (iframeElement) { | |
let existingCountdownInIframe = iframeElement.contentDocument.getElementById('${COUNTDOWN_ID}'); | |
if (existingCountdownInIframe) { | |
existingCountdownInIframe.parentNode.removeChild(existingCountdownInIframe); | |
} | |
} | |
let countdownElement = document.createElement('div'); | |
countdownElement.id = '${COUNTDOWN_ID}'; | |
if (iframeElement) { | |
iframeElement.contentDocument.body.appendChild(countdownElement); | |
} else { | |
document.body.appendChild(countdownElement); | |
} | |
countdownElement.style.fontSize = '2em'; | |
countdownElement.style.fontFamily = 'monospace'; | |
countdownElement.style.color = 'white'; | |
countdownElement.style.textShadow = '1px 1px 1px black'; | |
countdownElement.style.position = 'absolute'; | |
countdownElement.style.top = '0'; | |
countdownElement.style.right = '0'; | |
countdownElement.style.padding = '10px'; | |
countdownElement.style.zIndex = '999999999'; | |
// Set the date we're counting down to | |
let countDownDate = new Date().getTime() + countdownMs; | |
if (countdownMs > 0) { | |
let countdownInterval = setInterval(function () { | |
let now = new Date().getTime(); | |
let distance = countDownDate - now; | |
let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | |
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | |
let seconds = Math.floor((distance % (1000 * 60)) / 1000); | |
if (distance > 0) { | |
if (distance <= 60000) { | |
// If below 1 minute, change text to red | |
countdownElement.style.color = 'red'; | |
} | |
let countdownText = ''; | |
if (hours > 0) { | |
countdownText += (hours < 10 ? '0' : '') + hours + ':'; | |
} | |
countdownText += (minutes < 10 ? '0' : '') + minutes + ':' | |
+ (seconds < 10 ? '0' : '') + seconds; | |
countdownElement.textContent = countdownText; | |
} else { | |
clearInterval(countdownInterval); | |
countdownElement.textContent = 'TIME IS UP!'; | |
} | |
}, 1000); | |
} | |
})(); | |
`; | |
await applescript(` | |
tell application "Google Chrome" to tell window 1 | |
get execute active tab javascript " | |
${js} | |
" | |
end tell | |
`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment