/* SPEEDTEST JS FILE */ $('body').arrive('#activeInfo', {onceOnly: true}, function() { speedTestLaunch(); }); function clamp(num, min, max) { return num <= min ? min : num >= max ? max : num; } function I(id){return document.getElementById(id);} var w=null; //speedtest worker function startStop(){ if(w!=null){ //speedtest is running, abort w.postMessage('abort'); w=null; $('#speedTestButtonText').text('Start'); initUI(); }else{ //test is not running, begin w=new Worker('api/plugins/speedTest/misc/speedtest_worker.min.js'); w.postMessage('start'); //Add optional parameters as a JSON object to this command $('#speedTestButtonText').text('Running'); w.onmessage=function(e){ var data=e.data.split(';'); var status=Number(data[0]); if(status>=4){ //test completed $('#speedTestButtonText').text('Re-Test'); w=null; } var downloadText = Math.ceil((data[1]/1000)*1000); var downloadPercent = clamp(Math.ceil(((data[1]/1000)*100)/5)*5, 0,100); var uploadText = Math.ceil((data[2]/1000)*1000); var uploadPercent = clamp(Math.ceil(((data[2]/1000)*100)/5)*5, 0,100); I("ip").textContent=data[4]; I("dlText").textContent=(status==1&&data[1]==0)?"...":Math.ceil(data[1]); I("ulText").textContent=(status==3&&data[2]==0)?"...":Math.ceil(data[2]); I("pingText").textContent=Math.ceil(data[3]); I("jitText").textContent=Math.ceil(data[5]); var prog=(Number(data[6])*2+Number(data[7])*2+Number(data[8]))/5; I("progress").style.width=(100*prog)+"%"; $('#downloadPercent').attr('class', 'css-bar css-bar-'+downloadPercent+' css-bar-lg css-bar-default').attr('data-label', downloadText+'Mbps'); $('#uploadPercent').attr('class', 'css-bar css-bar-'+uploadPercent+' css-bar-lg css-bar-warning pull-right').attr('data-label', uploadText+'Mbps'); }; } } //poll the status from the worker every 200ms (this will also update the UI) setInterval(function(){ if(w) w.postMessage('status'); },200); //function to (re)initialize UI function initUI(){ I("dlText").textContent=""; I("ulText").textContent=""; I("pingText").textContent=""; I("jitText").textContent=""; I("ip").textContent=""; I("progress").style.width=""; $('#downloadPercent').attr('class', 'css-bar css-bar-0 css-bar-lg css-bar-default').attr('data-label', '0Mbps'); $('#uploadPercent').attr('class', 'css-bar css-bar-0 css-bar-lg css-bar-warning pull-right').attr('data-label', '0Mbps'); } // FUNCTIONS function speedTestLaunch(){ if(activeInfo.plugins["SPEEDTEST-enabled"] == true){ if (activeInfo.user.groupID <= activeInfo.plugins.includes["SPEEDTEST-Auth-include"]) { var menuList = `
  • Test Server Speed
  • `; var htmlDOM = `
    Test Speed to Server
    0% Complete (success)

    Mbps

    Mbps

    ms

    ms

    `; $('.append-menu').after(menuList); $('.organizr-area').after(htmlDOM); pageLoad(); } } }