Changed how the artwork gets sent to the API, this is done by sending it separately, so now we have 2 endpoints, its not the best but, it works for now :)

This commit is contained in:
2025-06-21 22:53:47 -07:00
parent 45a942b63e
commit 4ccf5edd2e
4 changed files with 54 additions and 40 deletions

View File

@ -1,7 +1,9 @@
let pauseStartTime = null;
let endpoint = null;
let artworkEndpoint = null;
let token = null;
let intervalId = null;
let lastSentArtworkKey = null;
async function imageUrlToBase64(url) {
try {
@ -72,6 +74,7 @@ async function getNowPlayingData() {
const currentTime = timestampInput ? Number(timestampInput.getAttribute('aria-valuenow')) : media.currentTime;
const duration = timestampInput ? Number(timestampInput.getAttribute('aria-valuemax')) : media.duration;
const commonData={details:title,state:artist,album,smallImageKey:'play',smallImageText:'Playing',startTimestamp:Math.floor(Date.now()/1000)-Math.floor(currentTime),endTimestamp:Math.floor(Date.now()/1000)+Math.floor(duration-currentTime),paused,currentTime,duration};
if (paused) {
if (!pauseStartTime) pauseStartTime = Date.now();
@ -81,37 +84,12 @@ async function getNowPlayingData() {
state: "",
paused: true
};
} else {
return {
details: title,
state: artist,
album,
smallImageKey: 'play',
smallImageText: 'Playing',
artworkBase64,
startTimestamp: Math.floor(Date.now() / 1000) - Math.floor(currentTime),
endTimestamp: Math.floor(Date.now() / 1000) + Math.floor(duration - currentTime),
paused: true,
currentTime,
duration
};
}
} else {
pauseStartTime = null;
}
return {
details: title,
state: artist,
album,
smallImageKey: 'play',
smallImageText: 'Playing',
artworkBase64,
startTimestamp: Math.floor(Date.now() / 1000) - Math.floor(currentTime),
endTimestamp: Math.floor(Date.now() / 1000) + Math.floor(duration - currentTime),
paused: false,
currentTime,
duration
...commonData,
artworkBase64
};
}
@ -122,23 +100,54 @@ async function sendNowPlaying() {
}
const data = await getNowPlayingData();
if (!data) return;
const { artworkBase64, details, state, ...noArtData } = data;
try {
const response = await fetch(endpoint, {
await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(data),
body: JSON.stringify({
...noArtData,
details,
state
})
});
if (response.ok) {
console.log('Now playing data sent:', data.details, '-', data.state);
} else {
console.error('Failed to send now playing data:', response.status);
}
console.log('Now playing data sent:', details, '-', state);
} catch (e) {
console.error('Error sending now playing data:', e);
console.error('Failed to send now playing:', e);
}
// Only send artwork when song changes
const currentTrackKey = `${details} - ${state}`;
if (
artworkEndpoint &&
artworkBase64 &&
currentTrackKey !== lastSentArtworkKey
) {
try {
await fetch(artworkEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
image: artworkBase64,
details,
state
})
});
lastSentArtworkKey = currentTrackKey;
console.log('Artwork sent for:', currentTrackKey);
} catch (e) {
console.error('Failed to send artwork:', e);
}
}
}
@ -169,13 +178,14 @@ if (!window._nowPlayingExtensionLogged) {
}
if (typeof chrome !== "undefined" && chrome.storage && chrome.storage.sync) {
chrome.storage.sync.get(['endpoint', 'token'], (result) => {
chrome.storage.sync.get(['endpoint', 'artworkEndpoint', 'token'], (result) => {
if (chrome.runtime.lastError) {
console.error("Failed to load settings:", chrome.runtime.lastError.message);
return;
}
endpoint = result.endpoint;
artworkEndpoint = result.artworkEndpoint;
token = result.token;
if (!endpoint || !token) {