“Un-redefining” Google Chrome's console Object

后端 未结 7 735
失恋的感觉
失恋的感觉 2020-12-31 06:58

I\'m dealing with a system where the following Javascript code (which is out of my control) is being executed early in the page

if (!(\"console\" in window)          


        
7条回答
  •  攒了一身酷
    2020-12-31 07:38

    Here is a method I wrote based on the previous answers. It's got a bit of extra logic so that you can call it multiple times without recreating the iframe.

    function fixConsole() {
    
        var iframe;
        if(!(iframe = document.getElementById('fixConsole'))) {
            iframe = document.createElement('iframe');
            iframe.id = 'fixConsole';
            document.body.appendChild(iframe);
        }
        delete console;    
        console = iframe.contentWindow.console;
    
    };
    

    I anticipate the need to call fixConsole multiple times in the case that the code which originally redefined console is self repairing.

提交回复
热议问题