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) ======
@@ -123,40 +135,42 @@
async function createGM_Menu() {
const curSrv = await BAF.getServer();
GM_registerMenuCommand(`Server: ${curSrv.label} (${curSrv.url})`, async () => {
try {
const cur = await STORAGE.getServerMode();
const next = (cur === 'local') ? 'prod' : 'local';
await STORAGE.setServerMode(next);
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);
} catch (e) {
TL.error('switch server error', e);
alert('Switch server error: ' + e.message);
}
try {
const cur = await STORAGE.getServerMode();
const next = (cur === 'local') ? 'prod' : 'local';
await STORAGE.setServerMode(next);
const nsv = await BAF.getServer();
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) {
TL.error('switch server error', e);
TL.noti('BAF Server Switched', `Switch server error: ${e.message}`);
}
});
GM_registerMenuCommand('Token', async () => {
try {
const curToken = await STORAGE.getToken();
const input = prompt('Bearer token:', curToken || '');
if (input !== null && input.trim() && input.trim() !== curToken) {
await STORAGE.setToken(input);
try {
const curToken = await STORAGE.getToken();
const input = prompt('Bearer token:', curToken || '');
if (input !== null && input.trim() && input.trim() !== curToken) {
await STORAGE.setToken(input);
}
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);
TL.noti('BAF Server', resStr);
} catch (e) {
const resStr = `ping error: ${e.message}`;
TL.error(resStr);
TL.noti('BAF Server', resStr);
}
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);
alert(resStr);
} catch (e) {
const resStr = `ping error: ${e.message}`;
TL.error(resStr);
alert(resStr);
}
});
}