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

44 lines
1.2 KiB
JavaScript
Raw Normal View History

window.addEventListener('DOMContentLoaded', async () => {
2021-11-27 13:50:35 +00:00
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);
2021-11-27 13:50:35 +00:00
return null;
}
const data = await result.json();
if (data.payload.listens.length === 0) {
2021-11-27 13:50:35 +00:00
return null;
}
2021-11-27 13:50:35 +00:00
return data.payload.listens[0];
}
function insertHtml(listen) {
2021-12-03 23:51:55 +00:00
if (listen === null) {
return;
}
2021-11-27 13:50:35 +00:00
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);
2021-11-27 13:50:35 +00:00
}