1
Fork 0
bauke-xyz/source/js/listenbrainz.js

44 lines
1.2 KiB
JavaScript

window.addEventListener('DOMContentLoaded', async () => {
document.querySelector('.page-main').insertAdjacentHTML('beforeend', '<div class="divider"></div>');
const loop = async () => insertHtml(await getCurrentListen());
await loop();
setInterval(loop, 60 * 1000);
});
async function getCurrentListen() {
const result = await window.fetch('https://api.listenbrainz.org/1/user/BaukeXYZ/playing-now');
if (!result.ok) {
console.warn(result.status);
return null;
}
const data = await result.json();
if (data.payload.listens.length === 0) {
return null;
}
return data.payload.listens[0];
}
function insertHtml(listen) {
if (listen === null) {
return;
}
const existing = document.querySelector('.listenbrainz');
if (existing !== null) {
existing.remove();
}
const listenHtml = `<div class="listenbrainz">
<a href="https://listenbrainz.org/user/BaukeXYZ/" target="_blank">
<img src="https://listenbrainz.org/static/img/logo_big.svg">
${listen.track_metadata.artist_name} - ${listen.track_metadata.track_name}
</a>
</div>`;
document.querySelector('.page-main').insertAdjacentHTML('beforeend', listenHtml);
}