From c353e60a91d407a75872feb181d2a0eb49581a50 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Thu, 15 Jan 2026 16:20:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 按钮移至页脚底部,主要面向移动端调试 - 显示所有 log/warn/error 信息(不再禁用 console) - 新增版本信息面板(Theme/WP/PHP/Git/UA) - 新增日志分类 Tab(全部/日志/警告/错误) - 电脑端窗口支持拖动 - 移动端自适应全屏显示 --- footer.php | 7 ++ functions.php | 274 ++++++++++++++++---------------------------------- 2 files changed, 94 insertions(+), 187 deletions(-) diff --git a/footer.php b/footer.php index f418c7b..0f14f3e 100644 --- a/footer.php +++ b/footer.php @@ -14,6 +14,13 @@ + + diff --git a/functions.php b/functions.php index 4d36b3e..ba7eb7a 100644 --- a/functions.php +++ b/functions.php @@ -460,7 +460,23 @@ function argon_clear_muted_errors() { } add_action('wp_ajax_argon_clear_muted_errors', 'argon_clear_muted_errors'); -// 输出调试控制台脚本 +// 在 footer 中输出调试按钮(放在页脚底部) +function argon_debug_console_footer_button() { + if (get_option('argon_enable_debug_console', 'false') != 'true') { + return; + } + ?> + + - - -
- -
- - -
+
+
+
+
+ Theme: + WP: + PHP: + Git: + UA: +
+
+ + + +
-
@@ -529,159 +561,27 @@ function argon_debug_console_script() { var mutedHashes = ; var ajaxUrl = ''; var nonce = ''; - var errorCount = 0; - var logs = []; - - // 生成错误哈希 - function hashError(msg, source) { - var str = (msg || '') + '|' + (source || ''); - var hash = 0; - for (var i = 0; i < str.length; i++) { - hash = ((hash << 5) - hash) + str.charCodeAt(i); - hash = hash & hash; - } - return 'e' + Math.abs(hash).toString(16); - } - - // 检查错误是否被屏蔽 - function isMuted(hash) { - return mutedHashes.indexOf(hash) !== -1; - } - - // 显示错误通知 - function showErrorToast(msg) { - var toast = document.getElementById('argon-error-toast'); - if (!toast) return; - toast.querySelector('.message').textContent = msg.length > 100 ? msg.substring(0, 100) + '...' : msg; - toast.classList.add('show'); - setTimeout(function() { - toast.classList.remove('show'); - }, 5000); - } - - // 添加日志到控制台 - function addLog(type, args, source) { - var msg = Array.prototype.slice.call(args).map(function(a) { - if (typeof a === 'object') { - try { return JSON.stringify(a); } catch(e) { return String(a); } - } - return String(a); - }).join(' '); - - var hash = hashError(msg, source); - var time = new Date().toLocaleTimeString(); - - logs.push({ type: type, msg: msg, source: source, time: time, hash: hash }); - - if (type === 'error') { - errorCount++; - updateBadge(); - - // 如果未被屏蔽,显示通知 - if (!isMuted(hash)) { - var userMsg = isAdmin ? msg : ''; - showErrorToast(userMsg); - } - } - - renderLogs(); - } - - // 更新错误计数 - function updateBadge() { - var badge = document.querySelector('#argon-debug-btn .badge'); - if (badge) { - badge.textContent = errorCount; - badge.style.display = errorCount > 0 ? 'block' : 'none'; - } - } - - // 渲染日志列表 - function renderLogs() { - var body = document.getElementById('argon-debug-console-body'); - if (!body) return; - - body.innerHTML = logs.map(function(log) { - var muteBtn = isAdmin && log.type === 'error' ? - '' : ''; - return '
' + - muteBtn + - '' + log.time + '' + - '' + log.msg.replace(//g, '>') + '' + - (log.source ? '' + log.source + '' : '') + - '
'; - }).join(''); - - body.scrollTop = body.scrollHeight; - } - - // 拦截 console 方法 - var originalConsole = { - log: console.log, - warn: console.warn, - error: console.error - }; - - // 禁用 console.log 和 console.warn(生产环境) - console.log = function() {}; - console.warn = function() {}; - - // 拦截 console.error - console.error = function() { - originalConsole.error.apply(console, arguments); - addLog('error', arguments); - }; - - // 捕获全局错误 - window.addEventListener('error', function(e) { - var source = e.filename ? e.filename + ':' + e.lineno + ':' + e.colno : ''; - addLog('error', [e.message], source); - }); - - // 捕获 Promise 错误 - window.addEventListener('unhandledrejection', function(e) { - addLog('error', ['Unhandled Promise Rejection: ' + (e.reason ? (e.reason.message || e.reason) : 'Unknown')]); - }); - - // 暴露全局方法 - window.argonDebug = { - toggle: function() { - var console = document.getElementById('argon-debug-console'); - if (console) console.classList.toggle('show'); - }, - clear: function() { - logs = []; - errorCount = 0; - updateBadge(); - renderLogs(); - }, - mute: function(hash, msg, source) { - if (!isAdmin) return; - - var xhr = new XMLHttpRequest(); - xhr.open('POST', ajaxUrl); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.onload = function() { - if (xhr.status === 200) { - mutedHashes.push(hash); - renderLogs(); - } - }; - xhr.send('action=argon_mute_error&nonce=' + nonce + '&error_hash=' + hash + '&error_message=' + msg + '&error_source=' + source); - }, - // 允许管理员手动记录 - log: function() { - if (isAdmin) addLog('log', arguments); - } - }; - - // 绑定按钮事件 - var btn = document.getElementById('argon-debug-btn'); - if (btn) { - btn.addEventListener('click', function() { - argonDebug.toggle(); - }); - } + var logs = [], counts = {all:0,log:0,warn:0,error:0}, currentFilter = 'all'; + var uaEl = document.getElementById('argon-debug-ua'); + if(uaEl){uaEl.textContent=navigator.userAgent;uaEl.title=navigator.userAgent;} + function hashError(m,s){var str=(m||'')+'|'+(s||''),h=0;for(var i=0;i';t.querySelector('.message').textContent=dm.length>80?dm.substring(0,80)+'...':dm;t.classList.add('show');setTimeout(function(){t.classList.remove('show');},5000);} + function updateCounts(){document.getElementById('count-all').textContent=counts.all;document.getElementById('count-log').textContent=counts.log;document.getElementById('count-warn').textContent=counts.warn;document.getElementById('count-error').textContent=counts.error;var b=document.getElementById('argon-debug-error-count');if(b){b.textContent=counts.error;b.style.display=counts.error>0?'inline':'none';}} + function addLog(type,args,source){var msg=Array.prototype.slice.call(args).map(function(a){if(typeof a==='object'){try{return JSON.stringify(a,null,2);}catch(e){return String(a);}}return String(a);}).join(' ');var hash=hashError(msg,source),time=new Date().toLocaleTimeString();logs.push({type:type,msg:msg,source:source,time:time,hash:hash});counts.all++;counts[type]=(counts[type]||0)+1;updateCounts();if(type==='error')showErrorToast(msg,hash);renderLogs();} + function renderLogs(){var body=document.getElementById('argon-debug-console-body');if(!body)return;var filtered=currentFilter==='all'?logs:logs.filter(function(l){return l.type===currentFilter;});body.innerHTML=filtered.map(function(log){var muted=isMuted(log.hash);var muteBtn=isAdmin&&log.type==='error'?'':'';return '
'+muteBtn+'['+log.time+'] '+log.msg.replace(//g,'>').replace(/\n/g,'
')+(log.source?''+log.source+'':'')+'
';}).join('');body.scrollTop=body.scrollHeight;} + var oc={log:console.log,warn:console.warn,error:console.error,info:console.info}; + console.log=function(){oc.log.apply(console,arguments);addLog('log',arguments);}; + console.info=function(){oc.info.apply(console,arguments);addLog('log',arguments);}; + console.warn=function(){oc.warn.apply(console,arguments);addLog('warn',arguments);}; + console.error=function(){oc.error.apply(console,arguments);addLog('error',arguments);}; + window.addEventListener('error',function(e){var src=e.filename?e.filename.split('/').pop()+':'+e.lineno:'';addLog('error',[e.message],src);}); + window.addEventListener('unhandledrejection',function(e){addLog('error',['Promise: '+(e.reason?(e.reason.message||e.reason):'Unknown')]);}); + var panel=document.getElementById('argon-debug-console'),header=document.getElementById('argon-debug-console-header'); + if(window.innerWidth>768&&header&&panel){var isDragging=false,startX,startY,startLeft,startTop;header.addEventListener('mousedown',function(e){if(e.target.tagName==='BUTTON')return;isDragging=true;startX=e.clientX;startY=e.clientY;var rect=panel.getBoundingClientRect();startLeft=rect.left;startTop=rect.top;panel.style.transition='none';});document.addEventListener('mousemove',function(e){if(!isDragging)return;var newLeft=Math.max(0,Math.min(startLeft+e.clientX-startX,window.innerWidth-panel.offsetWidth));var newTop=Math.max(0,Math.min(startTop+e.clientY-startY,window.innerHeight-panel.offsetHeight));panel.style.left=newLeft+'px';panel.style.top=newTop+'px';panel.style.bottom='auto';panel.style.right='auto';});document.addEventListener('mouseup',function(){isDragging=false;panel.style.transition='';});} + document.querySelectorAll('#argon-debug-console-tabs button').forEach(function(btn){btn.addEventListener('click',function(){document.querySelectorAll('#argon-debug-console-tabs button').forEach(function(b){b.classList.remove('active');});this.classList.add('active');currentFilter=this.dataset.filter;renderLogs();});}); + window.argonDebug={toggle:function(){panel.classList.toggle('show');},clear:function(){logs=[];counts={all:0,log:0,warn:0,error:0};updateCounts();renderLogs();},mute:function(hash,msg,source){if(!isAdmin)return;var xhr=new XMLHttpRequest();xhr.open('POST',ajaxUrl);xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xhr.onload=function(){if(xhr.status===200){mutedHashes.push(hash);renderLogs();}};xhr.send('action=argon_mute_error&nonce='+nonce+'&error_hash='+hash+'&error_message='+msg+'&error_source='+source);}}; + console.log(' - Theme v'); })();