How to control the commands executed by the browser console with JavaScript?

30 views Asked by At

I'm trying to control the commands that are executed in console so you can't acces some basic commands if you're the client.

I've tried to delete all the console using console = {}, but it only deletes the console commands.

1

There are 1 answers

0
Dimava On
  1. Generally, that's impossible. A programmer can easily pause your JS code in any point and take out all the values and functions they could want.

  2. If you want script-kiddies and browser extensions to not have acess to your classes and variables, don't place them in global (window) scope.
    If you write

var a = 123;
let b = 'asd'
class C {}

then they exist in global scope and can be acessed as

window.a
window.b
window.C

To prevent that you should either scope them

function f(){
  var a = 123;
  let b = 'asd'
  class C {}
}
f();
// no references here

or use <script type="module"> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

  1. If you MUST disallow acess to your things in every way possible, you can compile your code in C/C++/Rust/whatever into WebAssembly. That's not an easy task. Anyways, user can do anything with JS, web connection, etc.