“Un-redefining” Google Chrome's console Object

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

    You could write a pretty basic userscript to assign console as suggested above. Then go into the manifest.json for that userscript, and change the run_at setting (see http://code.google.com/chrome/extensions/content_scripts.html ) to document_start. That will make the userscript now run before any of the page scripts run, so before console ever gets messed with.

    ...edit...

    Actually, I note from http://blog.chromium.org/2011/07/chrome-extensions-now-with-more.html that chrome also now supports @run-at, so you can set that, and don't even have to mess with manifest.json directly. They have an example script using @run-at at document_start at http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/howto/userscript-runat/runat.user.js?view=markup

    0 讨论(0)
  • 2020-12-31 07:19

    Assuming the reason you're concerned is that you want to use the console normally as you develop, you could whip up a simple chrome extension that defines console.firebug so that the condition will evaluate to false. That way you don't need to worry about any potential quirky behavior that might arise from using some other frame's console object, nor do you have to worry about repeatedly inserting and deleting a hack as you develop/deploy.

    0 讨论(0)
  • 2020-12-31 07:23

    Can't you put your own script directly in the head of the html right at the top?

    <script ...>
        if (console) { window.cache_console = console; }
    </script>
    </head>
    <body>
    ... html ...
    
    <script>
        window.console = window.cache_console;
    </script>
    </body>
    </html>
    

    You can't get much earlier than right up in the HEAD.

    0 讨论(0)
  • 2020-12-31 07:33

    This seems to work:

    iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    console = iframe.contentWindow.console;
    

    However it looks like you cannot remove the iframe

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 07:42

    In Google Chrome, deleting the console object works:

    <script>
    window.console = {};
    delete console;
    console.log('still works');
    </script>
    

    However, this doesn't seem to work in Firefox 4. It's a start, though.

    0 讨论(0)
提交回复
热议问题