From b619e6acb5533c8a2fd5b70fd1e02a588f4778b6 Mon Sep 17 00:00:00 2001 From: Josh Patra <30350506+SoPat712@users.noreply.github.com> Date: Sun, 20 Apr 2025 13:56:25 -0400 Subject: [PATCH] SC widget api preloaded? --- src/lib/HeardleGame.svelte | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/lib/HeardleGame.svelte b/src/lib/HeardleGame.svelte index 5012440..165a80a 100644 --- a/src/lib/HeardleGame.svelte +++ b/src/lib/HeardleGame.svelte @@ -158,7 +158,7 @@ let iframeElement: HTMLIFrameElement; let widget: any; let widgetReady = false; - let loading = true; // ← disable play until preload + let loading = true; // ← stay disabled until probe finishes let artworkUrl = ''; let isPlaying = false; let currentPosition = 0; @@ -225,23 +225,25 @@ } onMount(() => { + // dark‑mode listener window .matchMedia('(prefers-color-scheme: dark)') .addEventListener('change', (e) => (darkMode = e.matches)); + + // countdown updateTime(); countdownInterval = setInterval(updateTime, 1000); + // SoundCloud widget widget = SC.Widget(iframeElement); - // bind READY right away, then “warm up” the widget after a short delay + // bind READY immediately widget.bind(SC.Widget.Events.READY, () => { - // grab duration & artwork widget.getDuration((d: number) => (fullDuration = d)); widget.getCurrentSound((sound: any) => { artworkUrl = sound.artwork_url || ''; }); - - // wait 2s for Netlify load, then probe + // warm‑up probe after 1 s setTimeout(() => { widget.play(); widget.pause(); @@ -308,24 +310,19 @@ } function submitGuess() { - if (!widgetReady || gameOver) return; + if (!widgetReady || gameOver || !userInput) return; if (!selectedTrack && suggestions.length) { selectedTrack = suggestions.find((t) => t.title.toLowerCase() === userInput.toLowerCase()) || suggestions[0]; } if (!selectedTrack) return; - attemptCount++; const ans = currentTrack.title.toLowerCase(); if (selectedTrack.title.toLowerCase() === ans) { attemptInfos = [...attemptInfos, { status: 'correct', title: currentTrack.title }]; gameOver = true; - message = `✅ Correct! It was “${currentTrack.title}.” You ${ - attemptCount === maxAttempts - ? 'nailed it on the last try!' - : `got it in ${attemptCount} ${attemptCount === 1 ? 'try' : 'tries'}.` - }`; + message = `✅ Correct! It was “${currentTrack.title}.” You got it in ${attemptCount} ${attemptCount === 1 ? 'try' : 'tries'}.`; widget.pause(); } else { attemptInfos = [...attemptInfos, { status: 'wrong', title: selectedTrack.title }]; @@ -361,6 +358,12 @@ : []; + + + + + + {#if showHowTo}