“Un-redefining” Google Chrome's console Object

后端 未结 7 689
失恋的感觉
失恋的感觉 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:44

    I believe you could possibly do this with an iframe inject and then copy the iframe's console object:

    <script type="text/javascript">
    console = {};
    try {
        console.log('1');
    } catch(e){
        alert('No console');
    }
    </script>
    <iframe id="text"></iframe>
    <script type="text/javascript">
    console = window.frames[0].console;
    try {
        console.log('test');
    } catch(e){
        alert('No console');
    }
    </script>
    

    http://jsfiddle.net/nmY6k/

    Note, this is just a demonstration that the concept should work.

    EDIT

    With a pure JS iframe:

    <script type="text/javascript">
    console = {};
    try {
        console.log('1');
    } catch(e){
        alert('No console');
    }
    var iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    console = window.frames[0].console;
    try {
        console.log('test');
    } catch(e){
        alert('No console');
    }
    </script>
    

    http://jsfiddle.net/nmY6k/1/

    EDIT

    And of course, if you need to remove the iframe element afterwards:

    <script type="text/javascript">
    console = {};
    try {
        console.log('1');
    } catch(e){
        alert('No console');
    }
    var iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    console = window.frames[0].console;
    try {
        console.log('test');
    } catch(e){
        alert('No console');
    }
    console.log(typeof window.frames);
    document.body.removeChild(iframe);
    console.log(typeof window.frames);
    </script>
    
    0 讨论(0)
提交回复
热议问题