diff --git a/src/lib/HeardleGame.svelte b/src/lib/HeardleGame.svelte index 165a80a..d09999c 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; // ← stay disabled until probe finishes + let loading = true; // disable play until we warm up the widget let artworkUrl = ''; let isPlaying = false; let currentPosition = 0; @@ -224,33 +224,43 @@ clearTimeout(snippetTimeout); } - onMount(() => { + onMount(async () => { + // dynamically load SC API if needed + if (typeof window.SC === 'undefined') { + await new Promise((resolve, reject) => { + const tag = document.createElement('script'); + tag.src = 'https://w.soundcloud.com/player/api.js'; + tag.async = true; + tag.onload = () => resolve(); + tag.onerror = () => reject(new Error('Failed to load SoundCloud API')); + document.head.appendChild(tag); + }); + } + // 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 immediately widget.bind(SC.Widget.Events.READY, () => { + // grab duration & artwork widget.getDuration((d: number) => (fullDuration = d)); widget.getCurrentSound((sound: any) => { artworkUrl = sound.artwork_url || ''; }); - // warm‑up probe after 1 s + // warm up on Netlify setTimeout(() => { widget.play(); widget.pause(); widget.seekTo(0); loading = false; widgetReady = true; - }, 1000); + }, 2000); }); widget.bind(SC.Widget.Events.PLAY, () => { @@ -299,6 +309,10 @@ isPlaying ? widget.pause() : playSegment(); } + function toggleDark() { + darkMode = !darkMode; + } + function skipIntro() { if (!widgetReady || gameOver) return; attemptInfos = [...attemptInfos, { status: 'skip' }]; @@ -317,6 +331,7 @@ suggestions[0]; } if (!selectedTrack) return; + attemptCount++; const ans = currentTrack.title.toLowerCase(); if (selectedTrack.title.toLowerCase() === ans) { @@ -358,24 +373,17 @@ : []; - - - - - - {#if showHowTo}
-

How to Play

+

How to Play

  • 🎵 Play the snippet.
  • 🔊 Skips & wrongs unlock more.
  • @@ -383,10 +391,7 @@