Skip to content

Instantly share code, notes, and snippets.

@aflansburg
Created April 20, 2022 19: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/5bfd281076c292082eb8ed4b5c49a31f to your computer and use it in GitHub Desktop.
Save aflansburg/5bfd281076c292082eb8ed4b5c49a31f to your computer and use it in GitHub Desktop.
Inject `dd-privacy-hidden` tag into specific inputs and inputs with autocomplete attribute
{
"exclusions": [
"email",
"firstName",
"lastName",
"phoneNumber",
"street1",
"street2",
"city",
"state",
"zipCode",
"other"
]
}
import { exclusions } from './dd_excludes.json';
const hideAutocompletes = () => {
const autoCompleteNodes = Array.from(document.querySelectorAll('input[autocomplete|=cc]'));
autoCompleteNodes.forEach((node) => {
node.classList.add('dd-privacy-hidden');
});
};
const hideInputExclusions = () => {
const inputNodes = Array.from(document.querySelectorAll('input'));
inputNodes.forEach((node) => {
if (exclusions.includes(node.name)) {
node.classList.add('dd-privacy-hidden');
}
});
};
const rumPromise = () =>
new Promise((resolve, reject) => {
try {
hideAutocompletes();
hideInputExclusions();
resolve();
} catch (e) {
reject(e);
}
});
const readyForRum = new Event('readyForRum');
// this can be useful with React, also note the 250ms timeout
// as sometimes input components haven't finished being 'painted'
document.addEventListener('readystatechange', () => {
if (document.readyState === 'complete') {
setTimeout(() => {
rumPromise().then(() => {
document.dispatchEvent(readyForRum);
});
}, 250);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment