How to Obfuscate HTML Classes in HTML, CSS, JavaScript Files via Python

Nowadays it’s hard to keep your website design code safe. When you launch a website, everyone can able to see its browser-rendered version of HTML, CSS, and Javascript files. So they can able to copy your website design quickly. This issue even gets worst when you are using CSS frameworks like Bootstrap or Tailwind CSS.

Companies like Google and Facebook always obfuscate the website Html classes. You can easily use obfuscated codes in anywhere like Static sites, CMS like WordPress, or sites created with Flask.

If you know Python basics and you want to obfuscate your website code. This tutorial is definitely going o help you a lot.

SEE ALSO: .htaccess Security Tips: Prevent Website from Hacking

So how to obfuscate Html classes using Python?

obfuscate html classes using python

First, create a new folder and put your Html files, CSS file, and Javascript files inside that folder.

Then create a Python file named

Install the html-classes-obfuscater package via the following pip command.

pip install html-classes-obfuscator

Then put the following code inside file.

import glob
import random
import string
from html_classes_obfuscator import html_classes_obfuscator

htmlfiles = glob.glob("./**/*.html", recursive=True)
cssfiles = glob.glob("./**/*.css", recursive=True)
jsfiles = glob.glob("./**/*.js", recursive=True)


# Generate random string
def generate_class(current_classes_list):

    def random_class():
        # Offers (26*2)^6 random class name possibilities
        return ''.join(random.choice(string.ascii_letters) for i in range(6))

    res = random_class()

    while res in current_classes_list.values():
        res = random_class()

    return res

html_classes_obfuscator.html_classes_obfuscator(htmlfiles, cssfiles, jsfiles, generate_class)

Now run that file. It will automatically replace the origins Html, CSS, and js files with the obfuscated version of the file.
That’s it.