Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 7 min read

Three Layers to Protect Browser-Based JavaScript Tools From Copy-Paste Thieves

TL;DR: Browser JavaScript can't be hidden, but it can be protected. Domain locks redirect copied pages back to your site. Minification makes code hard to read. Edge checks block scrapers and hotlinkers. Together, these l

Three Layers to Protect Browser-Based JavaScript Tools From Copy-Paste Thieves

TL;DR: Browser JavaScript can't be hidden, but it can be protected. Domain locks redirect copied pages back to your site. Minification makes code hard to read. Edge checks block scrapers and hotlinkers. Together, these layers make copying slow enough that most people give up.

If your tool runs in the browser, your code ships to every visitor. You cannot hide it. The browser downloads the JavaScript, reads it, and runs it. That is how the web works.

What you can do is make copying it pointless.

On visionvix.world, I protect 3D simulators with a simple build step: a domain lock, minifying, and edge checks. This article explains how I set it up, what each part stops, and what it doesn't stop. I built this, so I know exactly where the limits are.

A laptop showing two code panes: readable JavaScript with comments on the left, and the same file minified into dense single lines on the right

The three layers that slow down copiers

The setup has three parts. First, minify everything so the code is hard to read. Second, add a domain lock that sends visitors back to your site if someone copies the page. Third, run checks at the edge to block scraping tools and bandwidth thieves.

None of these is unbreakable. A patient developer can reverse minified code. Someone can find and delete the domain lock. A real browser driven by a person can get past edge checks.

But most people who copy a tool want a quick win. They want to save your page, upload it to their site, and claim it. This setup takes that quick win away.

What a domain lock does

A domain lock is a few lines of JavaScript at the very top of every page. It checks where the page is running. On your real site, it does nothing. If someone saves the page and puts it on their own domain, it redirects the visitor straight back to your site.

The copier gets nothing. The visitor lands on your site. The copied page becomes a free link.

A diagram of four steps: readable source, the build step, edge checks and the domain lock in the browser

Why minifying matters more than obfuscation

Minifying removes comments, whitespace, and renames variables to short letters. The code still works the same, but it looks like this:

javascript
function a(b,c){var d=b*c;return d>100?d:0}
`

Instead of this:

javascript
// Calculate product and return only if above threshold
function calculateProduct(width, height) {
var product = width * height;
return product > 100 ? product : 0;
}
`

A person can still read minified code, but it takes time. They have to guess what each variable does. They have to trace the flow. They cannot quickly change a feature or rebrand it.

Heavy obfuscation tools exist. They scramble the code even more. But obfuscation makes files bigger and slower, and a determined person can still reverse it. Minifying is cheaper, faster, and good enough for most cases.

How I set it up for a static site

I run this setup on a static Next.js site hosted on Vercel. The same approach works on any static host. The build step runs locally before deployment.

Step 1: Keep readable source private

All simulators live in a private folder called sim-src. That folder has comments, clear variable names, and my notes. I edit only in that folder.

A build script copies everything into the public folder that gets deployed. The readable source never leaves my project. The version on the internet is always the processed one.

Step 2: Minify scripts, skip libraries

The build runs every inline script and every local JavaScript file through terser, a standard minifier. Two lessons from doing this on real pages:

  • Do not rename top-level names. Old style pages call functions from onclick attributes in the HTML. If the minifier renames those functions, the buttons stop working. I keep top-level renaming off and let it rename everything inside functions.
  • Skip libraries. Files like three.js are already minified and public anyway. Running them again only slows the build and risks breaking them. The script skips vendor and lib folders.
  • Never fail the build. If a script won't minify, the build keeps the original and logs it. Shipping a broken page is worse than shipping one unminified file.

Side by side code: the readable quality script with comments and long names, and the minified version with short names on dense lines

The script also strips HTML and CSS comments. Comments often explain how a tool works better than the code does.

Step 3: Add the domain lock to every page

The build adds a tiny script as the very first thing inside the <head> of every page. It runs before anything else loads. Here is the core idea:

javascript
(function () {
var host = location.hostname;
var allowed = /^(www\.)?visionvix\.world$/;
if (allowed.test(host) || host === 'localhost') return;
location.replace('https://visionvix.world' + location.pathname);
})();

Three details matter:

  • Allow localhost so you can test the build on your own machine.
  • Allow your preview domains like Vercel preview links, or every preview will bounce.
  • Use location.replace so the copied page does not stay in the visitor's back history.

If the hostname matches your site or localhost, the script does nothing. If it doesn't match, the script redirects the visitor to your real site at the same path.

Step 4: Check the result every build

The script prints what it did. Here is the real output from the current build:

  • Minified 18 inline scripts
  • Minified 12 local JavaScript files
  • Skipped 7 vendor libraries
  • Added domain lock to 24 pages
  • Total size: 4.28 MB to 4.19 MB

The Solar System page dropped from approximately 142 KB to 116 KB after minification.

A terminal running the protect build: 22 pages, 108 inline scripts, 12 js files, 4.28 MB to 4.19 MB, and the Solar System page going from 142136 to 116105 bytes

Minifying is mostly about protection here, not size. Most of the weight is libraries, textures, and 3D models that are already compressed. But every kilobyte saved is also one less kilobyte a copier gets.

Step 5: Add checks at the edge

On Vercel, a middleware file runs before every request. Mine does four things, in order:

  1. Let good bots through. Search engines and AI answer engines are allowed straight in. SEO is not hurt.
  2. Refuse copying tools. Empty user agents, scraper libraries, and known site copiers get a 403.
  3. Block hotlinking. Heavy files like textures, fonts, and 3D models are only served when the request comes from the site itself or is typed in directly. Another site cannot borrow my bandwidth.
  4. Rate limit per visitor. A simple per-IP limit slows down anyone asking for too much too fast.

Each check is cheap. Each one runs in a few milliseconds. Together, they stop the bulk of automated copying before a single file is served.

What this setup stops and what it doesn't

Here is the honest breakdown:

Layer Stops Does not stop
Private source Anyone reading your comments and clear names Nothing on its own
Minifying Easy reading, quick edits, copy and rebrand A patient developer with a code formatter
Domain lock A saved copy running on another domain Someone who finds and deletes the lock line
Edge checks Bulk copying tools, hotlinking, simple floods A real browser driven by a determined person

None of these is unbreakable, and nothing that runs in a browser can be.

The point is that each layer is cheap for me and costs the copier time. Most people who copy a tool want a quick win. This takes the quick win away.

Why this approach beats heavy obfuscation

Some developers use heavy obfuscation tools that scramble code into unreadable nonsense. The output looks like this:

javascript
var _0x4d2f=['test','apply','return'];(function(_0x2a3b,_0x4d2f){...})();

This has three problems:

  • It makes pages bigger. Obfuscated code is often 2x to 3x larger than minified code.
  • It makes pages slower. The browser has to parse more characters and handle more indirection.
  • It is still reversible. Deobfuscation tools exist. A determined person can reverse it. You paid the size and speed cost for nothing.

Minifying plus domain locks is cheaper, faster, and achieves the same result for most threats.

What you should never put in front-end code

No amount of protection makes front-end code truly secret. Never put these in browser JavaScript:

  • API keys that grant access to paid services
  • Secret logic that determines pricing or access control
  • Authentication tokens with write permissions
  • Proprietary algorithms you rely on for competitive advantage

If it matters, move it to the server. If you cannot move it to the server, accept that someone can reverse it and design your business model accordingly. For more on setting up secure development workflows, I covered the full process in my earlier guide on moving dev setups to a new PC.

This is a speed bump, not a vault

The setup I use on visionvix.world is not perfect. A skilled developer who really wants your code can get it. But most copiers are not skilled. Most are looking for the fastest path to a working copy.

This setup makes that path slow and annoying. It makes the copied page useless on their domain. It blocks their scraping tools. It wastes their time.

For most browser tools, that is enough.

If you want to see this protection in action, check out the simulators on visionvix.world. Every page uses this exact build step. The code is minified. The domain lock is live. The edge checks run on every request.

I also walk through the build process and other technical projects on my YouTube channel, where I show the behind-the-scenes setup for tools like these.

The whole setup takes one build script and one middleware file. It runs in seconds. It costs nothing extra to host. And it stops the quick wins.

That is all most tools need.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.