重构开发者工具检测机制 - 使用多重检测方法

修复了控制台在其他页面打开后仍能在本站显示的问题。

核心改进:
1. 新增Console对象toString检测(最有效)
2. 优化debugger暂停时间检测
3. 保留窗口尺寸检测作为补充
4. 综合多种方法,提高检测准确率
5. 页面加载时立即执行检测
6. 持续监控确保实时防护

检测原理:
- console.log会触发对象的toString方法
- 当控制台打开时,这个方法会被调用
- 通过标志位检测到调用,即可判断控制台已打开

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-17 20:33:01 +08:00
parent 69e864bcfb
commit 4324d886d8

View File

@@ -2251,104 +2251,119 @@
// 检测开发者工具是否打开(调试模式下不检测) // 检测开发者工具是否打开(调试模式下不检测)
if (!isDebugMode) { if (!isDebugMode) {
(function() { let devtoolsDetected = false;
const threshold = 160;
let isDevToolsOpen = false;
let warningShown = false;
// 增强型检测:结合窗口尺寸和debugger暂停 // 方法1: 使用console对象检测
function checkDevTools() { const devtools = /./;
const widthThreshold = window.outerWidth - window.innerWidth > threshold; devtools.toString = function() {
const heightThreshold = window.outerHeight - window.innerHeight > threshold; devtoolsDetected = true;
};
// 检测是否有开发者工具打开 // 方法2: debugger暂停时间检测
const isOpen = widthThreshold || heightThreshold || function detectByDebugger() {
(window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized); const start = performance.now();
debugger;
if (isOpen && !isDevToolsOpen) { const end = performance.now();
isDevToolsOpen = true; return (end - start) > 100; // 如果暂停超过100ms说明开发者工具打开
if (!warningShown) {
warningShown = true;
// 清空控制台
console.clear();
// 显示警告页面
const warningHTML = `
<div style="
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
z-index: 999999;
font-family: Arial, sans-serif;
">
<div style="
background: white;
padding: 60px;
border-radius: 20px;
text-align: center;
max-width: 500px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
">
<i class="fas fa-shield-alt" style="font-size: 80px; color: #667eea; margin-bottom: 30px;"></i>
<h1 style="color: #333; margin-bottom: 20px; font-size: 32px;">检测到开发者工具</h1>
<p style="color: #666; font-size: 18px; margin-bottom: 30px; line-height: 1.6;">
为保护系统安全,请关闭浏览器开发者工具后刷新页面
</p>
<button onclick="location.reload()" style="
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 15px 40px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
transition: transform 0.2s;
" onmouseover="this.style.transform='translateY(-2px)'" onmouseout="this.style.transform='translateY(0)'">
<i class="fas fa-sync-alt"></i> 刷新页面
</button>
<p style="color: #999; font-size: 14px; margin-top: 20px;">
如需使用开发者工具,请联系管理员开启调试模式
</p>
</div>
</div>
`;
// 替换页面内容
document.body.innerHTML = warningHTML;
}
} else if (!isOpen) {
isDevToolsOpen = false;
}
} }
// 立即检测一次(页面加载时就检查) // 方法3: 窗口尺寸检测
checkDevTools(); function detectByWindowSize() {
const threshold = 160;
const widthDiff = window.outerWidth - window.innerWidth;
const heightDiff = window.outerHeight - window.innerHeight;
return widthDiff > threshold || heightDiff > threshold;
}
// 定期检测 // 方法4: 检测console.log是否被执行
setInterval(checkDevTools, 500); function detectByConsole() {
devtoolsDetected = false;
console.log(devtools);
console.clear();
return devtoolsDetected;
}
// 使用debugger语句检测更灵敏 // 显示警告页面
function showWarning() {
const warningHTML = `
<div style="
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
z-index: 999999;
font-family: Arial, sans-serif;
">
<div style="
background: white;
padding: 60px;
border-radius: 20px;
text-align: center;
max-width: 500px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
">
<i class="fas fa-shield-alt" style="font-size: 80px; color: #667eea; margin-bottom: 30px;"></i>
<h1 style="color: #333; margin-bottom: 20px; font-size: 32px;">检测到开发者工具</h1>
<p style="color: #666; font-size: 18px; margin-bottom: 30px; line-height: 1.6;">
为保护系统安全,请关闭浏览器开发者工具后刷新页面
</p>
<button onclick="location.reload()" style="
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 15px 40px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
transition: transform 0.2s;
" onmouseover="this.style.transform='translateY(-2px)'" onmouseout="this.style.transform='translateY(0)'">
<i class="fas fa-sync-alt"></i> 刷新页面
</button>
<p style="color: #999; font-size: 14px; margin-top: 20px;">
如需使用开发者工具,请联系管理员开启调试模式
</p>
</div>
</div>
`;
document.body.innerHTML = warningHTML;
}
// 综合检测函数
function checkDevTools() {
// 使用多种方法检测,任意一种检测到就触发
const method1 = detectByConsole();
const method2 = detectByDebugger();
const method3 = detectByWindowSize();
if (method1 || method2 || method3) {
showWarning();
return true;
}
return false;
}
// 页面加载完成后立即检测
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(checkDevTools, 100);
});
} else {
setTimeout(checkDevTools, 100);
}
// 持续监控
setInterval(function() { setInterval(function() {
const before = new Date().getTime(); if (!document.body.innerHTML.includes('检测到开发者工具')) {
debugger; // 如果开发者工具打开,这里会暂停
const after = new Date().getTime();
// 如果暂停时间超过100ms说明开发者工具打开了
if (after - before > 100) {
checkDevTools(); checkDevTools();
} }
}, 1000); }, 1000);
})();
} }
// 禁用console输出调试模式下不禁用 // 禁用console输出调试模式下不禁用