Aktualizovat content.js

This commit is contained in:
2026-09-23 15:10:32 +00:00
parent 876a537354
commit de82ebd56f

View File

@@ -1,45 +1,56 @@
const TARGET_URL = "http://localhost:8080/endpoint"; const TARGET_URL = "http://localhost:8080/endpoint";
let lastSentSong = ""; let lastSentSignature = "";
function extractAndSendSong() { function extractAndSendSong() {
// Najdeme widget "Now Playing" v levém dolním rohu Spotify const trackInfo = document.querySelector('[data-testid="now-playing-widget"]');
const trackInfo = document.querySelector('[data-testid="now-playing-widget"]');
if (trackInfo) {
if (trackInfo) { const links = trackInfo.querySelectorAll('a');
const links = trackInfo.querySelectorAll('a'); const songTitle = links[0] ? links[0].innerText : null;
const songTitle = links[0] ? links[0].innerText : null; const artistName = links[1] ? links[1].innerText : "Unknown Artist";
const artistName = links[1] ? links[1].innerText : "Unknown Artist";
// Získání obrázku alba
if (songTitle) { const imgElement = trackInfo.querySelector('img');
const currentTrackString = `${artistName} - ${songTitle}`; const albumCoverUrl = imgElement ? imgElement.src : null;
// Odesíláme pouze pokud se písnička změnila, abychom zbytečně nezatěžovali server // Získání uplynulého času a celkové délky písničky ze Spotify
if (currentTrackString !== lastSentSong) { const currentTimeEl = document.querySelector('[data-testid="playback-position"]');
lastSentSong = currentTrackString; const durationEl = document.querySelector('[data-testid="playback-duration"]');
console.log("Odesílám skladbu:", currentTrackString); const currentTime = currentTimeEl ? currentTimeEl.innerText : "0:00";
const duration = durationEl ? durationEl.innerText : "0:00";
fetch(TARGET_URL, {
method: "POST", // Spojený formát např. "0:53/3:45"
headers: { const progressFull = `${currentTime}/${duration}`;
"Content-Type": "application/json"
}, if (songTitle) {
body: JSON.stringify({ // Vytvoříme podpis, abychom posílali data buď při změně písničky, nebo každou vteřinu kvůli posunu času
artist: artistName, const currentSignature = `${artistTitle}-${songTitle}-${currentTime}`;
title: songTitle,
timestamp: new Date().toISOString() if (currentSignature !== lastSentSignature) {
}) lastSentSignature = currentSignature;
}).catch(err => {
console.error("Chyba při odesílání:", err); console.log("Odesílám data:", artistName, "-", songTitle, `[${progressFull}]`);
});
} fetch(TARGET_URL, {
} method: "POST",
} headers: {
} "Content-Type": "application/json"
},
// Sledujeme změny na stránce, protože Spotify funguje jako Single Page Application body: JSON.stringify({
const observer = new MutationObserver(() => { artist: artistName,
extractAndSendSong(); title: songTitle,
}); coverImg: albumCoverUrl,
progressTime: progressFull, // Bude obsahovat např. "0:53/3:45"
observer.observe(document.body, { childList: true, subtree: true }); timestamp: new Date().toISOString()
})
}).catch(err => {
console.error("Chyba při odesílání:", err);
});
}
}
}
}
// Kontrola každou vteřinu pro plynulou aktualizaci času v písničce
setInterval(extractAndSendSong, 1000);