2021-11-26 18:24:42 +00:00
|
|
|
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() {
|
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);
|
2021-11-27 13:50:35 +00:00
|
|
|
return null;
|
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) {
|
2021-11-27 13:50:35 +00:00
|
|
|
return null;
|
2021-11-27 12:05:52 +00:00
|
|
|
}
|
|
|
|
|
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">
|
2021-11-26 18:24:42 +00:00
|
|
|
<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
|
|
|
}
|