From 00dd2a2eee452c8185158fa488b87c986ce9d388 Mon Sep 17 00:00:00 2001 From: thuanle Date: Sun, 3 Aug 2025 01:28:10 +0700 Subject: [PATCH] Enhance agent.user.js by introducing a dashboard overlay for improved user interaction. Update server management functions and add a new dashboard update interval for real-time updates. Refactor existing overlay functionalities for consistency and clarity. --- agent.user.js | 134 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 52 deletions(-) diff --git a/agent.user.js b/agent.user.js index 62428f8..2f0f1d1 100644 --- a/agent.user.js +++ b/agent.user.js @@ -31,6 +31,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // ====== CONFIGURATION ====== const CONFIG = { HEARTBEAT_INTERVAL: 10000, + DASHBOARD_UPDATE_INTERVAL: 500, // Update dashboard overlay every 0.5 seconds SERVERS: { local: { label: '🏠 Local', url: 'http://localhost:3000' }, prod: { label: '🌐 Prod', url: 'https://baf.thuanle.me' }, @@ -55,7 +56,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); constructor() { this.data = { // Server configuration - Thông tin server hiện tại - server: null, // URL của server hiện tại (từ AppSettings) server_last_seen: null, // Thời gian cuối cùng ping server thành công // Page state - Trạng thái trang hiện tại @@ -84,16 +84,11 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); if (this.initialized) return; this.initialized = true; - // Load server URL từ AppSettings - const serverType = await AppSettings.getServerType(); - const server = CONFIG.SERVERS[serverType] || CONFIG.SERVERS.prod; - // Detect trang hiện tại (không block) const currentPage = BINANCE.page.detectPage(); // Update state (không bao gồm login status) await this.update({ - server, current_page: currentPage, }); @@ -111,7 +106,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); } // ====== GETTER METHODS ====== - getServer() { return this.data.server; } + getServer() { return CONFIG.SERVERS[AppSettings.getServerType()]; } getServerLastSeen() { return this.data.server_last_seen; } /** * Kiểm tra xem server có kết nối không @@ -137,7 +132,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); const oldData = { ...this.data }; Object.assign(this.data, updates); await this.notifyObservers(oldData, this.data); - TL.debug('STATE', 'Updated', updates); + // TL.debug('STATE', 'Updated', updates); } /** @@ -212,7 +207,10 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Server configuration getServerType: () => GM_getValue(AppSettings.key_server_type, 'prod'), - setServerType: (type) => GM_setValue(AppSettings.key_server_type, type), + setServerType: (type) => { + GM_setValue(AppSettings.key_server_type, type); + AppState.update({ server_last_seen: null }); + }, getBotStatus: () => GM_getValue(AppSettings.key_bot_status, AppEnums.BOT_STATUS.IDLE), setBotStatus: (status) => GM_setValue(AppSettings.key_bot_status, status), @@ -536,8 +534,8 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // ====== APP UI ====== const AppUi = { - // ====== STATUS OVERLAY ====== - statusOverlay: { + // ====== DASHBOARD OVERLAY ====== + dashboardOverlay: { element: null, // Get position CSS based on position number @@ -551,10 +549,10 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); } }, - // Create status overlay element + // Create dashboard overlay element create: () => { - if (AppUi.statusOverlay.element) { - document.body.removeChild(AppUi.statusOverlay.element); + if (AppUi.dashboardOverlay.element) { + document.body.removeChild(AppUi.dashboardOverlay.element); } const overlay = document.createElement('div'); @@ -608,7 +606,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); TL.dom.setHover(positionBtn, 'mouseleave', { scale: 1, background: '#007bff' }); overlay.appendChild(positionBtn); - AppUi.statusOverlay.element = overlay; + AppUi.dashboardOverlay.element = overlay; document.body.appendChild(overlay); return overlay; @@ -616,11 +614,11 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Update overlay position updatePosition: () => { - if (!AppUi.statusOverlay.element) return; + if (!AppUi.dashboardOverlay.element) return; - const positionCSS = AppUi.statusOverlay.getPositionCSS(AppSettings.getPopupPosition()); + const positionCSS = AppUi.dashboardOverlay.getPositionCSS(AppSettings.getPopupPosition()); - Object.assign(AppUi.statusOverlay.element.style, positionCSS); + Object.assign(AppUi.dashboardOverlay.element.style, positionCSS); }, // Update overlay content @@ -656,7 +654,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Build content const content = ` -
BAF Agent Status
+
BAF Agent Dashboard
🛟 Safety: ${safetyStatus.enabled ? '🛡️ Safe' : '🚨 Blocked'} @@ -668,7 +666,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); Bot Status: ${statusDisplay}
- 🌐 Server: ${serverLabel} ${serverConnected} + 🌐 Server: ${serverLabel} ${serverConnected}
👤 Login:${loginStatus} @@ -679,12 +677,12 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); `; // Update content (excluding position button) - const contentDiv = AppUi.statusOverlay.element.querySelector('.baf-content') || document.createElement('div'); + const contentDiv = AppUi.dashboardOverlay.element.querySelector('.baf-content') || document.createElement('div'); contentDiv.className = 'baf-content'; contentDiv.innerHTML = content; - if (!AppUi.statusOverlay.element.querySelector('.baf-content')) { - AppUi.statusOverlay.element.appendChild(contentDiv); + if (!AppUi.dashboardOverlay.element.querySelector('.baf-content')) { + AppUi.dashboardOverlay.element.appendChild(contentDiv); } // Add event listener for safety toggle button @@ -701,7 +699,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); AppSettings.setSafetyGuard(true); } // Update the content to reflect the change - AppUi.statusOverlay.updateContent(); + AppUi.dashboardOverlay.updateContent(); }; } @@ -712,44 +710,60 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); const newDebugState = !AppSettings.getDebug(); AppSettings.setDebug(newDebugState); // Update the content to reflect the change - AppUi.statusOverlay.updateContent(); + AppUi.dashboardOverlay.updateContent(); + }; + } + + // Add event listener for server toggle button + const serverToggleBtn = contentDiv.querySelector('#server-toggle-btn'); + if (serverToggleBtn) { + serverToggleBtn.onclick = async () => { + const currentServerType = AppSettings.getServerType(); + const newServerType = currentServerType === 'local' ? 'prod' : 'local'; + + // Update server type in settings + AppSettings.setServerType(newServerType); + await AppState.update({ server_last_seen: null }); + + // Update the content to reflect the change + AppUi.dashboardOverlay.updateContent(); }; } }, - // Show status overlay + // Show dashboard overlay show: () => { - if (!AppUi.statusOverlay.element) { - AppUi.statusOverlay.create(); + if (!AppUi.dashboardOverlay.element) { + AppUi.dashboardOverlay.create(); } - AppUi.statusOverlay.updatePosition(); - AppUi.statusOverlay.updateContent(); - AppUi.statusOverlay.element.style.display = 'block'; + AppUi.dashboardOverlay.updatePosition(); + AppUi.dashboardOverlay.updateContent(); + AppUi.dashboardOverlay.element.style.display = 'block'; }, - // Hide status overlay + // Hide dashboard overlay hide: () => { - if (AppUi.statusOverlay.element) { - AppUi.statusOverlay.element.style.display = 'none'; + if (AppUi.dashboardOverlay.element) { + AppUi.dashboardOverlay.element.style.display = 'none'; } }, - // Toggle status overlay + // Toggle dashboard overlay toggle: () => { if (AppSettings.getPopupVisible()) { - AppUi.statusOverlay.hide(); + AppUi.dashboardOverlay.hide(); AppSettings.setPopupVisible(false); } else { - AppUi.statusOverlay.show(); + AppUi.dashboardOverlay.show(); AppSettings.setPopupVisible(true); } }, - // Initialize status overlay + // Initialize dashboard overlay init: () => { if (AppSettings.getPopupVisible()) { - AppUi.statusOverlay.show(); + AppUi.dashboardOverlay.show(); } } }, @@ -796,7 +810,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Popup toggle menu GM_registerMenuCommand("👁️ Toggle Popup", () => { - AppUi.statusOverlay.toggle(); + AppUi.dashboardOverlay.toggle(); }); } catch (error) { @@ -812,24 +826,45 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); initialize: async () => { await AppState.initialize(); await AppUi.menu.create(); - AppUi.statusOverlay.init(); + AppUi.dashboardOverlay.init(); + + // Setup observers for dashboard overlay updates + AppServices.initObservers(); // Setup interval services AppServices.initInterval(); - - // Detect login status after initialization (non-blocking) - setTimeout(async () => { - await AppState.detectLoginStatus(); - }, 1000); // Delay 1 second to let page load - + TL.log('APP-SERVICES', 'AppServices initialized'); }, + initObservers: () => { + // AppState.subscribe('server_last_seen', () => { + // if (AppSettings.getPopupVisible()) { + // AppUi.dashboardOverlay.updateContent(); + // } + // }); + + // AppState.subscribe('is_logged_in', () => { + // if (AppSettings.getPopupVisible()) { + // AppUi.dashboardOverlay.updateContent(); + // } + // }); + + // AppState.subscribe('current_page', () => { + // if (AppSettings.getPopupVisible()) { + // AppUi.dashboardOverlay.updateContent(); + // } + // }); + }, + // Setup interval services initInterval: () => { // Setup heartbeat interval setInterval(() => AppServices.heartbeat(), CONFIG.HEARTBEAT_INTERVAL); + // Setup dashboard overlay update interval + setInterval(() => AppUi.dashboardOverlay.updateContent(), CONFIG.DASHBOARD_UPDATE_INTERVAL); + TL.debug('APP-SERVICES', 'Interval services started'); }, @@ -838,11 +873,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); return serverLastSeen && new Date() - serverLastSeen < 60000; }, - // Refresh login status - refreshLoginStatus: async () => { - await AppState.detectLoginStatus(); - }, - // ====== HEARTBEAT SYSTEM ====== heartbeat: async () => { if (!AppSettings.getSafetyGuard()) return null;