diff --git a/js-helper/goofyHistory.js b/js-helper/goofyHistory.js index 37bcad7..d8e31b8 100644 --- a/js-helper/goofyHistory.js +++ b/js-helper/goofyHistory.js @@ -1,20 +1,68 @@ -const unique = new Set(); +// max track buffer for localStorage +// when the limit is reached, old tracks will be removed from the beginning, and new ones will be added to the end +const MAX_TRACKS = 1000; -function goofyHistory(e, urlForm, idBox) { +// max delay between switching tracks (ms) +const MAX_DELAY = 1000; - const uri = e?.item?.uri; +function debounce(func, wait) { + let timeout; + return function executedFunction(...args) { + const later = () => { + clearTimeout(timeout); + func(...args); + }; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + }; +} - if (uri && uri.includes('spotify:track:') && !unique.has(uri)) { +const loadTracksFromStorage = () => { + try { + const savedTracks = localStorage.getItem('sentSpotifyTracks'); + return new Set(savedTracks ? JSON.parse(savedTracks) : []); + } catch (error) { + console.error('Error loading tracks from localStorage:', error); + return new Set(); + } +}; - unique.add(uri); +const saveTracksToStorage = (tracks) => { + try { + let tracksArray = [...tracks]; - fetch(urlForm, { + if (tracksArray.length > MAX_TRACKS) { + tracksArray = tracksArray.slice(-MAX_TRACKS); + } + + localStorage.setItem('sentSpotifyTracks', JSON.stringify(tracksArray)); + } catch (error) { + console.error('Error saving tracks to localStorage:', error); + } +}; + +const unique = loadTracksFromStorage(); + +async function sendToGoogleForm(uri, urlForm, idBox) { + try { + await fetch(urlForm, { "headers": { "content-type": "application/x-www-form-urlencoded", }, "body": "entry." + idBox + "=" + uri, "method": "POST", "mode": "no-cors", - }).catch(error => console.error('error sending uri to google form:', error)); + }); + saveTracksToStorage(unique); + } catch (error) { + console.error('Error sending uri to google form:', error); } -} \ No newline at end of file +} + +const goofyHistory = debounce(async (e, urlForm, idBox) => { + const uri = e?.item?.uri; + if (uri && uri.includes('spotify:track:') && !unique.has(uri)) { + unique.add(uri); + await sendToGoogleForm(uri, urlForm, idBox); + } +}, MAX_DELAY); \ No newline at end of file