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_registerMenuCommand
// @grant GM_notification // @grant GM_notification
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @grant GM_log
// @connect baf.thuanle.me // @connect baf.thuanle.me
// @connect localhost // @connect localhost
// @downloadURL https://git.thuanle.me/public/binance-alpha-farm-agent/raw/branch/main/agent.user.js // @downloadURL https://git.thuanle.me/public/binance-alpha-farm-agent/raw/branch/main/agent.user.js
@@ -40,8 +41,19 @@
// ====== Utility ====== // ====== Utility ======
const TL = { const TL = {
log: (msg, ...args) => console.log(`[TL] ${msg}`, ...args), log: (msg, ...args) => GM_log(`[TL] ${msg}`, ...args),
error: (msg, ...args) => console.error(`[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) ====== // ====== Network helpers (GM_xmlhttpRequest) ======
@@ -123,40 +135,42 @@
async function createGM_Menu() { async function createGM_Menu() {
const curSrv = await BAF.getServer(); const curSrv = await BAF.getServer();
GM_registerMenuCommand(`Server: ${curSrv.label} (${curSrv.url})`, async () => { GM_registerMenuCommand(`Server: ${curSrv.label} (${curSrv.url})`, async () => {
try { try {
const cur = await STORAGE.getServerMode(); const cur = await STORAGE.getServerMode();
const next = (cur === 'local') ? 'prod' : 'local'; const next = (cur === 'local') ? 'prod' : 'local';
await STORAGE.setServerMode(next); await STORAGE.setServerMode(next);
const nsv = await BAF.getServer(); const nsv = await BAF.getServer();
const msg = `Switched to ${nsv.label} (${nsv.url})`; const msg = `Switched to ${nsv.label} (${nsv.url})`;
TL.log(msg); TL.log(msg);
GM_notification?.({ title: 'BAF Server Switched', text: msg, timeout: 2500 }); TL.noti('BAF Server Switched', msg);
alert(msg);
} catch (e) { await TL.delay(300);
TL.error('switch server error', e); location.reload();
alert('Switch server error: ' + e.message); } catch (e) {
} TL.error('switch server error', e);
TL.noti('BAF Server Switched', `Switch server error: ${e.message}`);
}
}); });
GM_registerMenuCommand('Token', async () => { GM_registerMenuCommand('Token', async () => {
try { try {
const curToken = await STORAGE.getToken(); const curToken = await STORAGE.getToken();
const input = prompt('Bearer token:', curToken || ''); const input = prompt('Bearer token:', curToken || '');
if (input !== null && input.trim() && input.trim() !== curToken) { if (input !== null && input.trim() && input.trim() !== curToken) {
await STORAGE.setToken(input); 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);
}
}); });
} }