From 73f71286834702ac547354071780f96705b94f11 Mon Sep 17 00:00:00 2001 From: thuanle Date: Thu, 31 Jul 2025 10:56:11 +0700 Subject: [PATCH] Refactor logging in agent.user.js to improve readability and add error indicators. Introduce a welcome message function to display server status upon initialization. --- agent.user.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/agent.user.js b/agent.user.js index a5a600d..050c59f 100644 --- a/agent.user.js +++ b/agent.user.js @@ -41,8 +41,8 @@ // ====== Utility ====== const TL = { - log: (msg, ...args) => GM_log(`[TL] ${msg}`, ...args), - error: (msg, ...args) => GM_log(`[TL] [ERROR] ${msg}`, ...args), + log: (msg, ...args) => GM_log(`[TL]\n${msg}`, ...args), + error: (msg, ...args) => GM_log(`[TL] [ERROR] ❌\n${msg}`, ...args), noti: (title, text, timeout = 2500) => { if (typeof GM_notification === 'function') { GM_notification({ title, text, timeout }); @@ -75,7 +75,7 @@ let data = text; const isJSON = /content-type:\s*application\/json/i.test(resp.responseHeaders || ''); if (isJSON) { try { data = JSON.parse(text); } catch { } } - TL.log(`[GM] ${resp.status} ${url}`, data); + TL.log(`${init.method} ${resp.status} ${url}`, data); resolve({ status: resp.status, ok: resp.status >= 200 && resp.status < 300, @@ -143,7 +143,7 @@ const msg = `Switched to ${nsv.label} (${nsv.url})`; TL.log(msg); TL.noti('BAF Server Switched', msg); - + await TL.delay(300); location.reload(); } catch (e) { @@ -175,6 +175,21 @@ } + + // ====== Main ====== + async function welcome_message() { + + const s = await BAF.getServer(); + const res = await BAF.ping(); + const resStr = + `Server: ${s.label} (${s.url})\n` + + `Status: ${res.ok ? 'Connected ✅' : 'Failed ❌'} (${res.status})`; + + TL.log(resStr); + } + + // ====== Khởi tạo ====== + await welcome_message(); await createGM_Menu(); })(); \ No newline at end of file