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:
@@ -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) ======
|
||||||
@@ -130,11 +142,13 @@
|
|||||||
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);
|
|
||||||
|
await TL.delay(300);
|
||||||
|
location.reload();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
TL.error('switch server error', 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` +
|
`Server: ${s.label} (${s.url})\n` +
|
||||||
`Status: ${res.ok ? 'Connected ✅' : 'Failed ❌'} (${res.status})`;
|
`Status: ${res.ok ? 'Connected ✅' : 'Failed ❌'} (${res.status})`;
|
||||||
TL.log(resStr);
|
TL.log(resStr);
|
||||||
alert(resStr);
|
TL.noti('BAF Server', resStr);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const resStr = `ping error: ${e.message}`;
|
const resStr = `ping error: ${e.message}`;
|
||||||
TL.error(resStr);
|
TL.error(resStr);
|
||||||
alert(resStr);
|
TL.noti('BAF Server', resStr);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user