重构开发者工具检测机制 - 使用多重检测方法
修复了控制台在其他页面打开后仍能在本站显示的问题。 核心改进: 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:
@@ -2251,30 +2251,40 @@
|
|||||||
|
|
||||||
// 检测开发者工具是否打开(调试模式下不检测)
|
// 检测开发者工具是否打开(调试模式下不检测)
|
||||||
if (!isDebugMode) {
|
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;
|
const threshold = 160;
|
||||||
let isDevToolsOpen = false;
|
const widthDiff = window.outerWidth - window.innerWidth;
|
||||||
let warningShown = false;
|
const heightDiff = window.outerHeight - window.innerHeight;
|
||||||
|
return widthDiff > threshold || heightDiff > threshold;
|
||||||
|
}
|
||||||
|
|
||||||
// 增强型检测:结合窗口尺寸和debugger暂停
|
// 方法4: 检测console.log是否被执行
|
||||||
function checkDevTools() {
|
function detectByConsole() {
|
||||||
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
|
devtoolsDetected = false;
|
||||||
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
|
console.log(devtools);
|
||||||
|
|
||||||
// 检测是否有开发者工具打开
|
|
||||||
const isOpen = widthThreshold || heightThreshold ||
|
|
||||||
(window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized);
|
|
||||||
|
|
||||||
if (isOpen && !isDevToolsOpen) {
|
|
||||||
isDevToolsOpen = true;
|
|
||||||
|
|
||||||
if (!warningShown) {
|
|
||||||
warningShown = true;
|
|
||||||
|
|
||||||
// 清空控制台
|
|
||||||
console.clear();
|
console.clear();
|
||||||
|
return devtoolsDetected;
|
||||||
|
}
|
||||||
|
|
||||||
// 显示警告页面
|
// 显示警告页面
|
||||||
|
function showWarning() {
|
||||||
const warningHTML = `
|
const warningHTML = `
|
||||||
<div style="
|
<div style="
|
||||||
position: fixed;
|
position: fixed;
|
||||||
@@ -2322,33 +2332,38 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// 替换页面内容
|
|
||||||
document.body.innerHTML = warningHTML;
|
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() {
|
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输出(调试模式下不禁用)
|
||||||
|
|||||||
Reference in New Issue
Block a user