com.devtool.js 1001 B

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