From 78703ed57007329b3381a812a349b1c0f25bfd86 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Sat, 11 May 2024 21:47:07 +1000 Subject: [PATCH] Added interaction counts to posts --- weblog/B0. Template/fediverse.js.md | 42 ++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/weblog/B0. Template/fediverse.js.md b/weblog/B0. Template/fediverse.js.md index 5f1c3ef..87a3a45 100644 --- a/weblog/B0. Template/fediverse.js.md +++ b/weblog/B0. Template/fediverse.js.md @@ -121,21 +121,39 @@ customElements.define('fedi-social', FediSocial) function fediverse() { const data = JSON.parse(localStorage.getItem('fedi-social') || '{}') - document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => { + document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(async (el) => { if(data.template){ - href = data.template.replace("{uri}", el.href) - const innerHTML = ` - - - - ` + const orig_href = el.href + let href = data.template.replace("{uri}", orig_href) + el.href = href - const span = document.createElement('span') - span.classList.add('fediverse') - span.innerHTML = innerHTML + try{ + const res = await fetch(`https://monrepos.casa/api/v2/search?type=statuses&q=${orig_href}`, { + headers: { + 'Authorization': 'Basic Ym90OjJuUmZhTGJ1c3cyaFhA' + } + }) + const json = await res.json() + const status = json?.statuses?.find(s => s.uri == orig_href) - el.insertAdjacentElement('afterend', span) - el.remove() + if(status) { + const innerHTML = ` + ${status?.replies_count} + ${status?.favourites_count} + ${status?.reblogs_count} + ` + + const span = document.createElement('span') + span.classList.add('fediverse') + span.innerHTML = innerHTML + + el.insertAdjacentElement('afterend', span) + el.remove() + } + } + catch(ex){ + console.error(ex) + } } else { el.addEventListener('click', ev =>{