added artwork image caching and fixed some bugs
This commit is contained in:
48
content.js
48
content.js
@ -19,6 +19,12 @@ async function imageUrlToBase64(url) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cache for artwork base64 by URL
|
||||
const artworkBase64Cache = {
|
||||
lastArtworkUrl: null,
|
||||
lastArtworkBase64: null
|
||||
};
|
||||
|
||||
async function getNowPlayingData() {
|
||||
const audio = document.querySelector('audio#apple-music-player');
|
||||
|
||||
@ -52,7 +58,17 @@ async function getNowPlayingData() {
|
||||
const artworkSrc = metadata.artwork?.[0]?.src || null;
|
||||
|
||||
const largeImageKey = artworkSrc?.replace(/\d+x\d+[a-z]*/i, '150x150bb'); // More robust for Apple Music artwork URLs
|
||||
const artworkBase64 = artworkSrc ? await imageUrlToBase64(largeImageKey) : null;
|
||||
|
||||
let artworkBase64 = null;
|
||||
if (artworkSrc) {
|
||||
if (artworkBase64Cache.lastArtworkUrl === largeImageKey && artworkBase64Cache.lastArtworkBase64) {
|
||||
artworkBase64 = artworkBase64Cache.lastArtworkBase64;
|
||||
} else {
|
||||
artworkBase64 = await imageUrlToBase64(largeImageKey);
|
||||
artworkBase64Cache.lastArtworkUrl = largeImageKey;
|
||||
artworkBase64Cache.lastArtworkBase64 = artworkBase64;
|
||||
}
|
||||
}
|
||||
|
||||
const currentTime = timestampInput ? Number(timestampInput.getAttribute('aria-valuenow')) : media.currentTime;
|
||||
const duration = timestampInput ? Number(timestampInput.getAttribute('aria-valuemax')) : media.duration;
|
||||
@ -152,22 +168,26 @@ if (!window._nowPlayingExtensionLogged) {
|
||||
window._nowPlayingExtensionLogged = true;
|
||||
}
|
||||
|
||||
chrome.storage.sync.get(['endpoint', 'token'], (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
console.error("Failed to load settings:", chrome.runtime.lastError.message);
|
||||
return;
|
||||
}
|
||||
if (typeof chrome !== "undefined" && chrome.storage && chrome.storage.sync) {
|
||||
chrome.storage.sync.get(['endpoint', 'token'], (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
console.error("Failed to load settings:", chrome.runtime.lastError.message);
|
||||
return;
|
||||
}
|
||||
|
||||
endpoint = result.endpoint;
|
||||
token = result.token;
|
||||
endpoint = result.endpoint;
|
||||
token = result.token;
|
||||
|
||||
if (!endpoint || !token) {
|
||||
console.warn("No endpoint/token configured.");
|
||||
return;
|
||||
}
|
||||
if (!endpoint || !token) {
|
||||
console.error("No endpoint/token configured.");
|
||||
return;
|
||||
}
|
||||
|
||||
startNowPlayingLoop();
|
||||
});
|
||||
startNowPlayingLoop();
|
||||
});
|
||||
} else {
|
||||
console.warn("Chrome extension APIs are not available. Please run this script as a Chrome extension.");
|
||||
}
|
||||
let lastUrl = location.href;
|
||||
|
||||
new MutationObserver(() => {
|
||||
|
Reference in New Issue
Block a user