friendship ended with social-app. php is my new best friend
at main 3.0 kB view raw
1const loadPost = async (post, toplevel = false) => { 2 post.setAttribute('data-status', 'loading'); 3 const urlComponents = post.getAttribute('data-uri').split(/at:\/\/(did:plc:[a-z0-9]+)\/app\.bsky\.feed\.post\/([a-z0-9]+)/); 4 fetch('/api/post/'+urlComponents[1]+'/'+urlComponents[2]) 5 .then((data) => { 6 data.text().then((content) => { 7 post.innerHTML = content; 8 Promise.all([ 9 fetch('/api/likes/'+urlComponents[1]+"/"+urlComponents[2]), 10 fetch('/api/replies/'+urlComponents[1]+"/"+urlComponents[2]), 11 fetch('/api/reposts/'+urlComponents[1]+"/"+urlComponents[2]), 12 fetch('/api/quotes/'+urlComponents[1]+"/"+urlComponents[2]) 13 ]).then((values) => { 14 values[0].json().then((likes) => { 15 post.querySelector('.like-count').textContent = likes.total; 16 if (toplevel) { 17 document.getElementById('likes').querySelector('.inner').innerHTML = likes.rendered; 18 } 19 }); 20 values[1].json().then((replies) => { 21 post.querySelector('.reply-count').textContent = replies.total; 22 if (toplevel) { 23 document.getElementById('replies').querySelector('.inner').innerHTML = replies.rendered; 24 document.getElementById('replies').querySelectorAll('.inner .post').forEach((reply) => { 25 loadPost(reply); 26 }); 27 } 28 }); 29 values[2].json().then((reposts) => { 30 post.querySelector('.repost-count').textContent = reposts.total; 31 if (toplevel) { 32 document.getElementById('reposts').querySelector('.inner').innerHTML = reposts.rendered; 33 } 34 }); 35 values[3].json().then((quotes) => { 36 post.querySelector('.quote-count').textContent = quotes.total; 37 if (toplevel) { 38 document.getElementById('quotes').querySelector('.inner').innerHTML = quotes.rendered; 39 } 40 }); 41 post.setAttribute('data-status', 'loaded'); 42 }) 43 }); 44 }); 45} 46 47document.addEventListener('click', (e) => { 48 if (e.target.closest('.post .postSharing a')) { 49 e.preventDefault(); 50 const link = e.target.closest('.post .postSharing a'); 51 const url = link.getAttribute('data-share'); 52 navigator.clipboard.writeText(url); 53 const tooltip = document.createElement('span'); 54 tooltip.classList.add("copy-tooltip"); 55 tooltip.textContent = "Copied!"; 56 tooltip.style.top = e.y+"px"; 57 tooltip.style.left = e.x+"px"; 58 document.body.append(tooltip); 59 setTimeout(() => { 60 tooltip.remove(); 61 }, 3000); 62 } 63}); 64 65window.addEventListener('load', (_e) => { 66 Promise.all(Array.from(document.querySelectorAll('[data-status="unloaded"]')).map(async (post) => { 67 return new Promise(() => loadPost(post, document.querySelector("main").classList.contains("post"))); 68 })).then((ret) => { 69 document.getElementById('spinner-container').style.display = "none"; 70 }); 71});