forked from unimatrix/web
1 line
No EOL
1.8 KiB
JavaScript
1 line
No EOL
1.8 KiB
JavaScript
const $=id=>document.getElementById(id);const allowed=new Set(['/api/worlds','/api/staff/roster','/api/auth/session']);async function load(path){const response=await fetch(path,{credentials:'same-origin',cache:'no-store'});const text=await response.text();let data;try{data=JSON.parse(text);}catch{data={message:'Non-JSON response',status:response.status};}return {response,data};}async function session(){try{const {response,data}=await load('/api/auth/session');const user=response.ok&&data.authenticated?data.user:null;$('session-state').textContent=user?'Signed in':'Guest';$('guest-links').hidden=!!user;$('member-links').hidden=!user;$('account-intro').textContent=user?'Authenticated account tools':'Public documentation is available to everyone. Sign in for account-specific tools.';if(user)$('account-name').textContent='Signed in as '+String(user.username||'member');}catch{$('session-state').textContent='Unavailable';$('account-intro').textContent='Session lookup is currently unavailable.';}}async function worlds(){try{const {response}=await load('/api/worlds');$('world-state').textContent=response.ok?'Responding':'Unavailable';}catch{$('world-state').textContent='Unavailable';}}document.querySelectorAll('[data-try]').forEach(button=>button.addEventListener('click',async()=>{const path=button.dataset.try;if(!allowed.has(path))return;$('response-status').textContent='Loading '+path;try{const {response,data}=await load(path);const formatted=JSON.stringify(data,null,2);$('response-body').textContent=formatted.slice(0,12000)+(formatted.length>12000?'\n// Response truncated':'');$('response-status').textContent='HTTP '+response.status+' · '+path;}catch(error){$('response-status').textContent='Request failed';$('response-body').textContent=String(error.message||error);}}));session();worlds(); |

Staff