Created
February 10, 2024 15:18
-
-
Save paulshestakov/42fec74c0bd6ba8b85fbf1fab53982ac to your computer and use it in GitHub Desktop.
obfuscate html string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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