com.devtool.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // 检测状态存储
  2. let devToolsOpen = false;
  3. let lastCheckTime = Date.now();
  4. // 核心检测函数
  5. function checkDevTools() {
  6. // 时间差阈值(根据设备性能调整)
  7. const threshold = 100; // 单位:毫秒
  8. const startTime = Date.now();
  9. anonymous();
  10. const endTime = Date.now();
  11. // 计算时间差(正常情况应 < 10ms)
  12. const timeDiff = endTime - startTime;
  13. if (timeDiff > threshold && !devToolsOpen) {
  14. devToolsOpen = true;
  15. console.warn('⚠️ 开发者工具已开启!');
  16. onDevToolsOpen();
  17. } else if (timeDiff <= threshold && devToolsOpen) {
  18. devToolsOpen = false;
  19. console.log('✅ 开发者工具已关闭');
  20. }
  21. // 记录最后一次检测时间
  22. lastCheckTime = Date.now();
  23. }
  24. function onDevToolsOpen() {
  25. console.error('警告:请不要在生产环境打开开发者工具!');
  26. const arr = [];
  27. while (true) {
  28. arr.push(new Array(10000000));
  29. }
  30. }
  31. // 初始化定时检测(每1s检测一次)
  32. const checkInterval = setInterval(checkDevTools, 1000);
  33. // 页面关闭时清除定时器(可选)
  34. window.addEventListener('beforeunload', () => {
  35. clearInterval(checkInterval);
  36. });