Disable JavaScript console debugging methods

  • 2020-03-30 02:18:47
  • OfStack

Several major companies, Facebook and Netflix, have decided to bar users from executing JavaScript commands on the console.
This was originally started by Facebook to prevent malicious users from executing specific commands through the JavaScript console to spread the message (sending a lot of spam to all Facebook users).
Of course this gets a lot of flak, but before I got involved, the code they used was as follows:
//It seems that Netflix is only Facebook followed [https://news.ycombinator.com/item?id=7222129].

(function() { 
try { 
var $_console$$ = console; 
Object.defineProperty(window, "console", { 
get: function() { 
if ($_console$$._commandLineAPI) 
throw " I'm sorry ,  For user safety ,  This site is disabled console The script function "; 
return $_console$$ 
set: function($val$$) { 
$_console$$ = $val$$ 
} catch ($ignore$$) { 

I don't think so, but I actually think it's legal. From their perspective, if disabling the console temporarily helps prevent a problem, you have to do so.

But in the long run, it's really not a good idea; The goal may simply be to block the target users who depend on them.

In any case, this code looks good if you want to prevent the console from executing the script, and it does.

Related articles: