Server list can now be loaded from a JSON file instead of hardcoding it
This commit is contained in:
parent
4a1ea40b32
commit
bfe80d9fb9
|
@ -32,29 +32,48 @@ var SPEEDTEST_SERVERS=[
|
|||
//INITIALIZE SPEEDTEST
|
||||
var s=new Speedtest(); //create speedtest object
|
||||
s.setParameter("telemetry_level","basic"); //enable telemetry
|
||||
s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
|
||||
|
||||
//SERVER AUTO SELECTION
|
||||
function initServers(){
|
||||
s.selectServer(function(server){
|
||||
if(server!=null){ //at least 1 server is available
|
||||
I("loading").className="hidden"; //hide loading message
|
||||
//populate server list for manual selection
|
||||
for(var i=0;i<SPEEDTEST_SERVERS.length;i++){
|
||||
if(SPEEDTEST_SERVERS[i].pingT==-1) continue;
|
||||
var option=document.createElement("option");
|
||||
option.value=i;
|
||||
option.textContent=SPEEDTEST_SERVERS[i].name;
|
||||
if(SPEEDTEST_SERVERS[i]===server) option.selected=true;
|
||||
I("server").appendChild(option);
|
||||
}
|
||||
//show test UI
|
||||
I("testWrapper").className="visible";
|
||||
initUI();
|
||||
}else{ //no servers are available, the test cannot proceed
|
||||
I("message").innerHTML="No servers available";
|
||||
}
|
||||
});
|
||||
var noServersAvailable=function(){
|
||||
I("message").innerHTML="No servers available";
|
||||
}
|
||||
var runServerSelect=function(){
|
||||
s.selectServer(function(server){
|
||||
if(server!=null){ //at least 1 server is available
|
||||
I("loading").className="hidden"; //hide loading message
|
||||
//populate server list for manual selection
|
||||
for(var i=0;i<SPEEDTEST_SERVERS.length;i++){
|
||||
if(SPEEDTEST_SERVERS[i].pingT==-1) continue;
|
||||
var option=document.createElement("option");
|
||||
option.value=i;
|
||||
option.textContent=SPEEDTEST_SERVERS[i].name;
|
||||
if(SPEEDTEST_SERVERS[i]===server) option.selected=true;
|
||||
I("server").appendChild(option);
|
||||
}
|
||||
//show test UI
|
||||
I("testWrapper").className="visible";
|
||||
initUI();
|
||||
}else{ //no servers are available, the test cannot proceed
|
||||
noServersAvailable();
|
||||
}
|
||||
});
|
||||
}
|
||||
if(typeof SPEEDTEST_SERVERS === "string"){
|
||||
//need to fetch list of servers from specified URL
|
||||
s.loadServerList(SPEEDTEST_SERVERS,function(servers){
|
||||
if(servers==null){ //failed to load server list
|
||||
noServersAvailable();
|
||||
}else{ //server list loaded
|
||||
SPEEDTEST_SERVERS=servers;
|
||||
runServerSelect();
|
||||
}
|
||||
});
|
||||
}else{
|
||||
//hardcoded server list
|
||||
s.addTestPoints(SPEEDTEST_SERVERS);
|
||||
runServerSelect();
|
||||
}
|
||||
}
|
||||
|
||||
var meterBk=/Trident.*rv:(\d+\.\d+)/i.test(navigator.userAgent)?"#EAEAEA":"#80808040";
|
||||
|
|
|
@ -32,7 +32,7 @@ var SPEEDTEST_SERVERS=[
|
|||
|
||||
//INITIALIZE SPEEDTEST
|
||||
var s=new Speedtest(); //create speedtest object
|
||||
s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
|
||||
|
||||
s.onupdate=function(data){ //callback to update data in UI
|
||||
I("ip").textContent=data.clientIp;
|
||||
I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
|
||||
|
@ -46,13 +46,29 @@ s.onend=function(aborted){ //callback for test ended/aborted
|
|||
initUI();
|
||||
}
|
||||
}
|
||||
function selectServer(){ //called when the page is fully loaded
|
||||
I("startStopBtn").style.display="none"; //hide start/stop button during server selection
|
||||
function selectServer(){ //called after loading server list
|
||||
s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
|
||||
I("startStopBtn").style.display=""; //show start/stop button again
|
||||
I("serverId").textContent=server.name; //show name of test server
|
||||
});
|
||||
}
|
||||
function loadServers(){ //called when the page is fully loaded
|
||||
I("startStopBtn").style.display="none"; //hide start/stop button during server selection
|
||||
if(typeof SPEEDTEST_SERVERS === "string"){
|
||||
//load servers from url
|
||||
s.loadServerList(SPEEDTEST_SERVERS,function(servers){
|
||||
//list loaded
|
||||
SPEEDTEST_SERVERS=servers;
|
||||
selectServer();
|
||||
});
|
||||
}else{
|
||||
//hardcoded list of servers, already loaded
|
||||
s.addTestPoints(SPEEDTEST_SERVERS);
|
||||
selectServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function startStop(){ //start/stop button pressed
|
||||
|
@ -219,7 +235,7 @@ function I(id){return document.getElementById(id);}
|
|||
<a href="https://github.com/librespeed/speedtest">Source code</a>
|
||||
<script type="text/javascript">
|
||||
initUI();
|
||||
selectServer();
|
||||
loadServers();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
36
speedtest.js
36
speedtest.js
|
@ -49,7 +49,7 @@ function Speedtest() {
|
|||
this._settings = {}; //settings for the speedtest worker
|
||||
this._state = 0; //0=adding settings, 1=adding servers, 2=server selection done, 3=test running, 4=done
|
||||
console.log(
|
||||
"LibreSpeed by Federico Dossena v5.1 - https://github.com/librespeed/speedtest"
|
||||
"LibreSpeed by Federico Dossena v5.2 - https://github.com/librespeed/speedtest"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -127,6 +127,40 @@ Speedtest.prototype = {
|
|||
addTestPoints: function(list) {
|
||||
for (var i = 0; i < list.length; i++) this.addTestPoint(list[i]);
|
||||
},
|
||||
/**
|
||||
* Load a JSON server list from URL (multiple points of test)
|
||||
* url: the url where the server list can be fetched. Must be an array with objects containing the following elements:
|
||||
* {
|
||||
* "name": "User friendly name",
|
||||
* "server":"http://yourBackend.com/", URL to your server. You can specify http:// or https://. If your server supports both, just write // without the protocol
|
||||
* "dlURL":"garbage.php" path to garbage.php or its replacement on the server
|
||||
* "ulURL":"empty.php" path to empty.php or its replacement on the server
|
||||
* "pingURL":"empty.php" path to empty.php or its replacement on the server. This is used to ping the server by this selector
|
||||
* "getIpURL":"getIP.php" path to getIP.php or its replacement on the server
|
||||
* }
|
||||
* result: callback to be called when the list is loaded correctly. An array with the loaded servers will be passed to this function, or null if it failed
|
||||
*/
|
||||
loadServerList: function(url,result) {
|
||||
if (this._state == 0) this._state = 1;
|
||||
if (this._state != 1) throw "You can't add a server after server selection";
|
||||
this._settings.mpot = true;
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onload = function(){
|
||||
try{
|
||||
var servers=JSON.parse(xhr.responseText);
|
||||
for(var i=0;i<servers.length;i++){
|
||||
this._checkServerDefinition(servers[i]);
|
||||
}
|
||||
this.addTestPoints(servers);
|
||||
result(servers);
|
||||
}catch(e){
|
||||
result(null);
|
||||
}
|
||||
}.bind(this);
|
||||
xhr.onerror = function(){result(null);}
|
||||
xhr.open("GET",url);
|
||||
xhr.send();
|
||||
},
|
||||
/**
|
||||
* Returns the selected server (multiple points of test)
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue