This commit is contained in:
hrx 2025-11-20 10:27:14 +08:00
parent 10c58a72ce
commit bcf186539c

View File

@ -138,21 +138,53 @@ if (process.env.NODE_ENV === 'production') {
} }
}); });
// 检测开发者工具是否打开 // 更准确的开发者工具检测
let lastTime = Date.now(); let isDevToolsOpened = false;
function checkDebugger() {
const currentTime = Date.now(); // 方法1: 检查开发者工具宽度
if (currentTime - lastTime > 100) { function checkDevToolsByWidth() {
// 如果时间差大于100ms可能是在调试 const threshold = 160; // 开发者工具通常至少160px宽
handleDebuggerDetected(); const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
return widthThreshold || heightThreshold;
} }
lastTime = currentTime;
// 方法2: 检查debugger执行时间更宽松的阈值
function checkDevToolsByDebugger() {
return new Promise((resolve) => {
const start = performance.now();
debugger;
const end = performance.now();
// 使用更宽松的阈值,避免误报
resolve(end - start > 200);
});
}
// 方法3: 检查控制台是否打开
function checkDevToolsByConsole() {
const element = new Image();
Object.defineProperty(element, 'id', {
get: function() {
isDevToolsOpened = true;
}
});
console.log(element);
console.clear(); // 清除测试日志
}
// 方法4: 检查Eruda等移动端调试工具
function checkMobileDevTools() {
return !!(window.eruda || window.__eruda || window.vConsole);
} }
// 处理检测到调试器的情况 // 处理检测到调试器的情况
function handleDebuggerDetected() { function handleDebuggerDetected() {
// 可以采取以下措施之一: if (!isDevToolsOpened) {
isDevToolsOpened = true;
// 可以选择以下一种或多种处理方式
// 1. 显示警告信息(推荐) // 1. 显示警告信息(推荐)
alert('检测到开发者工具已打开,为了系统安全,请关闭开发者工具。'); alert('检测到开发者工具已打开,为了系统安全,请关闭开发者工具。');
@ -165,21 +197,55 @@ if (process.env.NODE_ENV === 'production') {
// 4. 关闭窗口(慎用) // 4. 关闭窗口(慎用)
// window.close(); // window.close();
// 5. 禁用页面交互
// document.body.innerHTML = '<h1>请关闭开发者工具后刷新页面</h1>';
}
} }
// 定期检查 // 定期检查(使用更宽松的间隔)
setInterval(function() { const checkInterval = setInterval(async function() {
checkDebugger(); // 如果已经检测到开发工具打开,停止检查
// 使用debugger检测 if (isDevToolsOpened) {
(function() { clearInterval(checkInterval);
const startTime = Date.now(); return;
debugger; }
const endTime = Date.now();
if (endTime - startTime > 100) { // 检查移动端调试工具
if (checkMobileDevTools()) {
handleDebuggerDetected();
return;
}
// 检查窗口大小
if (checkDevToolsByWidth()) {
handleDebuggerDetected();
return;
}
// 检查debugger异步
const isDebugging = await checkDevToolsByDebugger();
if (isDebugging) {
handleDebuggerDetected();
return;
}
// 偶尔检查控制台(不要太频繁)
if (Math.random() < 0.1) { // 10%的概率检查
checkDevToolsByConsole();
}
}, 2000); // 每2秒检查一次减少性能影响
// 监听窗口大小变化(添加去抖)
let resizeTimer;
window.addEventListener('resize', function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
if (checkDevToolsByWidth() && !isDevToolsOpened) {
handleDebuggerDetected(); handleDebuggerDetected();
} }
})(); }, 500);
}, 1000); });
// 禁用控制台输出(可选,根据需求开启) // 禁用控制台输出(可选,根据需求开启)
// 注意这会影响你自己的console.log调试建议只在生产环境使用 // 注意这会影响你自己的console.log调试建议只在生产环境使用
@ -202,22 +268,6 @@ if (process.env.NODE_ENV === 'production') {
}); });
} }
// 检测窗口大小变化(开发者工具打开时窗口大小会变化)
let lastWidth = window.innerWidth;
let lastHeight = window.innerHeight;
window.addEventListener('resize', function() {
const currentWidth = window.innerWidth;
const currentHeight = window.innerHeight;
if (Math.abs(currentWidth - lastWidth) > 100 || Math.abs(currentHeight - lastHeight) > 100) {
handleDebuggerDetected();
}
lastWidth = currentWidth;
lastHeight = currentHeight;
});
console.log('防调试保护已启用'); console.log('防调试保护已启用');
} }
// ==================== 防 F12 和右键检查功能结束 ==================== // ==================== 防 F12 和右键检查功能结束 ====================