Skip to content

Instantly share code, notes, and snippets.

@aflansburg
Created April 8, 2022 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflansburg/65112ca6aacd0f7a2862ee53d229410e to your computer and use it in GitHub Desktop.
Save aflansburg/65112ca6aacd0f7a2862ee53d229410e to your computer and use it in GitHub Desktop.
Simple Redact/Hide/Obscure/Mask text content on page with JS
/* Finds text content in some element and redacts it
with the provided mask value
*/
function redactContent(textValue, tagType, maskValue){
for (const tag of document.querySelectorAll(tagType)) {
if (tag.textContent.includes(textValue)) {
tag.textContent = maskValue;
}}
}
// use case: redact pricing on a page
// before: <span>$100.45</span>
// redactContent('$', 'span', 'REDACTED')
// after: <span>REDACTED</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment