反调试
js
/**
* 安全版调试检测
* @param {object} options - 可选配置
* @param {number} options.threshold - 时间差阈值(毫秒),超过则认为可能被调试
* @param {number} options.interval - 检测间隔(毫秒)
* @param {Function} options.onDetect - 当检测到调试时执行的回调
*/
function setupSafeDebuggerDetector(options = {}) {
const {
threshold = 200, // 时间差阈值
interval = 2000, // 检测间隔
onDetect = () => console.warn("[检测到可能的调试行为]"), // 默认行为
} = options;
function detect() {
const start = Date.now();
debugger; // 👈 检测核心
const end = Date.now();
// 如果检测到明显的时间延迟
if (end - start > threshold) {
try {
onDetect(end - start);
} catch (err) {
console.error("onDetect 回调执行出错:", err);
}
}
setTimeout(detect, interval);
}
detect();
}