JavaScript Obfuscator · Tutorial

How to obfuscate a JavaScript widget before sharing

6 min readintermediateUpdated

When you share a widget for others to embed, its source is visible to everyone. You will obfuscate a price calculator, keep its public function name and check that the output still gives the same result.

What you need

  • The JavaScript Obfuscator
  • A script to protect, or the example price calculator
  • Your browser's developer console to test the result

Steps

  1. Load the widget

    Press Try it to load the calculator. It works out a discount from the quantity and logs the total.

  2. Choose a strength

    Leave Strength on Medium. The output has hexadecimal names, an encoded string table and scrambled control flow, and the message shows how much bigger it is.

  3. Keep the public name

    If pages call your function by name, turn on Rename global names and type the function name under Names to keep. Everything else is renamed, but that name stays.

    calculatePrice
  4. Test the result

    Copy the output, paste it into the browser console on any page and press Enter. It should log the same total as the original: 45.9 with a 15% discount.

Try it yourself

Opens the JavaScript Obfuscator with this tutorial's sample loaded.

Try it now

Common mistakes

Hiding secrets in the code
Obfuscated code can still be analysed. Keep API keys and anything that must stay private on a server.
Throwing away the original
The output can't be turned back into readable source. Keep the original for future changes.
Using High for a whole app
High strength makes code much larger and slower. Use it only for small, sensitive parts.

Next steps

Go back to the JavaScript Obfuscator, or keep going with one of these:

FAQ

Should I minify before or after obfuscating?

Obfuscate last. The output is already compact, and running other tools on it afterwards can break self-defending code.