-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbookmarklet.js
More file actions
1 lines (1 loc) · 5.79 KB
/
bookmarklet.js
File metadata and controls
1 lines (1 loc) · 5.79 KB
1
var thisButton,min_gap=4,max_slow_speed=5,mycolor="#ff4500",beep_for_all=!1,show_bibs=!1,teams=[],xmlhttp=new XMLHttpRequest,url="/api/allCompetitors-2022",peloton=[],riders_prev=[],velofacts=new EventSource("/live-stream"),paused=!1,selected_team="";str_pad_left=function(b,c,a){return(new Array(a+1).join(c)+b).slice(-a)},prety_time=function(a){var b=Math.floor(a/60);return b+":"+str_pad_left(a-60*b,"0",2)},select_team=function(){var b=document.getElementsByClassName("teamJersey");[].forEach.call(b,function(a){a.classList.remove("selected")});var a=this.getAttribute("data-id");return a==selected_team?(selected_team="",this.classList.remove("selected")):(selected_team=a,this.classList.toggle("selected")),!1},toggle_pause=function(){return paused=!paused,document.getElementById("pause-icon").className=paused?"fas fa-play-circle":"fas fa-pause-circle",!1};var style=document.createElement("style");style.innerHTML=".group {border-bottom: 2px solid #ba4a19; margin-bottom: 3px; font-size: 11px; color: #333} .gc div { background-color: #ff0; border: 1px solid #ffb700; } .green div { background-color: #45AE51; color: #fff} .mylist div { background-color: "+mycolor+"; color: #fff} .slow div {color: #fff; background-color: #000} .team {color:#fff; background-color: #ba4a19; } #q-app {padding: 10px} #pause, #button-settings {font-size: 40px;} .group div div { padding-left: 2px; padding-right: 2px;} #toolbar { display: flex; background: #fee5d9; padding: 10px; border-bottom: 2px solid #ba4a19; align-items: center } #toolbar > img { width: 72px; } #jerseyWrapper { display: flex;margin: auto; align-items: center; } .row { justify-content: center; } .selected { background: #ffa47b; } .fas { color: #ba4a19; } .row>.col-md-2 { padding: 2px 5px } .teamJersey:hover { background: #ffa47b; } #toolbar .distance { padding-left: 10px; font-size: 24px }",style.innerHTML+=" #jerseyWrapper a {width: 3.6%} #jerseyWrapper img {max-width:100%}",style.innerHTML+=" #settings { display: none} #settings input {width: 500px;}",style.innerHTML+=" .bib {background-color: #fff; color: #000; font-size: 8px; border: 1px solid #000; padding-left: 1px; padding-right: 1px;}",document.head.appendChild(style),document.getElementById("q-app").innerHTML='<div id="toolbar"><a href="#" id="pause"><i id="pause-icon" class="fas fa-pause-circle"></i></a><a href="#" id="button-settings"><i id="settings-icon" class="fas fa-wrench"></i></a><span class="distance"><span id="distance">...</span> km</span><div id="jerseyWrapper"></div></div><div id="settings"></div><div id="rows"> waiting for data...</div>',save_settings=function(){var a=this.id;localStorage.setItem(a,this.value),lists[a].bibs=","+this.value+","};var list_array=["gc","pc","my","bp"],lists=[];lists.gc={id:"gc",label:"General classification",classname:"gc",sound:"",bibs:"1,11,18,21,22,28,31,41,61,71,81,82,91,97,98,124,141,142,147,151,172,185,194,198"},lists.pc={id:"pc",label:"Sprinters",classname:"green",sound:"",bibs:"16,48,51,78,88,105,112,121,125,155,161,171,178,181,201,204,207,216"},lists.my={id:"my",label:"My favourites",classname:"mylist",sound:"",bibs:""},lists.bp={id:"bp",label:"Play a sound when slower than "+max_slow_speed+"kmh",classname:"",sound:"sound1",bibs:""};for(var i=0;i<list_array.length;i++)l=lists[list_array[i]],document.getElementById("settings").innerHTML+='<input type="text" id="'+l.id+'" placeholder="comma separated list of bib numbers">'+l.label+"<br>";document.getElementById("settings").innerHTML+="";for(var i=0;i<teams.length;i++)document.getElementById("jerseyWrapper").innerHTML+='<a href="#" class="teamJersey" id="team'+i+'" data-id="'+teams[i]._id+'" title="'+teams[i].name+'"><img src="'+teams[i].jersey_sm+'"></a>';document.getElementById("toolbar").innerHTML+='<img src="img/logo.f4376aa6.png" />',document.getElementById("pause").onclick=toggle_pause,document.getElementById("button-settings").onclick=function(){var a=document.getElementById("settings");return"block"!==a.style.display?(a.style.display="block",show_bibs=!0):(a.style.display="none",show_bibs=!1),!1};for(var i=0;i<teams.length;i++)document.getElementById("team"+i).onclick=select_team;for(var i=0;i<list_array.length;i++)l=lists[list_array[i]],document.getElementById(l.id).onkeyup=save_settings,localStorage.getItem(l.id)?(l.bibs=","+localStorage.getItem(l.id)+",",document.getElementById(l.id).value=""+localStorage.getItem(l.id)):(document.getElementById(l.id).value=l.bibs,l.bibs=","+l.bibs+",");function start_listening(){velofacts.addEventListener("update",function(l){if(!paused){var h=JSON.parse(l.data),e='<div class="row group">',j=0;if("telemetryCompetitor-2022"==h.bind){for(var f="",k=h.data.Riders,c=0;c<k.length;c++){var a=k[c];0==c&&(document.getElementById("distance").innerHTML=a.kmToFinish);var d=a.secToFirstRider,g=a.kph,m=a.kphAvg,b="";d-j>min_gap&&(e+='</div><div class="row group">'),(lists.bp.bibs.includes(","+a.Bib+",")||beep_for_all)&&g<max_slow_speed&&(f=lists.bp.sound),""==selected_team?(lists.gc.bibs.includes(","+a.Bib+",")&&(b+=" gc"),lists.pc.bibs.includes(","+a.Bib+",")&&(b+=" green"),lists.my.bibs.includes(","+a.Bib+",")&&(b+=" mylist")):peloton[a.Bib].$team.split(":")[1]==selected_team&&(b+=" team"),g<max_slow_speed&&(b="slow"),bib_html="",show_bibs&&(bib_html='<span class="bib">'+a.Bib+"</span> "),e+='<div id="r'+a.Bib+'" title="Speed: '+g+"km/h | Average Speed: "+m+"km/h | "+a.kmToFinish+"km to go, bib:"+a.Bib+'" class="col-md-2 '+b+'"><div>'+bib_html+peloton[a.Bib].lastnameshort+" "+peloton[a.Bib].firstname+" "+prety_time(d)+"</div></div>",d>0&&(j=d)}""!=f&&document.getElementById(f).play(),document.getElementById("rows").innerHTML=e+"</div>"}}})}xmlhttp.onreadystatechange=function(){if(4==this.readyState&&200==this.status){tmp=JSON.parse(this.responseText);for(var a=0;a<tmp.length;a++)peloton[tmp[a].bib]=tmp[a];console.log(peloton)}start_listening()},xmlhttp.open("GET",url,!0),xmlhttp.send()