index.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <title>speedtest-x</title>
  7. <link rel="shortcut icon" href="favicon.ico">
  8. <script type="text/javascript" src="speedtest.js"></script>
  9. <script type="text/javascript">
  10. //INITIALIZE SPEEDTEST
  11. var s=new Speedtest(); //create speedtest object
  12. var xhr=new XMLHttpRequest();
  13. var url_report='backend/report.php';
  14. s.onupdate=function(data){ //callback to update data in UI
  15. I("ip").textContent=data.clientIp;
  16. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  17. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  18. I("pingText").textContent=data.pingStatus;
  19. I("jitText").textContent=data.jitterStatus;
  20. var prog=(Number(data.dlProgress)*2+Number(data.ulProgress)*2+Number(data.pingProgress))/5;
  21. I("progress").style.width=(100*prog)+"%";
  22. var ipIspArr = I("ip").textContent.split(' - ', 3);
  23. var ip = ipIspArr[0];
  24. var isp = ipIspArr[1];
  25. var addr = ipIspArr[2] === undefined? '' :ipIspArr[2];
  26. var progress = Math.floor(100*prog);
  27. if (progress > 20 && (progress % 10 == 0)) {
  28. var params = 'ip='+ip+'&isp='+isp+'&addr='+addr+'&dspeed='+I("dlText").textContent+'&uspeed='+I("ulText").textContent+'&ping='+I("pingText").textContent
  29. +'&jitter='+I("jitText").textContent;
  30. xhr.open('POST', url_report, true);
  31. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  32. xhr.send(params);
  33. }
  34. }
  35. s.onend=function(aborted){ //callback for test ended/aborted
  36. I("startStopBtn").className=""; //show start button again
  37. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  38. initUI();
  39. }
  40. }
  41. function startStop(){ //start/stop button pressed
  42. if(s.getState()==3){
  43. //speedtest is running, abort
  44. s.abort();
  45. }else{
  46. //test is not running, begin
  47. s.start();
  48. I("startStopBtn").className="running";
  49. }
  50. }
  51. //function to (re)initialize UI
  52. function initUI(){
  53. I("dlText").textContent="";
  54. I("ulText").textContent="";
  55. I("pingText").textContent="";
  56. I("jitText").textContent="";
  57. I("ip").textContent="";
  58. }
  59. function I(id){return document.getElementById(id);}
  60. </script>
  61. <style type="text/css">
  62. html,body{
  63. border:none; padding:0; margin:0;
  64. background:#FFFFFF;
  65. color:#202020;
  66. }
  67. body{
  68. text-align:center;
  69. font-family:"Roboto",sans-serif;
  70. }
  71. h1{
  72. color:#404040;
  73. }
  74. #startStopBtn{
  75. display:inline-block;
  76. margin:0 auto;
  77. color:#6060AA;
  78. background-color:rgba(0,0,0,0);
  79. border:0.15em solid #6060FF;
  80. border-radius:0.3em;
  81. transition:all 0.3s;
  82. box-sizing:border-box;
  83. width:8em; height:3em;
  84. line-height:2.7em;
  85. cursor:pointer;
  86. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  87. }
  88. #startStopBtn:hover{
  89. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  90. }
  91. #startStopBtn.running{
  92. background-color:#FF3030;
  93. border-color:#FF6060;
  94. color:#FFFFFF;
  95. }
  96. #startStopBtn:before{
  97. content:"Start";
  98. }
  99. #startStopBtn.running:before{
  100. content:"Abort";
  101. }
  102. #test{
  103. margin-top:2em;
  104. margin-bottom:12em;
  105. }
  106. div.testArea{
  107. display:inline-block;
  108. width:14em;
  109. height:9em;
  110. position:relative;
  111. box-sizing:border-box;
  112. }
  113. div.testName{
  114. position:absolute;
  115. top:0.1em; left:0;
  116. width:100%;
  117. font-size:1.4em;
  118. z-index:9;
  119. }
  120. div.meterText{
  121. position:absolute;
  122. bottom:1.5em; left:0;
  123. width:100%;
  124. font-size:2.5em;
  125. z-index:9;
  126. }
  127. #dlText{
  128. color:#6060AA;
  129. }
  130. #ulText{
  131. color:#309030;
  132. }
  133. #pingText,#jitText{
  134. color:#AA6060;
  135. }
  136. div.meterText:empty:before{
  137. color:#505050 !important;
  138. content:"0.00";
  139. }
  140. div.unit{
  141. position:absolute;
  142. bottom:2em; left:0;
  143. width:100%;
  144. z-index:9;
  145. }
  146. div.testGroup{
  147. display:inline-block;
  148. }
  149. @media all and (max-width:65em){
  150. body{
  151. font-size:1.5vw;
  152. }
  153. }
  154. @media all and (max-width:40em){
  155. body{
  156. font-size:0.8em;
  157. }
  158. div.testGroup{
  159. display:block;
  160. margin: 0 auto;
  161. }
  162. }
  163. #progressBar{
  164. width:90%;
  165. height:0.3em;
  166. background-color:#EEEEEE;
  167. position:relative;
  168. display:block;
  169. margin:0 auto;
  170. margin-bottom:2em;
  171. }
  172. #progress{
  173. position:absolute;
  174. top:0; left:0;
  175. height:100%;
  176. width:0%;
  177. transition: width 2s;
  178. background-color:#90BBFF;
  179. }
  180. </style>
  181. </head>
  182. <body>
  183. <h1>LibreSpeed Example</h1>
  184. <div id="startStopBtn" onclick="startStop()"></div>
  185. <div id="test">
  186. <div id="progressBar"><div id="progress"></div></div>
  187. <div class="testGroup">
  188. <div class="testArea">
  189. <div class="testName">Download</div>
  190. <div id="dlText" class="meterText"></div>
  191. <div class="unit">Mbps</div>
  192. </div>
  193. <div class="testArea">
  194. <div class="testName">Upload</div>
  195. <div id="ulText" class="meterText"></div>
  196. <div class="unit">Mbps</div>
  197. </div>
  198. </div>
  199. <div class="testGroup">
  200. <div class="testArea">
  201. <div class="testName">Ping</div>
  202. <div id="pingText" class="meterText"></div>
  203. <div class="unit">ms</div>
  204. </div>
  205. <div class="testArea">
  206. <div class="testName">Jitter</div>
  207. <div id="jitText" class="meterText"></div>
  208. <div class="unit">ms</div>
  209. </div>
  210. </div>
  211. <div id="ipArea">
  212. IP Address: <span id="ip"></span>
  213. </div>
  214. </div>
  215. <a href="https://github.com/BadApple9/speedtest-x">speedtest-x 项目地址</a>
  216. <script type="text/javascript">
  217. initUI();
  218. </script>
  219. </body>
  220. </html>