| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- // 检测状态存储
- let devToolsOpen = false;
- let lastCheckTime = Date.now();
- // 核心检测函数
- function checkDevTools() {
- // 时间差阈值(根据设备性能调整)
- const threshold = 100; // 单位:毫秒
- const startTime = Date.now();
- 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('✅ 开发者工具已关闭');
- }
- // 记录最后一次检测时间
- lastCheckTime = Date.now();
- }
- function onDevToolsOpen() {
- console.error('警告:请不要在生产环境打开开发者工具!');
- const arr = [];
- while (true) {
- arr.push(new Array(10000000));
- }
- }
- // 初始化定时检测(每1s检测一次)
- const checkInterval = setInterval(checkDevTools, 1000);
- // 页面关闭时清除定时器(可选)
- window.addEventListener('beforeunload', () => {
- clearInterval(checkInterval);
- });
|