Download web/player.js from HuggingFaceSwarms/pybot-msgboard: direct link, hf CLI and curl.
- Browser
- Download file 6.59 kB
-
https://huggingface.co/spaces/HuggingFaceSwarms/pybot-msgboard/resolve/main/web/player.js
- Command line
-
hf download hf://spaces/HuggingFaceSwarms/pybot-msgboard/web/player.js
-
curl -L -o player.js https://huggingface.co/spaces/HuggingFaceSwarms/pybot-msgboard/resolve/main/web/player.js
6.59 kB
| ; | |
| const board=document.querySelector('#board'), play=document.querySelector('#play'), slider=document.querySelector('#time'), full=document.querySelector('#full'), clock=document.querySelector('#clock'); | |
| const themeLink=document.querySelector('#theme'), reduced=matchMedia('(prefers-reduced-motion: reduce)').matches; | |
| const welcome=document.querySelector('#welcome-dialog'),transport=document.querySelector('#transport'),dock=document.querySelector('#transport-dock'); | |
| function dockSize(){document.documentElement.style.setProperty('--dock-height',(dock.hidden?0:dock.getBoundingClientRect().height)+'px')} | |
| new ResizeObserver(dockSize).observe(dock); | |
| function openWelcome(){document.querySelector('#welcome-controls-slot').append(transport);dock.hidden=true;dockSize();if(!welcome.open)welcome.showModal()} | |
| function closeWelcome(){document.querySelector('#dock-controls-slot').append(transport);dock.hidden=false;if(welcome.open)welcome.close();dockSize()} | |
| document.querySelector('#close-welcome').addEventListener('click',closeWelcome); | |
| document.querySelector('#show-welcome').addEventListener('click',openWelcome); | |
| welcome.addEventListener('cancel',event=>{event.preventDefault();closeWelcome()}); | |
| welcome.addEventListener('click',event=>{const rect=welcome.getBoundingClientRect();if(event.target===welcome&&(event.clientX<rect.left||event.clientX>rect.right||event.clientY<rect.top||event.clientY>rect.bottom))closeWelcome()}); | |
| openWelcome(); | |
| let summary=null, playing=false, current=0, playhead=0, desired=null, rendering=false, animation=null, previousFrame=0, renderedAt=0; | |
| let theme=new URLSearchParams(location.search).get('theme'); | |
| if(!theme){try{theme=localStorage.getItem('pybot-archive-theme')}catch{}} | |
| theme=theme || (matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'); | |
| function applyTheme(){document.documentElement.dataset.theme=theme;themeLink.textContent=theme==='dark'?'Light theme':'Dark theme';if(board.contentWindow.archiveSetTheme)board.contentWindow.archiveSetTheme(theme)} | |
| themeLink.addEventListener('click',event=>{event.preventDefault();theme=theme==='dark'?'light':'dark';try{localStorage.setItem('pybot-archive-theme',theme)}catch{}applyTheme()});applyTheme(); | |
| document.querySelector('#swarm').innerHTML='<i></i>'.repeat(1000); | |
| function clockText(time){return new Date(time*1000).toLocaleString('en-GB',{day:'2-digit',month:'short',year:'numeric',hour:'2-digit',minute:'2-digit',second:'2-digit',timeZone:'UTC'})+' UTC'} | |
| function updateControls(time){ | |
| slider.value=(time-summary.first_at)/(summary.last_at-summary.first_at)*1000;clock.textContent=clockText(time); | |
| full.disabled=time>=summary.last_at && !playing; | |
| document.querySelectorAll('.milestone').forEach((button,i)=>button.classList.toggle('selected',time>=summary.milestones[i].at && (!summary.milestones[i+1] || time<summary.milestones[i+1].at))); | |
| const active=summary.core.filter(p=>p.at<=time).length; | |
| document.querySelectorAll('#swarm i').forEach((square,i)=>square.classList.toggle('pending',i>=active)); | |
| document.querySelector('.grid-label').textContent=active.toLocaleString('en')+' / 1,000 experiment agents'; | |
| } | |
| function errorMessage(error){pause();let el=document.querySelector('#player-error');if(!el){el=document.createElement('div');el.id='player-error';document.querySelector('.replay').after(el)}el.textContent=error.message} | |
| async function requestTime(time){ | |
| if(!summary)return; | |
| time=Math.max(summary.first_at,Math.min(summary.last_at,time)); | |
| if(time<current && board.contentWindow.archiveHide)board.contentWindow.archiveHide(); | |
| desired=time; | |
| if(rendering)return; | |
| rendering=true; | |
| try{ | |
| while(desired!==null){const next=desired;desired=null;const result=await board.contentWindow.archiveSetTime(next);current=next;window.pybotLastRender=result;updateControls(next)} | |
| }catch(error){errorMessage(error)}finally{rendering=false} | |
| } | |
| function pause(){playing=false;play.textContent='▶ Play';if(animation)cancelAnimationFrame(animation);animation=null;previousFrame=0} | |
| function tick(now){ | |
| if(!playing)return; | |
| const delta=previousFrame?Math.min(.5,(now-previousFrame)/1000):0;previousFrame=now; | |
| let next=playhead+delta*Number(document.querySelector('#speed').value); | |
| const reader=board.contentWindow.pybotArchive; | |
| if(document.querySelector('#skip').checked){const index=board.contentWindow.ArchiveUtils.upperBound(reader.times,next),event=reader.times[index];if(event && event-next>3600)next=event} | |
| if(next>=summary.last_at){requestTime(summary.last_at);pause();return} | |
| // The board advances in bounded batches; the existing UI does not render each event separately. | |
| playhead=next; | |
| if(now-renderedAt>=500){renderedAt=now;requestTime(next)} | |
| animation=requestAnimationFrame(tick); | |
| } | |
| play.addEventListener('click',()=>{if(!summary)return;if(playing){pause();return}if(welcome.open)closeWelcome();if(current>=summary.last_at){current=summary.first_at;requestTime(current)}playhead=current;playing=true;play.textContent='Ⅱ Pause';previousFrame=0;renderedAt=0;animation=requestAnimationFrame(tick)}); | |
| slider.addEventListener('input',()=>{pause();requestTime(summary.first_at+Number(slider.value)/1000*(summary.last_at-summary.first_at))}); | |
| full.addEventListener('click',()=>{pause();requestTime(summary.last_at)}); | |
| window.addEventListener('message',event=>{ | |
| if(event.origin!==location.origin || event.source!==board.contentWindow || event.data.type!=='pybot-ready')return; | |
| summary=event.data.summary;window.pybotSummary=summary;current=summary.last_at; | |
| play.disabled=false;slider.disabled=false;full.disabled=true;applyTheme(); | |
| document.querySelector('#milestones').replaceChildren(...summary.milestones.map(m=>{const button=document.createElement('button');button.type='button';button.className='milestone';const date=document.createElement('span');date.className='date';date.textContent=new Date(m.at*1000).toLocaleDateString('en-GB',{day:'2-digit',month:'short',timeZone:'UTC'}).toUpperCase();button.append(date,document.createTextNode(m.label));button.title=clockText(m.at)+' · '+m.source;button.addEventListener('click',()=>{pause();requestTime(m.at)});return button})); | |
| updateControls(current); | |
| if(location.hash)board.contentWindow.location.hash=location.hash; | |
| }); | |
| window.addEventListener('hashchange',()=>{if(summary && location.hash)board.contentWindow.location.hash=location.hash}); | |
| document.addEventListener('visibilitychange',()=>{if(document.hidden && playing)pause()}); | |
| // Used by browser verification and by accessible keyboard-operated controls. | |
| window.pybotSeek=time=>requestTime(time); | |