1
0

index.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. var milestone=0;
  15. var key_prefix=Date.parse(new Date());
  16. s.onupdate=function(data){ //callback to update data in UI
  17. I("ip").textContent=data.clientIp || "获取失败";
  18. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  19. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  20. I("pingText").textContent=data.pingStatus;
  21. I("jitText").textContent=data.jitterStatus;
  22. var prog=(Number(data.dlProgress)*2+Number(data.ulProgress)*2+Number(data.pingProgress))/5;
  23. I("progress").style.width=(100*prog)+"%";
  24. var ipIspArr = I("ip").textContent.split(' - ', 3);
  25. var ip = ipIspArr[0];
  26. var isp = ipIspArr[1];
  27. var addr = ipIspArr[2] === undefined? '' :ipIspArr[2];
  28. var progress = Math.floor(100*prog);
  29. var key = key_prefix + "_" + ip;
  30. if (progress > 20 && (progress % 10 == 0) && progress != milestone) {
  31. console.log(progress);
  32. var params = 'key='+key+'&ip='+ip+'&isp='+isp+'&addr='+addr+'&dspeed='+I("dlText").textContent+'&uspeed='+I("ulText").textContent+'&ping='+I("pingText").textContent
  33. +'&jitter='+I("jitText").textContent;
  34. xhr.timeout = 3000;
  35. xhr.ontimeout = function (e) {
  36. console.log('上报超时');
  37. };
  38. xhr.open('POST', url_report, true);
  39. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  40. xhr.send(params);
  41. milestone = progress;
  42. }
  43. }
  44. s.onend=function(aborted){ //callback for test ended/aborted
  45. I("startStopBtn").className=""; //show start button again
  46. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  47. initUI();
  48. }
  49. }
  50. function startStop(){ //start/stop button pressed
  51. if(s.getState()==3){
  52. //speedtest is running, abort
  53. s.abort();
  54. }else{
  55. //test is not running, begin
  56. s.start();
  57. I("startStopBtn").className="running";
  58. }
  59. }
  60. //function to (re)initialize UI
  61. function initUI(){
  62. I("dlText").textContent="";
  63. I("ulText").textContent="";
  64. I("pingText").textContent="";
  65. I("jitText").textContent="";
  66. I("ip").textContent="";
  67. }
  68. function I(id){return document.getElementById(id);}
  69. </script>
  70. <style type="text/css">
  71. html,body{
  72. border:none; padding:0; margin:0;
  73. background:#FFFFFF;
  74. color:#202020;
  75. }
  76. body{
  77. text-align:center;
  78. font-family:"Roboto",sans-serif;
  79. }
  80. h1{
  81. color:#404040;
  82. }
  83. #startStopBtn{
  84. display:inline-block;
  85. margin:0 auto;
  86. color:#6060AA;
  87. background-color:rgba(0,0,0,0);
  88. border:0.15em solid #6060FF;
  89. border-radius:0.3em;
  90. transition:all 0.3s;
  91. box-sizing:border-box;
  92. width:8em; height:3em;
  93. line-height:2.7em;
  94. cursor:pointer;
  95. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  96. }
  97. #startStopBtn:hover{
  98. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  99. }
  100. #startStopBtn.running{
  101. background-color:#FF3030;
  102. border-color:#FF6060;
  103. color:#FFFFFF;
  104. }
  105. #startStopBtn:before{
  106. content:"Start";
  107. }
  108. #startStopBtn.running:before{
  109. content:"Abort";
  110. }
  111. #test{
  112. margin-top:2em;
  113. margin-bottom:12em;
  114. }
  115. div.testArea{
  116. display:inline-block;
  117. width:14em;
  118. height:9em;
  119. position:relative;
  120. box-sizing:border-box;
  121. }
  122. div.testName{
  123. position:absolute;
  124. top:0.1em; left:0;
  125. width:100%;
  126. font-size:1.4em;
  127. z-index:9;
  128. }
  129. div.meterText{
  130. position:absolute;
  131. bottom:1.5em; left:0;
  132. width:100%;
  133. font-size:2.5em;
  134. z-index:9;
  135. }
  136. #dlText{
  137. color:#6060AA;
  138. }
  139. #ulText{
  140. color:#309030;
  141. }
  142. #pingText,#jitText{
  143. color:#AA6060;
  144. }
  145. div.meterText:empty:before{
  146. color:#505050 !important;
  147. content:"0.00";
  148. }
  149. div.unit{
  150. position:absolute;
  151. bottom:2em; left:0;
  152. width:100%;
  153. z-index:9;
  154. }
  155. div.testGroup{
  156. display:inline-block;
  157. }
  158. @media all and (max-width:65em){
  159. body{
  160. font-size:1.5vw;
  161. }
  162. }
  163. @media all and (max-width:40em){
  164. body{
  165. font-size:0.8em;
  166. }
  167. div.testGroup{
  168. display:block;
  169. margin: 0 auto;
  170. }
  171. }
  172. #progressBar{
  173. width:90%;
  174. height:0.3em;
  175. background-color:#EEEEEE;
  176. position:relative;
  177. display:block;
  178. margin:0 auto;
  179. margin-bottom:2em;
  180. }
  181. #progress{
  182. position:absolute;
  183. top:0; left:0;
  184. height:100%;
  185. width:0%;
  186. transition: width 2s;
  187. background-color:#90BBFF;
  188. }
  189. @media (prefers-color-scheme: dark) {
  190. html, body {
  191. background: #181818;
  192. color: #E0E0E0;
  193. }
  194. h1 {
  195. color: #FFFFFF;
  196. }
  197. #startStopBtn {
  198. color: #AAAAFF;
  199. border-color: #AAAAFF;
  200. }
  201. #startStopBtn:hover {
  202. box-shadow: 0 0 2em rgba(255, 255, 255, 0.1), inset 0 0 1em rgba(255, 255, 255, 0.1);
  203. }
  204. #startStopBtn.running {
  205. background-color: #FF5050;
  206. border-color: #FF8080;
  207. color: #FFFFFF;
  208. }
  209. #progressBar {
  210. background-color: #404040;
  211. }
  212. #progress {
  213. background-color: #6060FF;
  214. }
  215. #dlText {
  216. color: #AAAAFF;
  217. }
  218. #ulText {
  219. color: #50C050;
  220. }
  221. #pingText, #jitText {
  222. color: #FF8080;
  223. }
  224. }
  225. </style>
  226. </head>
  227. <body>
  228. <h1>Speedtest-X</h1>
  229. <div id="startStopBtn" onclick="startStop()"></div>
  230. <div id="test">
  231. <div id="progressBar"><div id="progress"></div></div>
  232. <div class="testGroup">
  233. <div class="testArea">
  234. <div class="testName">下载速度</div>
  235. <div id="dlText" class="meterText"></div>
  236. <div class="unit">Mbps</div>
  237. </div>
  238. <div class="testArea">
  239. <div class="testName">上传速度</div>
  240. <div id="ulText" class="meterText"></div>
  241. <div class="unit">Mbps</div>
  242. </div>
  243. </div>
  244. <div class="testGroup">
  245. <div class="testArea">
  246. <div class="testName">延迟</div>
  247. <div id="pingText" class="meterText"></div>
  248. <div class="unit">ms</div>
  249. </div>
  250. <div class="testArea">
  251. <div class="testName">抖动</div>
  252. <div id="jitText" class="meterText"></div>
  253. <div class="unit">ms</div>
  254. </div>
  255. </div>
  256. <div id="ipArea">
  257. IP地址: <span id="ip"></span>
  258. </div>
  259. </div>
  260. <p><a href="./results.html" target="_blank">查看测速记录</a></p>
  261. <p><a href="https://github.com/BadApple9/speedtest-x" target="_blank">speedtest-x 项目地址</a></p>
  262. <script type="text/javascript">
  263. initUI();
  264. </script>
  265. </body>
  266. </html>