From 75bc8ab147f468f265f672e55cf96ae5a8708031 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Thu, 22 Jan 2026 09:56:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9D=97=E5=88=B0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E8=B0=83=E8=AF=95=E6=8E=A7=E5=88=B6=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 ArgonDebug 输出方法,支持模块标识前缀 - 为不同模块添加标识:DOM缓存、事件管理、渲染优化、性能监控 - 在管理员控制台的缓存清理功能中添加性能优化模块清理 - 清理 DOM 缓存、事件监听器、内存管理器、渲染优化器 - 优化日志输出,所有日志现在会显示来源模块 --- argon-performance.js | 74 +++++++++++++++++++++++++++++++++----------- functions.php | 33 +++++++++++++++++--- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/argon-performance.js b/argon-performance.js index 3e9abbc..1ef6498 100644 --- a/argon-performance.js +++ b/argon-performance.js @@ -12,31 +12,68 @@ /** * 自定义调试控制台 - * 仅在调试模式下输出日志,避免生产环境污染控制台 + * 输出日志到管理员调试控制台(如果启用)或浏览器控制台 + * 为不同模块添加标识前缀 */ const ArgonDebug = { enabled: false, + useAdminConsole: false, init() { if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) { this.enabled = true; } + // 检查是否有管理员调试控制台 + if (typeof window.argonDebug !== 'undefined' && typeof window.argonDebug.log === 'function') { + this.useAdminConsole = true; + } }, - log(...args) { - if (this.enabled) console.log('[Argon]', ...args); + /** + * 输出日志到管理员控制台或浏览器控制台 + * @param {string} level - 日志级别 (log/warn/error/info) + * @param {string} module - 模块名称 + * @param {...*} args - 日志内容 + */ + _output(level, module, ...args) { + if (!this.enabled) return; + + const prefix = module ? `[${module}]` : '[Argon]'; + const message = args.map(arg => { + if (typeof arg === 'object') { + try { + return JSON.stringify(arg, null, 2); + } catch (e) { + return String(arg); + } + } + return String(arg); + }).join(' '); + + // 如果有管理员控制台,优先使用 + if (this.useAdminConsole && window.argonDebug) { + // 管理员控制台会自动处理日志级别和格式 + console[level](`${prefix} ${message}`); + } else { + // 否则输出到浏览器控制台 + console[level](`${prefix} ${message}`); + } }, - warn(...args) { - if (this.enabled) console.warn('[Argon]', ...args); + log(module, ...args) { + this._output('log', module, ...args); }, - error(...args) { - if (this.enabled) console.error('[Argon]', ...args); + warn(module, ...args) { + this._output('warn', module, ...args); }, - info(...args) { - if (this.enabled) console.info('[Argon]', ...args); + error(module, ...args) { + this._output('error', module, ...args); + }, + + info(module, ...args) { + this._output('info', module, ...args); }, group(label) { @@ -318,7 +355,7 @@ class ArgonDOMCache { // 如果启用了调试模式,输出淘汰信息 if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) { - ArgonDebug.log(`DOM缓存 LRU 淘汰: ${lruKey}`); + ArgonDebug.log('DOM缓存', `LRU 淘汰: ${lruKey}`); } } @@ -375,7 +412,7 @@ class ArgonDOMCache { */ setMaxSize(newMaxSize) { if (newMaxSize < 1) { - ArgonDebug.warn('DOM缓存最大大小必须大于 0'); + ArgonDebug.warn('DOM缓存', 'DOM缓存最大大小必须大于 0'); return; } @@ -403,6 +440,7 @@ class ArgonDOMCache { this.accessTimes.delete(key); this._removeFromAccessOrder(key); + ArgonDebug.log('DOM缓存', `删除缓存项: ${key}`); return true; } } @@ -845,7 +883,7 @@ class ArgonRenderOptimizer { try { readFn(); } catch (error) { - ArgonDebug.error('读取操作错误:', error); + ArgonDebug.error('渲染优化', '读取操作错误:', error); } } @@ -855,7 +893,7 @@ class ArgonRenderOptimizer { try { writeFn(); } catch (error) { - ArgonDebug.error('写入操作错误:', error); + ArgonDebug.error('渲染优化', '写入操作错误:', error); } } @@ -953,7 +991,7 @@ class ArgonRenderOptimizer { try { animationFn(element); } catch (error) { - ArgonDebug.error('动画执行错误:', error); + ArgonDebug.error('渲染优化', '动画执行错误:', error); // 出错时也要清理 this.endAnimation(element); } @@ -1261,7 +1299,7 @@ class ArgonPerformanceMonitor { this.metrics.timestamp = Date.now(); } catch (error) { - ArgonDebug.warn('性能指标记录失败:', error); + ArgonDebug.warn('性能监控', '性能指标记录失败:', error); } } @@ -1442,7 +1480,7 @@ class ArgonPerformanceMonitor { // 如果有问题,在控制台输出警告 if (issues.length > 0) { ArgonDebug.group('性能问题检测'); - issues.forEach(issue => ArgonDebug.warn(issue)); + issues.forEach(issue => ArgonDebug.warn('性能监控', issue)); ArgonDebug.groupEnd(); } @@ -1701,9 +1739,9 @@ class ArgonPerformanceMonitor { // 输出建议 if (suggestions.length > 0) { - suggestions.forEach(suggestion => ArgonDebug.log(suggestion)); + suggestions.forEach(suggestion => ArgonDebug.log('性能监控', suggestion)); } else { - ArgonDebug.log('✅ 未检测到明显的性能问题,继续保持!'); + ArgonDebug.log('性能监控', '✅ 未检测到明显的性能问题,继续保持!'); } ArgonDebug.groupEnd(); diff --git a/functions.php b/functions.php index a04ad55..4b9c12e 100644 --- a/functions.php +++ b/functions.php @@ -360,6 +360,7 @@ function argon_clear_all_caches() { $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_timeout_puc_%'"); // 触发缓存清理钩子,允许其他插件响应 + // 这也会触发前端 JavaScript 清理性能优化模块的缓存 do_action('argon_cache_cleared'); } @@ -882,11 +883,35 @@ function argon_debug_console_script() { window.argonDebug={toggle:function(){panel.classList.toggle('show');if(panel.classList.contains('show')&¤tFilter==='resources'){collectResources();renderResources();}},clear:function(){logs=[];counts={all:0,log:0,warn:0,error:0,resources:counts.resources};updateCounts();renderLogs();},clearCache:function(){ // 清除浏览器缓存 var cleared = []; - // 1. 清除 localStorage + + // 1. 清除性能优化模块缓存 + try { + if (typeof argonDOMCache !== 'undefined' && argonDOMCache) { + argonDOMCache.clear(); + cleared.push('DOM缓存'); + } + if (typeof argonEventManager !== 'undefined' && argonEventManager) { + argonEventManager.clear(); + cleared.push('事件监听器'); + } + if (typeof argonMemoryManager !== 'undefined' && argonMemoryManager) { + argonMemoryManager.clearAll(); + cleared.push('内存管理器'); + } + if (typeof argonRenderOptimizer !== 'undefined' && argonRenderOptimizer) { + argonRenderOptimizer.clearAllAnimations(); + cleared.push('渲染优化器'); + } + console.log(''); + } catch(e) { + console.warn(':', e); + } + + // 2. 清除 localStorage try { var lsLen = localStorage.length; localStorage.clear(); if(lsLen > 0) cleared.push('localStorage ('+lsLen+' items)'); } catch(e) {} - // 2. 清除 sessionStorage + // 3. 清除 sessionStorage try { var ssLen = sessionStorage.length; sessionStorage.clear(); if(ssLen > 0) cleared.push('sessionStorage ('+ssLen+' items)'); } catch(e) {} - // 3. 清除 Service Worker 缓存 + // 4. 清除 Service Worker 缓存 if ('caches' in window) { caches.keys().then(function(names) { var count = names.length; @@ -896,7 +921,7 @@ function argon_debug_console_script() { } }); } - // 4. 注销 Service Worker + // 5. 注销 Service Worker if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistrations().then(function(registrations) { registrations.forEach(function(registration) { registration.unregister(); });