Skip to content

Instantly share code, notes, and snippets.

@paulshestakov
Created February 10, 2024 15:18
Show Gist options
  • Save paulshestakov/42fec74c0bd6ba8b85fbf1fab53982ac to your computer and use it in GitHub Desktop.
Save paulshestakov/42fec74c0bd6ba8b85fbf1fab53982ac to your computer and use it in GitHub Desktop.
obfuscate html string
import {JSDOM} from "jsdom"
const obfuscateString = (str) => {
return new Array(str.length).fill("*").join("");
};
const obfuscateElement = (element) => {
const attrs = element.getAttributeNames();
for (const attr of attrs) {
const value = element.getAttribute(attr);
const valueObfuscated = obfuscateString(value);
element.setAttribute(attr, valueObfuscated);
}
};
const obfuscateNode = (node) => {
switch (node.nodeType) {
case 1: {
obfuscateElement(node);
break;
}
case 3: {
node.nodeValue = obfuscateString(node.wholeText);
break;
}
default:
break;
}
};
const obfuscateTree = (node) => {
obfuscateNode(node);
for (const child of Array.from(node.childNodes || [])) {
obfuscateTree(child);
}
return node;
};
export const obfuscateHtml = (text) => {
const dom = new JSDOM(`<!DOCTYPE html><html lang="en"><body>${text}</body>`);
const body = dom.window.document.querySelector("body");
obfuscateTree(body);
return body.innerHTML;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment