diff --git a/agent.user.js b/agent.user.js index 64488f1..a5a600d 100644 --- a/agent.user.js +++ b/agent.user.js @@ -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,42 +135,44 @@ 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); - } - }); - - 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 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}`); } - 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); - } }); - + + 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); + } + 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); + } + }); + } // ====== Khởi tạo ======