From b3f855440fece09e9881c2de1780952eb2a0106e Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 27 Nov 2021 14:50:35 +0100 Subject: [PATCH] Make ListenBrainz loop. --- source/js/listenbrainz.js | 27 ++++++++++++++++++++++----- yarn.lock | 6 +++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/source/js/listenbrainz.js b/source/js/listenbrainz.js index acc0e78..1d0d701 100644 --- a/source/js/listenbrainz.js +++ b/source/js/listenbrainz.js @@ -1,17 +1,34 @@ window.addEventListener('DOMContentLoaded', async () => { + document.querySelector('.page-main').insertAdjacentHTML('beforeend', '
'); + + 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; + return null; } const data = await result.json(); if (data.payload.listens.length === 0) { - return; + return null; } - const listen = data.payload.listens[0]; - const listenHtml = `
+ return data.payload.listens[0]; +} + +function insertHtml(listen) { + const existing = document.querySelector('.listenbrainz'); + if (existing !== null) { + existing.remove(); + } + + const listenHtml = ``; document.querySelector('.page-main').insertAdjacentHTML('beforeend', listenHtml); -}); +} diff --git a/yarn.lock b/yarn.lock index 972a693..3b0163e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1994,7 +1994,7 @@ dependencies: "@types/yargs-parser" "*" -"@typescript-eslint/eslint-plugin@*": +"@typescript-eslint/eslint-plugin@^5.4.0": version "5.4.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.4.0.tgz#05e711a2e7b68342661fde61bccbd1531c19521a" integrity sha512-9/yPSBlwzsetCsGEn9j24D8vGQgJkOTr4oMLas/w886ZtzKIs1iyoqFrwsX2fqYEeUwsdBpC21gcjRGo57u0eg== @@ -2020,7 +2020,7 @@ eslint-scope "^5.1.1" eslint-utils "^3.0.0" -"@typescript-eslint/parser@*": +"@typescript-eslint/parser@^5.4.0": version "5.4.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.4.0.tgz#3aa83ce349d66e39b84151f6d5464928044ca9e3" integrity sha512-JoB41EmxiYpaEsRwpZEYAJ9XQURPFer8hpkIW9GiaspVLX8oqbqNM8P4EP8HOZg96yaALiLEVWllA2E8vwsIKw== @@ -4098,7 +4098,7 @@ eslint-config-prettier@^8.3.0: resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.3.0.tgz#f7471b20b6fe8a9a9254cc684454202886a2dd7a" integrity sha512-BgZuLUSeKzvlL/VUjx/Yb787VQ26RU3gGjA3iiFvdsp/2bMfVIWUVP7tjxtjS0e+HP409cPlPvNkQloz8C91ew== -eslint-config-xo-typescript@*: +eslint-config-xo-typescript@^0.47.1: version "0.47.1" resolved "https://registry.yarnpkg.com/eslint-config-xo-typescript/-/eslint-config-xo-typescript-0.47.1.tgz#87b5865d8a3428fa26cc8dc3146ef4f712dfed46" integrity sha512-BkbzIltZCWp8QLekKJKG8zJ/ZGezD8Z9FaJ+hJ5PrAVUGkIPmxXLLEHCKS3ax7oOqZLYQiG+jyKfQDIEdTQgbg==