2013-04-25 43 views
3

是否有某种事件可以用来检测用户何时打开开发人员工具?目前我的setInterval解决该问题与我可以在IE8-9中检测到用户开放的开发者工具吗?

var interval, consoleOpen = false; 
interval = setInterval(function() { 
    if(typeof console !== 'undefined' && typeof console.log !== 'undefined') { 
     clearInterval(interval); 
     consoleOpen = true; 
     console.log("Console is open!"); 
     // dump debug message queue... 
    } 
}, 100); 

,但我想避免这样的,如果我能解决,所以是有,我可以用任何方法更好?原因是只要控制台存在,就要保留它们的调试消息和console.log()。我已经将消息存储在一个数组中,该数组的运行方式与限制为100条消息的队列类似

回答

2

这可能不适用于IE8(definePropertysomewhat buggy),但我没有那个手来验证情况。但是,它在IE9中工作正常[1]。

(欣赏这使得在一个不完全完整的解决方案,但它可能是一个很好的起点。)

(function() { 
    if ('console' in window) return; 
    if (!Object.defineProperty) return; 

    Object.defineProperty(window, 'console', { 
     configurable: true, 
     enumerable: true, 
     set: function (val) { 
      delete this.console; // 'Unwatch' console changes 
      this.console = val; 

      // Notify your logging service that it can start 
      // outputting to `console.log` here 
      // Logger.start() or whatever's appropriate 
     } 
    }); 
})(); 

[1]警告:我没有实际测试过它超出了在IE浏览器把它扔看看会发生什么。

相关问题