修复主题
This commit is contained in:
@@ -1,5 +1,84 @@
|
||||
// main.js - 主脚本文件
|
||||
|
||||
// 页面数据缓存对象
|
||||
const pageDataCache = {
|
||||
// 页面初始化状态
|
||||
initializedPages: {},
|
||||
// 页面数据缓存
|
||||
data: {
|
||||
dashboard: {
|
||||
timestamp: 0,
|
||||
data: null,
|
||||
expiry: 5 * 60 * 1000 // 5分钟过期
|
||||
},
|
||||
logs: {
|
||||
timestamp: 0,
|
||||
data: null,
|
||||
expiry: 5 * 60 * 1000 // 5分钟过期
|
||||
},
|
||||
shield: {
|
||||
timestamp: 0,
|
||||
data: null,
|
||||
expiry: 10 * 60 * 1000 // 10分钟过期
|
||||
},
|
||||
hosts: {
|
||||
timestamp: 0,
|
||||
data: null,
|
||||
expiry: 10 * 60 * 1000 // 10分钟过期
|
||||
},
|
||||
gfwlist: {
|
||||
timestamp: 0,
|
||||
data: null,
|
||||
expiry: 10 * 60 * 1000 // 10分钟过期
|
||||
}
|
||||
},
|
||||
|
||||
// 检查缓存是否有效
|
||||
isCacheValid: function(page) {
|
||||
const cache = this.data[page];
|
||||
if (!cache || !cache.data) return false;
|
||||
const now = Date.now();
|
||||
return (now - cache.timestamp) < cache.expiry;
|
||||
},
|
||||
|
||||
// 获取缓存数据
|
||||
getCache: function(page) {
|
||||
if (this.isCacheValid(page)) {
|
||||
return this.data[page].data;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
// 设置缓存数据
|
||||
setCache: function(page, data) {
|
||||
if (this.data[page]) {
|
||||
this.data[page].data = data;
|
||||
this.data[page].timestamp = Date.now();
|
||||
}
|
||||
},
|
||||
|
||||
// 清除缓存
|
||||
clearCache: function(page) {
|
||||
if (this.data[page]) {
|
||||
this.data[page].data = null;
|
||||
this.data[page].timestamp = 0;
|
||||
}
|
||||
},
|
||||
|
||||
// 标记页面已初始化
|
||||
markPageInitialized: function(page) {
|
||||
this.initializedPages[page] = true;
|
||||
},
|
||||
|
||||
// 检查页面是否已初始化
|
||||
isPageInitialized: function(page) {
|
||||
return this.initializedPages[page] || false;
|
||||
}
|
||||
};
|
||||
|
||||
// 页面可见性状态
|
||||
let isPageVisible = true;
|
||||
|
||||
// 页面初始化函数 - 根据当前hash值初始化对应页面
|
||||
function initPageByHash() {
|
||||
const hash = window.location.hash.substring(1);
|
||||
@@ -46,31 +125,51 @@ function initPageByHash() {
|
||||
if (hash === 'shield') {
|
||||
setTimeout(() => {
|
||||
if (typeof initShieldPage === 'function') {
|
||||
initShieldPage();
|
||||
// 检查页面是否已经初始化
|
||||
if (!pageDataCache.isPageInitialized('shield') || !pageDataCache.isCacheValid('shield')) {
|
||||
initShieldPage();
|
||||
pageDataCache.markPageInitialized('shield');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
} else if (hash === 'hosts') {
|
||||
setTimeout(() => {
|
||||
if (typeof initHostsPage === 'function') {
|
||||
initHostsPage();
|
||||
// 检查页面是否已经初始化
|
||||
if (!pageDataCache.isPageInitialized('hosts') || !pageDataCache.isCacheValid('hosts')) {
|
||||
initHostsPage();
|
||||
pageDataCache.markPageInitialized('hosts');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
} else if (hash === 'gfwlist') {
|
||||
setTimeout(() => {
|
||||
if (typeof initGFWListPage === 'function') {
|
||||
initGFWListPage();
|
||||
// 检查页面是否已经初始化
|
||||
if (!pageDataCache.isPageInitialized('gfwlist') || !pageDataCache.isCacheValid('gfwlist')) {
|
||||
initGFWListPage();
|
||||
pageDataCache.markPageInitialized('gfwlist');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
} else if (hash === 'logs') {
|
||||
setTimeout(() => {
|
||||
if (typeof initLogsPage === 'function') {
|
||||
initLogsPage();
|
||||
// 检查页面是否已经初始化
|
||||
if (!pageDataCache.isPageInitialized('logs') || !pageDataCache.isCacheValid('logs')) {
|
||||
initLogsPage();
|
||||
pageDataCache.markPageInitialized('logs');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
} else if (hash === 'dashboard') {
|
||||
setTimeout(() => {
|
||||
if (typeof loadDashboardData === 'function') {
|
||||
loadDashboardData();
|
||||
// 检查页面是否已经初始化
|
||||
if (!pageDataCache.isPageInitialized('dashboard') || !pageDataCache.isCacheValid('dashboard')) {
|
||||
loadDashboardData();
|
||||
pageDataCache.markPageInitialized('dashboard');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
@@ -407,6 +506,39 @@ function updateThemeIcon(toggleElement, isDark) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理页面可见性变化
|
||||
function handleVisibilityChange() {
|
||||
if (document.visibilityState === 'visible') {
|
||||
isPageVisible = true;
|
||||
console.log('页面变为可见');
|
||||
|
||||
// 当页面重新可见时,检查当前页面是否需要刷新数据
|
||||
const hash = window.location.hash.substring(1);
|
||||
|
||||
// 只有当缓存过期时才重新加载数据
|
||||
if (hash && !pageDataCache.isCacheValid(hash)) {
|
||||
console.log(`缓存已过期,重新加载${hash}页面数据`);
|
||||
|
||||
// 根据当前页面类型重新加载数据
|
||||
if (hash === 'dashboard' && typeof loadDashboardData === 'function') {
|
||||
loadDashboardData();
|
||||
} else if (hash === 'logs' && typeof loadLogs === 'function') {
|
||||
loadLogs();
|
||||
} else if (hash === 'shield' && typeof initShieldPage === 'function') {
|
||||
initShieldPage();
|
||||
} else if (hash === 'hosts' && typeof initHostsPage === 'function') {
|
||||
initHostsPage();
|
||||
}
|
||||
}
|
||||
|
||||
// 更新系统状态
|
||||
updateSystemStatus();
|
||||
} else {
|
||||
isPageVisible = false;
|
||||
console.log('页面变为隐藏');
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化函数
|
||||
function init() {
|
||||
// 设置导航
|
||||
@@ -424,6 +556,9 @@ function init() {
|
||||
// 添加hashchange事件监听,处理浏览器前进/后退按钮
|
||||
window.addEventListener('hashchange', initPageByHash);
|
||||
|
||||
// 添加页面可见性变化监听
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
|
||||
// 定期更新系统状态
|
||||
setInterval(updateSystemStatus, 5000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user