to_nas.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>自动跳转</title>
  7. </head>
  8. <body>
  9. 跳转中......
  10. <!-- 列表,不同的type对应不同的url -->
  11. <li><a href="?type=tri">tri 笔记</a></li>
  12. <li><a href="?type=photo">photo 照片</a></li>
  13. <li><a href="?type=photo_family">photo_family 分享</a></li>
  14. </body>
  15. <script>
  16. (function () {
  17. let urlMap = {
  18. tri: ["http://192.168.3.36:9002/", "https://tri.tianyunperfect.cn/"],
  19. photo_family: [
  20. "http://192.168.3.36:8063/share?key=LSMTb36t",
  21. "https://photo.n.tianyunperfect.cn/share?key=LSMTb36t",
  22. "https://photo.tianyunperfect.cn/share?key=LSMTb36t",
  23. ],
  24. photo: [
  25. "http://192.168.3.36:8063/",
  26. "https://photo.n.tianyunperfect.cn/",
  27. "https://photo.tianyunperfect.cn/photos",
  28. ],
  29. kedao: [
  30. "http://192.168.3.36:8081/",
  31. "https://nkedaoyun.tianyunperfect.cn/#explorer",
  32. "https://kedao.n.tianyunperfect.cn/",
  33. ],
  34. };
  35. // 从url里面读取参数 type
  36. const urlParams = new URLSearchParams(window.location.search);
  37. const type = urlParams.get("type");
  38. // 如果type为空,则警告lert("请输入type参数")
  39. if (!type) {
  40. showStatus("请输入type参数");
  41. return;
  42. }
  43. const urls = urlMap[type];
  44. function checkUrl(url) {
  45. return new Promise((resolve) => {
  46. const startTime = performance.now();
  47. const img = new Image();
  48. let isResolved = false;
  49. img.onload = function () {
  50. if (!isResolved) {
  51. isResolved = true;
  52. resolve({
  53. url: url,
  54. accessible: true,
  55. responseTime: performance.now() - startTime,
  56. });
  57. }
  58. };
  59. img.onerror = function () {
  60. if (!isResolved) {
  61. isResolved = true;
  62. resolve({
  63. url: url,
  64. accessible: true,
  65. responseTime: performance.now() - startTime,
  66. });
  67. }
  68. };
  69. setTimeout(() => {
  70. if (!isResolved) {
  71. isResolved = true;
  72. resolve({
  73. url: url,
  74. accessible: false,
  75. responseTime: 99999,
  76. });
  77. }
  78. }, 5000);
  79. const timestamp = new Date().getTime();
  80. img.src = url + `favicon.ico?t=${timestamp}`;
  81. });
  82. }
  83. function showStatus(message, isError = false) {
  84. let statusDiv = document.getElementById("url-checker-status");
  85. if (!statusDiv) {
  86. statusDiv = document.createElement("div");
  87. statusDiv.id = "url-checker-status";
  88. statusDiv.style.cssText = `
  89. position: fixed;
  90. top: 10px;
  91. right: 10px;
  92. padding: 10px;
  93. background: ${
  94. isError ? "rgba(255, 0, 0, 0.8)" : "rgba(0, 0, 0, 0.8)"
  95. };
  96. color: white;
  97. border-radius: 5px;
  98. z-index: 999999;
  99. font-family: Arial, sans-serif;
  100. font-size: 14px;
  101. max-width: 300px;
  102. word-break: break-word;
  103. `;
  104. document.body.appendChild(statusDiv);
  105. }
  106. statusDiv.textContent = message;
  107. return statusDiv;
  108. }
  109. async function checkAllUrls() {
  110. const statusDiv = showStatus("正在检测网络连接...");
  111. try {
  112. const results = await Promise.all(urls.map((url) => checkUrl(url)));
  113. const accessibleUrls = results.filter((result) => result.accessible);
  114. console.log("检测结果:", results);
  115. if (accessibleUrls.length === 0) {
  116. showStatus("所有地址均无法访问!", true);
  117. setTimeout(() => statusDiv.remove(), 3000);
  118. return;
  119. }
  120. const fastestUrl = accessibleUrls.reduce((prev, current) =>
  121. prev.responseTime < current.responseTime ? prev : current
  122. );
  123. showStatus(
  124. `正在跳转到最快地址: ${fastestUrl.url}\n响应时间: ${Math.round(
  125. fastestUrl.responseTime
  126. )}ms`
  127. );
  128. setTimeout(() => {
  129. window.location.href = fastestUrl.url;
  130. statusDiv.remove();
  131. }, 1500);
  132. } catch (error) {
  133. console.error("检测过程出错:", error);
  134. showStatus("检测过程出错,请稍后重试", true);
  135. setTimeout(() => statusDiv.remove(), 3000);
  136. }
  137. }
  138. checkAllUrls();
  139. })();
  140. </script>
  141. </html>