Переглянути джерело

feat(monkey): 添加 URL 参数处理功能

- 从 URL 中读取 type 参数
- 根据 type 参数选择对应的 URL 列表
- 如果没有提供 type 参数,则显示警告并返回
- 更新 push.sh 脚本,上传 to_nas.html 文件到服务器
tianyunperfect 5 місяців тому
батько
коміт
d0f6cd7a87
3 змінених файлів з 154 додано та 4 видалено
  1. 15 2
      monkey/域名检测跳转.js
  2. 2 2
      simple-demo/push.sh
  3. 137 0
      simple-demo/to_nas.html

+ 15 - 2
monkey/域名检测跳转.js

@@ -1,5 +1,18 @@
-javascript: (function () {
-  const urls = ["http://192.168.3.36:9002/", "https://tri.tianyunperfect.cn/"];
+(function () {
+  // 从url里面读取参数 type
+  const urlParams = new URLSearchParams(window.location.search);
+  const type = urlParams.get("type");
+  // 如果type为空,则警告lert("请输入type参数")
+  if (!type) {
+    alert("请输入type参数");
+    return;
+  }
+
+  let urlMap = {
+    tri: ["http://192.168.3.36:9002/", "https://tri.tianyunperfect.cn/"],
+  };
+
+  const urls = urlMap[type];
 
   function checkUrl(url) {
     return new Promise((resolve) => {

+ 2 - 2
simple-demo/push.sh

@@ -3,9 +3,9 @@ cd "$(dirname "$0")"
 
 remote=root@www.tianyunperfect.cn
 
-
 # 在本地执行的代码,比如上传文件到服务器 scp 本地文件 user@ip:远程目录
 #scp -i ~/.ssh/tianyun.pem -r ../simple-demo/* ${remote}:/www/wwwroot/web.tianyunperfect.cn/simple
-scp -r memos.html ${remote}:/www/wwwroot/web.tianyunperfect.cn/simple
+scp -r to_nas.html ${remote}:/www/wwwroot/web.tianyunperfect.cn/simple
+# scp -r memos.html ${remote}:/www/wwwroot/web.tianyunperfect.cn/simple
 
 echo ok

+ 137 - 0
simple-demo/to_nas.html

@@ -0,0 +1,137 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>自动跳转</title>
+</head>
+<body>
+    跳转中......
+</body>
+<script>
+    (function () {
+            // 从url里面读取参数 type
+            const urlParams = new URLSearchParams(window.location.search);
+            const type = urlParams.get("type");
+            // 如果type为空,则警告lert("请输入type参数")
+            if (!type) {
+                showStatus("请输入type参数");
+                return;
+            }
+
+            let urlMap = {
+                tri: ["http://192.168.3.36:9002/", "https://tri.tianyunperfect.cn/"],
+                photo: ["http://192.168.3.36:8063/", "https://photo.n.tianyunperfect.cn/","https://photo.tianyunperfect.cn/photos"]
+            };
+
+            const urls = urlMap[type];
+
+            function checkUrl(url) {
+                return new Promise((resolve) => {
+                    const startTime = performance.now();
+                    const img = new Image();
+                    let isResolved = false;
+
+                    img.onload = function () {
+                        if (!isResolved) {
+                            isResolved = true;
+                            resolve({
+                                url: url,
+                                accessible: true,
+                                responseTime: performance.now() - startTime,
+                            });
+                        }
+                    };
+
+                    img.onerror = function () {
+                        if (!isResolved) {
+                            isResolved = true;
+                            resolve({
+                                url: url,
+                                accessible: true,
+                                responseTime: performance.now() - startTime,
+                            });
+                        }
+                    };
+
+                    setTimeout(() => {
+                        if (!isResolved) {
+                            isResolved = true;
+                            resolve({
+                                url: url,
+                                accessible: false,
+                                responseTime: 99999,
+                            });
+                        }
+                    }, 5000);
+
+                    const timestamp = new Date().getTime();
+                    img.src = url + `favicon.ico?t=${timestamp}`;
+                });
+            }
+
+            function showStatus(message, isError = false) {
+                let statusDiv = document.getElementById("url-checker-status");
+                if (!statusDiv) {
+                    statusDiv = document.createElement("div");
+                    statusDiv.id = "url-checker-status";
+                    statusDiv.style.cssText = `
+                position: fixed;
+                top: 10px;
+                right: 10px;
+                padding: 10px;
+                background: ${isError ? "rgba(255, 0, 0, 0.8)" : "rgba(0, 0, 0, 0.8)"
+                        };
+                color: white;
+                border-radius: 5px;
+                z-index: 999999;
+                font-family: Arial, sans-serif;
+                font-size: 14px;
+                max-width: 300px;
+                word-break: break-word;
+            `;
+                    document.body.appendChild(statusDiv);
+                }
+                statusDiv.textContent = message;
+                return statusDiv;
+            }
+
+            async function checkAllUrls() {
+                const statusDiv = showStatus("正在检测网络连接...");
+
+                try {
+                    const results = await Promise.all(urls.map((url) => checkUrl(url)));
+                    const accessibleUrls = results.filter((result) => result.accessible);
+                    console.log("检测结果:", results);
+
+                    if (accessibleUrls.length === 0) {
+                        showStatus("所有地址均无法访问!", true);
+                        setTimeout(() => statusDiv.remove(), 3000);
+                        return;
+                    }
+
+                    const fastestUrl = accessibleUrls.reduce((prev, current) =>
+                        prev.responseTime < current.responseTime ? prev : current
+                    );
+
+                    showStatus(
+                        `正在跳转到最快地址: ${fastestUrl.url}\n响应时间: ${Math.round(
+                            fastestUrl.responseTime
+                        )}ms`
+                    );
+
+                    setTimeout(() => {
+                        window.location.href = fastestUrl.url;
+                        statusDiv.remove();
+                    }, 1500);
+                } catch (error) {
+                    console.error("检测过程出错:", error);
+                    showStatus("检测过程出错,请稍后重试", true);
+                    setTimeout(() => statusDiv.remove(), 3000);
+                }
+            }
+
+            checkAllUrls();
+        })();
+</script>
+</html>