- script(defer). async function toggleSub(sub) { let thinger = document.getElementById(`thinger_${sub}`); if (thinger.innerText === 'unsubscribe') { await doThing(sub, 'unsubscribe'); } else { await doThing(sub, 'subscribe'); } } function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(";").shift(); } async function doThing(sub, thing) { const jwtToken = getCookie("auth_token"); const response = await fetch(`/${thing}`, { method: 'POST', headers: { 'Authorization': `Bearer ${jwtToken}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ subreddit: sub }), }); let thinger = document.getElementById(`thinger_${sub}`); if (thing === 'subscribe') { thinger.innerText = 'unsubscribe'; } else { thinger.innerText = 'subscribe'; } if (!response.ok) { console.error(response); console.error(`Failed to do ${thing}`); } } function toggleDetails(details_id) { var detailsElement = document.getElementById(details_id); if (detailsElement) { detailsElement.open = !detailsElement.open; } }