Created
April 20, 2022 19:53
-
-
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
This file contains hidden or 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
{ | |
"exclusions": [ | |
"email", | |
"firstName", | |
"lastName", | |
"phoneNumber", | |
"street1", | |
"street2", | |
"city", | |
"state", | |
"zipCode", | |
"other" | |
] | |
} |
This file contains hidden or 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 { 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