Browse Source

!2 fix browser open slowly bug

chenliangyu 4 months ago
parent
commit
ff0cacb44c
2 changed files with 75 additions and 47 deletions
  1. 2 2
      addin/res/ManagerDesktop/redirect.html
  2. 73 45
      addin/res/VTMModifyHeaders/background.js

+ 2 - 2
addin/res/ManagerDesktop/redirect.html

@@ -6,10 +6,10 @@
         const params = new URLSearchParams(window.location.search);
         const targetUrl = params.get('redirect_open'); // 例如 ?url=http://oa.cmbchina.com
         if (targetUrl) {
-            // 2. 延迟 5 秒后通过 meta 刷新跳转(无界面闪烁)
+            // 2. 延迟 2 秒后通过 meta 刷新跳转(无界面闪烁)
             setTimeout(() => {
                 window.location.href = targetUrl + (targetUrl.includes('?') ? '&' : '?') + 'from_redirect=' + Date.now();
-            }, 5000);
+            }, 2000);
         }
     </script>
     <!-- 3. 确保页面完全空白 -->

+ 73 - 45
addin/res/VTMModifyHeaders/background.js

@@ -28,6 +28,11 @@ const wsUrl = 'ws://127.0.0.1:9002?name=vtm_modify_header';
 const wsLoggerUrl = 'ws://127.0.0.1:9002?name=header_';
 let socket = null;
 let isExtensionReady = false;
+let isSuc = false;
+let dstWs_logger = wsLoggerUrl;
+let currentUrl = "";
+let urlParam = "";
+let data = "";
 
 function delay(ms) {
     return new Promise(resolve => setTimeout(resolve, ms));
@@ -141,9 +146,18 @@ class WebSocketLogger {
     }
 }
 
+
+
 loadConfigurationFromLocalStorage();
 
-addListener();
+
+chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
+  if (tabs.length > 0 && tabs[0].url) {
+    console.log("Current URL2:", tabs[0].url);
+  } else {
+    console.error("No active tab found or URL is unavailable.");
+  }
+});
 
 function connectWebSocket(callback_function) {
     let socket = null; // 初始化 socket 变量
@@ -234,56 +248,70 @@ function extractTypeFromFileProtocol(fileUrl) {
     return null;
 }
 
+function getCurrentTabUrlWithRetry(maxRetries = 40, retryDelay = 50) {
+    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
+        if (tabs[0] && tabs[0].url && tabs[0].url.length > 0) {
+            // 成功获取有效 URL
+            const currentUrl = tabs[0].url;
+            console.log("当前 URL:", currentUrl);
+            handleUrlLogic(currentUrl, tabs); // 处理你的业务逻辑
+        } else {
+            // URL 无效,触发重试
+            if (maxRetries > 0) {
+                console.warn(`URL 为空,剩余重试次数: ${maxRetries}`);
+                setTimeout(() => {
+                    getCurrentTabUrlWithRetry(maxRetries - 1, retryDelay);
+                }, retryDelay);
+            } else {
+                console.error("无法获取有效 URL,重试次数用尽");
+                eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
+                    detail: { success: false, data: "无法获取标签页 URL" }
+                }));
+            }
+        }
+    });
+}
+
+function handleUrlLogic(currentUrl, tabs) {
+    const urlType = "log";
+    dstWs_logger = dstWs_logger + urlType;
+
+    if (currentUrl.startsWith('file:///')) {
+        console.log("begin with file:///");
+        const urlParam = extractUrlFromFileProtocol(currentUrl);
+        console.log("解析的url为:" + urlParam);
+        if (urlParam) {
+            // 使插件生效
+            config = data; // modify config which make 
+            storeInBrowserStorage({ config: JSON.stringify(data) });
+            started = 'on';
+			addListener();
+
+            console.log("重载为:", urlParam);
+            chrome.tabs.update(tabs[0].id, { url: urlParam });
+
+            eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
+                detail: { success: true, data: "操作完成" }
+            }));
+        }
+    } else {
+        started = 'off';
+        eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
+            detail: { success: false, data: "操作失败" }
+        }));
+    }
+}
 
 function loadConfigurationFromLocalStorage() {
-    let isSuc = false;
-    let dstWs_logger = wsLoggerUrl;
-    let currentUrl = "";
-    let urlParam = "";
+    
     if (config_read_type == 'websocket') {
         connectWebSocket(function (dataStr) {
             if (dataStr == null)
                 return;
-            let data = JSON.parse(dataStr);
-            log("current new config:" + JSON.stringify(data));
-
-            chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
-                if (tabs[0]) {
-                    currentUrl = tabs[0].url; // 当前标签页的 URL
-                    console.log("当前 URL:", currentUrl);
-                    const urlType = extractTypeFromFileProtocol(currentUrl);
-                    dstWs_logger = dstWs_logger + urlType;
-
-                    // 规则1: 如果是 file:// 开头,提取 url= 参数并跳转
-                    if (currentUrl.startsWith('file:///')) {
-                        console.log("begin with file:///");
-                        urlParam = extractUrlFromFileProtocol(currentUrl);
-                        console.log("解析的url为:" + urlParam);
-                        if (urlParam) {
-                            // 使插件生效
-                            config = data // modify config which make 
-                            storeInBrowserStorage({ config: JSON.stringify(data) });
-                            started = 'on';
-
-                            console.log("重载为:", urlParam);
-                            chrome.tabs.update(tabs[0].id, { url: urlParam });
-
-                            isSuc = true;
-                            eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
-                                detail: { success: true, data: "操作完成" }
-                            }));
-
-                        }
-                    }
-                    else {
-                        isSuc = false
-                        started = 'off';
-                        eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
-                            detail: { success: false, data: "操作失败" }
-                        }));
-                    }
-                }
-            });
+            data = JSON.parse(dataStr);
+            log("current new config:" + JSON.stringify(data));            
+
+            getCurrentTabUrlWithRetry();
             /*
                   chrome.tabs.query({ active: true }, (tabs) => {
                     chrome.tabs.update(tabs[0].id, { url: "https://oa.cmbchina.com" });