YogeshChauhan.com
How to obfuscate JavaScript code to hide it from View Source?
November 7, 2020

What is Obfuscation?

Obfuscation is the technique to hide the intended meaning of message/code/text by making the message difficult to understand, usually with confusing and ambiguous language.

For e.g. let’s say this is our function in JavaScript.


function test(name) {
 console.log('Hello, ' + name); 
} 
hello('Yogesh');

For that, this is obfuscated code:


eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--){d[c]=k[c]||c}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('2 1(0){3.7('4, '+0)}5('6');',8,8,'name|test|function|console|Hello|hello|Yogesh|log'.split('|'),0,{}))

Remember that the obfuscated code depends on which tool you use to obfuscate your code.

There are many tools available to obfuscate the JS code for e.g. Google Closure Compiler

You can also install tool on your local machine. In this article, I will go through the step by step guide on how to install obfuscate tool on your local machine.

Step 1

Make sure you have node and npm are installed on your machine. Check the version using this command:


node --version

npm --version

Step 2

The obfuscator tool should be installed globally. So, we can use -g for that.

Use npm with the -g parameter and sudo (admin user) to install it.


sudo npm install -g  javascript-obfuscator

// it will ask for admin password

It will get installed in few seconds and you’ll get a similar message like this one:


+ [email protected]
added 110 packages from 160 contributors 
in 5.6s

Step 3

Use obfuscator tool to obfuscate your JS code.

Go the folder where your JS code file is and type this command in CLI.


javascript-obfuscator .

You’ll see the new obfuscated file in the same folder within seconds.

That’s it.

There are customization options available to use.

Find out more on: npm

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

WordPress: How to get field values in Advanced Custom Fields?WordPressLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPSome SQL LIKE Operators We Need to Keep in MindSQL/MySQLSQL ALL OperatorSQL/MySQLFull and Partial CUBE in Postgres with ExamplesPostgresWhat is Object Mutability in JavaScript?JavaScript