Refactor logging in agent.user.js to improve readability and add error indicators. Introduce a welcome message function to display server status upon initialization.

This commit is contained in:
thuanle
2025-07-31 10:56:11 +07:00
parent 3a3b5362e3
commit 73f7128683

View File

@@ -41,8 +41,8 @@
// ====== Utility ====== // ====== Utility ======
const TL = { const TL = {
log: (msg, ...args) => GM_log(`[TL] ${msg}`, ...args), log: (msg, ...args) => GM_log(`[TL]\n${msg}`, ...args),
error: (msg, ...args) => GM_log(`[TL] [ERROR] ${msg}`, ...args), error: (msg, ...args) => GM_log(`[TL] [ERROR] \n${msg}`, ...args),
noti: (title, text, timeout = 2500) => { noti: (title, text, timeout = 2500) => {
if (typeof GM_notification === 'function') { if (typeof GM_notification === 'function') {
GM_notification({ title, text, timeout }); GM_notification({ title, text, timeout });
@@ -75,7 +75,7 @@
let data = text; let data = text;
const isJSON = /content-type:\s*application\/json/i.test(resp.responseHeaders || ''); const isJSON = /content-type:\s*application\/json/i.test(resp.responseHeaders || '');
if (isJSON) { try { data = JSON.parse(text); } catch { } } if (isJSON) { try { data = JSON.parse(text); } catch { } }
TL.log(`[GM] ${resp.status} ${url}`, data); TL.log(`${init.method} ${resp.status} ${url}`, data);
resolve({ resolve({
status: resp.status, status: resp.status,
ok: resp.status >= 200 && resp.status < 300, ok: resp.status >= 200 && resp.status < 300,
@@ -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 ====== // ====== Khởi tạo ======
await welcome_message();
await createGM_Menu(); await createGM_Menu();
})(); })();