Aktualizovat content.js
This commit is contained in:
37
content.js
37
content.js
@@ -1,8 +1,7 @@
|
|||||||
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) {
|
||||||
@@ -10,14 +9,28 @@ function extractAndSendSong() {
|
|||||||
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
|
||||||
|
const imgElement = trackInfo.querySelector('img');
|
||||||
|
const albumCoverUrl = imgElement ? imgElement.src : null;
|
||||||
|
|
||||||
|
// Získání uplynulého času a celkové délky písničky ze Spotify
|
||||||
|
const currentTimeEl = document.querySelector('[data-testid="playback-position"]');
|
||||||
|
const durationEl = document.querySelector('[data-testid="playback-duration"]');
|
||||||
|
|
||||||
|
const currentTime = currentTimeEl ? currentTimeEl.innerText : "0:00";
|
||||||
|
const duration = durationEl ? durationEl.innerText : "0:00";
|
||||||
|
|
||||||
|
// Spojený formát např. "0:53/3:45"
|
||||||
|
const progressFull = `${currentTime}/${duration}`;
|
||||||
|
|
||||||
if (songTitle) {
|
if (songTitle) {
|
||||||
const currentTrackString = `${artistName} - ${songTitle}`;
|
// Vytvoříme podpis, abychom posílali data buď při změně písničky, nebo každou vteřinu kvůli posunu času
|
||||||
|
const currentSignature = `${artistTitle}-${songTitle}-${currentTime}`;
|
||||||
|
|
||||||
// Odesíláme pouze pokud se písnička změnila, abychom zbytečně nezatěžovali server
|
if (currentSignature !== lastSentSignature) {
|
||||||
if (currentTrackString !== lastSentSong) {
|
lastSentSignature = currentSignature;
|
||||||
lastSentSong = currentTrackString;
|
|
||||||
|
|
||||||
console.log("Odesílám skladbu:", currentTrackString);
|
console.log("Odesílám data:", artistName, "-", songTitle, `[${progressFull}]`);
|
||||||
|
|
||||||
fetch(TARGET_URL, {
|
fetch(TARGET_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -27,6 +40,8 @@ function extractAndSendSong() {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
artist: artistName,
|
artist: artistName,
|
||||||
title: songTitle,
|
title: songTitle,
|
||||||
|
coverImg: albumCoverUrl,
|
||||||
|
progressTime: progressFull, // Bude obsahovat např. "0:53/3:45"
|
||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
})
|
})
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
@@ -37,9 +52,5 @@ function extractAndSendSong() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sledujeme změny na stránce, protože Spotify funguje jako Single Page Application
|
// Kontrola každou vteřinu pro plynulou aktualizaci času v písničce
|
||||||
const observer = new MutationObserver(() => {
|
setInterval(extractAndSendSong, 1000);
|
||||||
extractAndSendSong();
|
|
||||||
});
|
|
||||||
|
|
||||||
observer.observe(document.body, { childList: true, subtree: true });
|
|
||||||
Reference in New Issue
Block a user