Enhance agent.user.js by adding GM_log support for logging, implementing a notification utility, and improving server mode switching and token management with better error handling and user feedback.

This commit is contained in:
thuanle
2025-07-31 10:44:12 +07:00
parent ab0a553b96
commit 3a3b5362e3

View File

@@ -10,6 +10,7 @@
// @grant GM_registerMenuCommand
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @grant GM_log
// @connect baf.thuanle.me
// @connect localhost
// @downloadURL https://git.thuanle.me/public/binance-alpha-farm-agent/raw/branch/main/agent.user.js
@@ -40,8 +41,19 @@
// ====== Utility ======
const TL = {
log: (msg, ...args) => console.log(`[TL] ${msg}`, ...args),
error: (msg, ...args) => console.error(`[TL] ${msg}`, ...args),
log: (msg, ...args) => GM_log(`[TL] ${msg}`, ...args),
error: (msg, ...args) => GM_log(`[TL] [ERROR] ${msg}`, ...args),
noti: (title, text, timeout = 2500) => {
if (typeof GM_notification === 'function') {
GM_notification({ title, text, timeout });
return true;
}
alert(`${title}\n${text}`); // fallback
return false;
},
delay: (ms) => new Promise(resolve => setTimeout(resolve, ms)),
};
// ====== Network helpers (GM_xmlhttpRequest) ======
@@ -130,11 +142,13 @@
const nsv = await BAF.getServer();
const msg = `Switched to ${nsv.label} (${nsv.url})`;
TL.log(msg);
GM_notification?.({ title: 'BAF Server Switched', text: msg, timeout: 2500 });
alert(msg);
TL.noti('BAF Server Switched', msg);
await TL.delay(300);
location.reload();
} catch (e) {
TL.error('switch server error', e);
alert('Switch server error: ' + e.message);
TL.noti('BAF Server Switched', `Switch server error: ${e.message}`);
}
});
@@ -151,11 +165,11 @@
`Server: ${s.label} (${s.url})\n` +
`Status: ${res.ok ? 'Connected ✅' : 'Failed ❌'} (${res.status})`;
TL.log(resStr);
alert(resStr);
TL.noti('BAF Server', resStr);
} catch (e) {
const resStr = `ping error: ${e.message}`;
TL.error(resStr);
alert(resStr);
TL.noti('BAF Server', resStr);
}
});