function update(){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { var json_text = xhttp.responseText; var json_obj = JSON.parse(json_text); var playing = ''; var first = getFirstPlaying(json_obj); if (first == null) { playing = 'Mitte munnigi ei mängi prg'; } else if (first != null && first.artist != undefined) { playing = first.artist + ' - '; playing += first.title; } else { playing += first.title; } var nowPlaying = playing; update_field('on-air', playing); update_listeners(json_obj); } }; xhttp.open("GET", "http://kahtlane.eu:8000/status-json.xsl", true); xhttp.send(); } function update_field(field, data){ document.getElementById(field).innerHTML = data; return 'ok'; } function update_listeners(json_obj){ var nowPlaying = getFirstPlaying(json_obj); if(nowPlaying != null) { update_field('listeners1', nowPlaying.listeners); } } function autoupdate(){ update(); setInterval(function(){ update(); }, 30000); } function change_bg(id){ document.body.style.backgroundImage = "url('http://i.imgur.com/" + id + ".jpg')"; } function reset_bg(){ document.body.style.backgroundImage = "url('http://i.imgur.com/BkSL2KJ.png')"; } function getFirstPlaying(jsonObj){ var sources = jsonObj.icestats.source; var masterStreams = [ '/live', '/sai', '/siinus', '/viido' ] for(o=0; o < sources.length; o++) { if (sources[o].title != undefined && masterStreams.some(stream=>sources[o].listenurl.endsWith(stream))) { //check if source has a title key AND belongs to masterStreams return sources[o]; } } return null; }