mirror of
https://github.com/SpotX-Official/SpotX.git
synced 2026-04-11 17:37:21 +10:00
goofy_History update
- async support added - added a buffer zone in localStorage for storing sent tracks - added a delay between sending tracks
This commit is contained in:
@@ -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": {
|
"headers": {
|
||||||
"content-type": "application/x-www-form-urlencoded",
|
"content-type": "application/x-www-form-urlencoded",
|
||||||
},
|
},
|
||||||
"body": "entry." + idBox + "=" + uri,
|
"body": "entry." + idBox + "=" + uri,
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"mode": "no-cors",
|
"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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
Reference in New Issue
Block a user