Refactor session storage management in agent.user.js by renaming keys for clarity and adding functions to handle current task and step states. Update session handling logic to improve task tracking and data retrieval.

This commit is contained in:
thuanle
2025-08-04 16:25:37 +07:00
parent a5325631d0
commit 42950c77cc

View File

@@ -30,14 +30,16 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
const uuid = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); const uuid = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
const KEYS = { const SESSION_STORAGE_KEYS = {
SESSION_ID: 'aRah9OhHeijee6sho3baequu9phoovah', SESSION_ID: 'aRah9OhHeijee6sho3baequu9phoovah',
SESSION_TOKEN: 'ThiegiecohViuZ1Iecio7gahphiechub', SESSION_TOKEN: 'ThiegiecohViuZ1Iecio7gahphiechub',
CURRENT_TASK: 'Reebo1eitahh2aotumai5jae1neetoh3',
AGENT_TOKEN: 'baf-agent-token', CURRENT_TASK_DATA: 'cheishailoh5keePoo6oe2Quie1gaxah',
CURRENT_STEP: 'eDusaidu2hooweiMoonahng3fua7aoso',
} }
if (!sessionStorage.getItem(KEYS.SESSION_ID)) {
sessionStorage.setItem(KEYS.SESSION_ID, uuid()); if (!sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_ID)) {
sessionStorage.setItem(SESSION_STORAGE_KEYS.SESSION_ID, uuid());
} }
// ====== CONFIGURATION ====== // ====== CONFIGURATION ======
@@ -53,10 +55,23 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
const AppSession = { const AppSession = {
SESSION_ID: sessionStorage.getItem(KEYS.SESSION_ID), SESSION_ID: sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_ID),
getSessionToken: () => sessionStorage.getItem(KEYS.SESSION_TOKEN), getSessionToken: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_TOKEN),
setSessionToken: token => sessionStorage.setItem(KEYS.SESSION_TOKEN, token), setSessionToken: token => sessionStorage.setItem(SESSION_STORAGE_KEYS.SESSION_TOKEN, token),
// Task state - Trạng thái task hiện tại (lưu trong sessionStorage)
getCurrentTask: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_TASK),
setCurrentTask: task => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_TASK, task),
getCurrentTaskData: () => {
const data = sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_TASK_DATA);
return data ? JSON.parse(data) : null;
},
setCurrentTaskData: data => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_TASK_DATA, JSON.stringify(data)),
getCurrentStep: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_STEP),
setCurrentStep: step => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_STEP, step),
} }
// ====== APP ENUMS ====== // ====== APP ENUMS ======
@@ -83,10 +98,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
current_page: null, // Trang hiện tại (detected từ URL) current_page: null, // Trang hiện tại (detected từ URL)
is_logged_in: false, // Trạng thái đăng nhập Binance is_logged_in: false, // Trạng thái đăng nhập Binance
// UI state - Trạng thái UI
is_loading: false, // Đang loading
error_message: null, // Thông báo lỗi
// Session flags - Flags cho session hiện tại (reset khi reload) // Session flags - Flags cho session hiện tại (reset khi reload)
menuCreated: false, // Menu đã được tạo menuCreated: false, // Menu đã được tạo
popupInitialized: false, // Popup đã được khởi tạo popupInitialized: false, // Popup đã được khởi tạo
@@ -139,7 +150,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
getCurrentPage() { return this.data.current_page; } getCurrentPage() { return this.data.current_page; }
getIsLoggedIn() { return this.data.is_logged_in; } getIsLoggedIn() { return this.data.is_logged_in; }
getIsLoading() { return this.data.is_loading; }
getMenuCreated() { return this.data.menuCreated; } getMenuCreated() { return this.data.menuCreated; }
getPopupInitialized() { return this.data.popupInitialized; } getPopupInitialized() { return this.data.popupInitialized; }
getAppInitialized() { return this.data.appInitialized; } getAppInitialized() { return this.data.appInitialized; }
@@ -926,9 +936,9 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
logged_in: AppState.getIsLoggedIn(), logged_in: AppState.getIsLoggedIn(),
current_page: AppState.getCurrentPage(), current_page: AppState.getCurrentPage(),
bot_status: AppSettings.getBotStatus(), bot_status: AppSettings.getBotStatus(),
current_task: null, // TODO: Add to AppState if needed current_task: AppSession.getCurrentTask(),
current_task_data: null, // TODO: Add to AppState if needed current_task_data: AppSession.getCurrentTaskData(),
current_step: null, // TODO: Add to AppState if needed current_step: AppSession.getCurrentStep(),
}; };
// TL.debug(`HEARTBEAT`, `${JSON.stringify(status, null, 2)}`); // TL.debug(`HEARTBEAT`, `${JSON.stringify(status, null, 2)}`);
@@ -948,13 +958,26 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
} }
}, },
getTask: async () => {
const data = {
logged_in: AppState.getIsLoggedIn(),
current_page: AppState.getCurrentPage(),
bot_status: AppSettings.getBotStatus(),
current_task: AppSession.getCurrentTask(),
current_task_data: AppSession.getCurrentTaskData(),
current_step: AppSession.getCurrentStep(),
};
const task = await BAF.getTask(data);
return task;
},
handleTask: (task) => { handleTask: (task) => {
TL.debug('APP-SERVICES', `Task: ${JSON.stringify(task, null, 2)}`); TL.debug('APP-SERVICES', `Task: ${JSON.stringify(task, null, 2)}`);
}, },
start: async () => { start: async () => {
while (true) { while (true) {
const task = await BAF.getTask(); const task = await AppServices.getTask();
if (task.ok) { if (task.ok) {
AppServices.handleTask(task.data); AppServices.handleTask(task.data);
} }