How to block the browser console using javascript?

12

I would like to know how to block the user from running scripts through the browser console.

    
asked by anonymous 16.02.2014 / 23:59

3 answers

8
  

Edit: This code no longer works in Google Chrome.

In fact, it is possible to block the Chrome console just as Facebook is doing, with the following code:

var _z = console;
Object.defineProperty(window, 'console', {
    get: function(){
        if (_z._commandLineAPI) {
            throw new Error('console bloqueado');
        }
        return _z;
    },
    set: function(val) {
        _z = val;
    }
});

Open the Chrome console on your Facebook page and see for yourself (does not appear for all users):

References:

17.02.2014 / 00:13
12

Not possible. The page is submissive to the browser and the javascript console is not something you can really control, it's an extra. It would be like preventing them from seeing your html or creating a .txt file that could not be edited, does not make sense.

The user is completely free to edit anything from your page. Any code can be changed and any validation done purely in JS can be fooled. This is why the server should never trust something from the client.

    
17.02.2014 / 00:03
2

Sorry folks, but as for the answer on Facebook, I'm disabling the console just by displaying that message. Do not check.

What Facebook does is simply displaying a warning for the user not to use the console and for this displays the mansage using the console object in Javascripr. I do this in my projects to create a signature that is displayed on the console. Here's what I do in the main Javasript site:

style = "color:blue;font-size:1.1em;";
style2 = "color:green; font-weight:bold;font-size:1.1em;";
console.groupCollapsed("Creditos do desenvolvedor:");
   console.info("%c-------------------------------------------------------------",style);
   console.info("%cEste é mais um site desenvolvido pela...", style2);
   console.info("%chttp://www.site.com.br",style);
   console.info("%cTodos os direitos reservados © 2017",style);
   console.info("%c-------------------------------------------------------------",style);
   console.info("");
console.groupEnd();
    
04.09.2018 / 22:55