to_nas.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. };
  30. // 从url里面读取参数 type
  31. const urlParams = new URLSearchParams(window.location.search);
  32. const type = urlParams.get("type");
  33. // 如果type为空,则警告lert("请输入type参数")
  34. if (!type) {
  35. showStatus("请输入type参数");
  36. return;
  37. }
  38. const urls = urlMap[type];
  39. function checkUrl(url) {
  40. return new Promise((resolve) => {
  41. const startTime = performance.now();
  42. const img = new Image();
  43. let isResolved = false;
  44. img.onload = function () {
  45. if (!isResolved) {
  46. isResolved = true;
  47. resolve({
  48. url: url,
  49. accessible: true,
  50. responseTime: performance.now() - startTime,
  51. });
  52. }
  53. };
  54. img.onerror = function () {
  55. if (!isResolved) {
  56. isResolved = true;
  57. resolve({
  58. url: url,
  59. accessible: true,
  60. responseTime: performance.now() - startTime,
  61. });
  62. }
  63. };
  64. setTimeout(() => {
  65. if (!isResolved) {
  66. isResolved = true;
  67. resolve({
  68. url: url,
  69. accessible: false,
  70. responseTime: 99999,
  71. });
  72. }
  73. }, 5000);
  74. const timestamp = new Date().getTime();
  75. img.src = url + `favicon.ico?t=${timestamp}`;
  76. });
  77. }
  78. function showStatus(message, isError = false) {
  79. let statusDiv = document.getElementById("url-checker-status");
  80. if (!statusDiv) {
  81. statusDiv = document.createElement("div");
  82. statusDiv.id = "url-checker-status";
  83. statusDiv.style.cssText = `
  84. position: fixed;
  85. top: 10px;
  86. right: 10px;
  87. padding: 10px;
  88. background: ${
  89. isError ? "rgba(255, 0, 0, 0.8)" : "rgba(0, 0, 0, 0.8)"
  90. };
  91. color: white;
  92. border-radius: 5px;
  93. z-index: 999999;
  94. font-family: Arial, sans-serif;
  95. font-size: 14px;
  96. max-width: 300px;
  97. word-break: break-word;
  98. `;
  99. document.body.appendChild(statusDiv);
  100. }
  101. statusDiv.textContent = message;
  102. return statusDiv;
  103. }
  104. async function checkAllUrls() {
  105. const statusDiv = showStatus("正在检测网络连接...");
  106. try {
  107. const results = await Promise.all(urls.map((url) => checkUrl(url)));
  108. const accessibleUrls = results.filter((result) => result.accessible);
  109. console.log("检测结果:", results);
  110. if (accessibleUrls.length === 0) {
  111. showStatus("所有地址均无法访问!", true);
  112. setTimeout(() => statusDiv.remove(), 3000);
  113. return;
  114. }
  115. const fastestUrl = accessibleUrls.reduce((prev, current) =>
  116. prev.responseTime < current.responseTime ? prev : current
  117. );
  118. showStatus(
  119. `正在跳转到最快地址: ${fastestUrl.url}\n响应时间: ${Math.round(
  120. fastestUrl.responseTime
  121. )}ms`
  122. );
  123. setTimeout(() => {
  124. window.location.href = fastestUrl.url;
  125. statusDiv.remove();
  126. }, 1500);
  127. } catch (error) {
  128. console.error("检测过程出错:", error);
  129. showStatus("检测过程出错,请稍后重试", true);
  130. setTimeout(() => statusDiv.remove(), 3000);
  131. }
  132. }
  133. checkAllUrls();
  134. })();
  135. </script>
  136. </html>