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

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

核心改进:
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,30 +2251,40 @@
// 检测开发者工具是否打开(调试模式下不检测)
if (!isDebugMode) {
(function() {
let devtoolsDetected = false;
// 方法1: 使用console对象检测
const devtools = /./;
devtools.toString = function() {
devtoolsDetected = true;
};
// 方法2: debugger暂停时间检测
function detectByDebugger() {
const start = performance.now();
debugger;
const end = performance.now();
return (end - start) > 100; // 如果暂停超过100ms说明开发者工具打开
}
// 方法3: 窗口尺寸检测
function detectByWindowSize() {
const threshold = 160;
let isDevToolsOpen = false;
let warningShown = false;
const widthDiff = window.outerWidth - window.innerWidth;
const heightDiff = window.outerHeight - window.innerHeight;
return widthDiff > threshold || heightDiff > threshold;
}
// 增强型检测:结合窗口尺寸和debugger暂停
function checkDevTools() {
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
// 检测是否有开发者工具打开
const isOpen = widthThreshold || heightThreshold ||
(window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized);
if (isOpen && !isDevToolsOpen) {
isDevToolsOpen = true;
if (!warningShown) {
warningShown = true;
// 清空控制台
// 方法4: 检测console.log是否被执行
function detectByConsole() {
devtoolsDetected = false;
console.log(devtools);
console.clear();
return devtoolsDetected;
}
// 显示警告页面
function showWarning() {
const warningHTML = `
<div style="
position: fixed;
@@ -2322,33 +2332,38 @@
</div>
</div>
`;
// 替换页面内容
document.body.innerHTML = warningHTML;
}
} else if (!isOpen) {
isDevToolsOpen = false;
// 综合检测函数
function checkDevTools() {
// 使用多种方法检测,任意一种检测到就触发
const method1 = detectByConsole();
const method2 = detectByDebugger();
const method3 = detectByWindowSize();
if (method1 || method2 || method3) {
showWarning();
return true;
}
return false;
}
// 立即检测一次(页面加载时就检查)
checkDevTools();
// 页面加载完成后立即检测
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(checkDevTools, 100);
});
} else {
setTimeout(checkDevTools, 100);
}
// 定期检测
setInterval(checkDevTools, 500);
// 使用debugger语句检测更灵敏
// 持续监控
setInterval(function() {
const before = new Date().getTime();
debugger; // 如果开发者工具打开,这里会暂停
const after = new Date().getTime();
// 如果暂停时间超过100ms说明开发者工具打开了
if (after - before > 100) {
if (!document.body.innerHTML.includes('检测到开发者工具')) {
checkDevTools();
}
}, 1000);
})();
}
// 禁用console输出调试模式下不禁用