簡體   English   中英

我可以在IE8-9中檢測用戶打開開發人員工具嗎?

[英]Can I detect the user opening developer tools in IE8-9?

是否有某種事件我可以用來檢測用戶打開開發人員工具的時間? 目前我在setInterval周圍的問題

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條消息的隊列。

這可能在IE8中不起作用( defineProperty 有些錯誤 ),但我沒有那個用來驗證情況。 但是,它在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上看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM