| 123456789101112131415161718192021222324252627282930313233343536 |
- // 检测状态存储
- let devToolsOpen = false;
- // 核心检测函数
- function checkDevTools() {
- // 时间差阈值(根据设备性能调整)
- const threshold = 100; // 单位:毫秒
- const startTime = Date.now();
- window.anonymous?.();
- const endTime = Date.now();
- // 计算时间差(正常情况应 < 10ms)
- const timeDiff = endTime - startTime;
- if (timeDiff > threshold && !devToolsOpen) {
- devToolsOpen = true;
- console.warn('⚠️ 开发者工具已开启!');
- onDevToolsOpen();
- } else if (timeDiff <= threshold && devToolsOpen) {
- devToolsOpen = false;
- console.log('✅ 开发者工具已关闭');
- }
- }
- function onDevToolsOpen() {
- console.error('警告:请不要在生产环境打开开发者工具!');
- }
- // 初始化定时检测(每1s检测一次)
- const checkInterval = setInterval(checkDevTools, 1000);
- // 页面关闭时清除定时器(可选)
- window.addEventListener('beforeunload', () => {
- clearInterval(checkInterval);
- });
|