YogeshChauhan . com

How to obfuscate JavaScript code to hide it from View Source?

in JavaScript on 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

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

3 Common Usability Mistakes In Web DesignUI/UXWhat is Hoisting in JavaScript?JavaScriptHow to add today’s date in HTML input date value using JavaScript?JavaScriptHow to vertically and horizontally align text and image block (without flex or grid) in CSS?CSSHTML Copyright SymbolHTML4 different ways to create JavaScript ObjectsJavaScript