2021-11-26 18:24:42 +00:00
|
|
|
window.addEventListener('DOMContentLoaded', async () => {
|
2022-03-28 13:07:22 +00:00
|
|
|
const loop = async () => {
|
|
|
|
const listen = await getCurrentListen();
|
2022-03-28 19:10:02 +00:00
|
|
|
if (listen === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-30 16:40:07 +00:00
|
|
|
let image;
|
|
|
|
try {
|
|
|
|
image = await getCoverArt(listen);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:07:22 +00:00
|
|
|
insertHtml(listen, image);
|
|
|
|
};
|
|
|
|
|
2021-11-27 13:50:35 +00:00
|
|
|
await loop();
|
|
|
|
|
2022-03-28 13:07:22 +00:00
|
|
|
setInterval(loop, 5 * 60 * 1000);
|
2021-11-27 13:50:35 +00:00
|
|
|
});
|
|
|
|
|
2022-03-28 13:07:22 +00:00
|
|
|
async function getCoverArt(listen) {
|
|
|
|
const releaseMbid = listen.track_metadata.additional_info?.release_mbid;
|
|
|
|
if (releaseMbid === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await window.fetch(`https://coverartarchive.org/release/${releaseMbid}`, {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!result.ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await result.json();
|
|
|
|
if (data.images[0]?.thumbnails === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const thumbnails = data.images[0].thumbnails;
|
|
|
|
return thumbnails.small ?? thumbnails['250'] ?? thumbnails['500'] ?? undefined;
|
|
|
|
}
|
|
|
|
|
2021-11-27 13:50:35 +00:00
|
|
|
async function getCurrentListen() {
|
2021-11-26 18:24:42 +00:00
|
|
|
const result = await window.fetch('https://api.listenbrainz.org/1/user/BaukeXYZ/playing-now');
|
|
|
|
if (!result.ok) {
|
|
|
|
console.warn(result.status);
|
2022-03-28 13:18:16 +00:00
|
|
|
return;
|
2021-11-26 18:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = await result.json();
|
2021-11-27 12:05:52 +00:00
|
|
|
if (data.payload.listens.length === 0) {
|
2022-03-28 13:18:16 +00:00
|
|
|
return;
|
2021-11-27 12:05:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 13:50:35 +00:00
|
|
|
return data.payload.listens[0];
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:07:22 +00:00
|
|
|
function insertHtml(listen, image) {
|
2022-03-28 13:18:16 +00:00
|
|
|
if (listen === undefined) {
|
2021-12-03 23:51:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:18:16 +00:00
|
|
|
const existing = document.querySelector('.listenbrainz') ?? undefined;
|
|
|
|
if (existing !== undefined) {
|
2022-03-28 16:18:06 +00:00
|
|
|
existing.previousElementSibling.remove();
|
2021-11-27 13:50:35 +00:00
|
|
|
existing.remove();
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:07:22 +00:00
|
|
|
const text = `${listen.track_metadata.artist_name} - ${listen.track_metadata.track_name}`;
|
|
|
|
const alt = image === undefined ? 'ListenBrainz Logo' : `${text} Cover Art`;
|
|
|
|
|
|
|
|
image = image ?? 'https://listenbrainz.org/static/img/logo_big.svg';
|
2022-03-28 18:23:31 +00:00
|
|
|
image = image.startsWith('http://') ? 'https' + image.slice(4) : image;
|
2022-03-28 13:07:22 +00:00
|
|
|
|
2022-03-28 16:18:06 +00:00
|
|
|
const listenHtml = `
|
|
|
|
<div class="divider"></div>
|
|
|
|
<p class="listenbrainz">
|
2022-03-28 13:07:22 +00:00
|
|
|
<img alt="${alt}" title="${alt}" src="${image}">
|
2022-03-28 12:19:08 +00:00
|
|
|
<span class="byline">Currently listening to</span>
|
|
|
|
<a href="https://listenbrainz.org/user/BaukeXYZ/" target="_blank">
|
2022-03-28 13:07:22 +00:00
|
|
|
${text}
|
2022-03-28 12:19:08 +00:00
|
|
|
</a>
|
|
|
|
</p>`;
|
2021-11-26 18:24:42 +00:00
|
|
|
|
|
|
|
document.querySelector('.page-main').insertAdjacentHTML('beforeend', listenHtml);
|
2021-11-27 13:50:35 +00:00
|
|
|
}
|