Skip to content

Instantly share code, notes, and snippets.

@GuillaumeLeon
Created July 29, 2023 00:11
Show Gist options
  • Save GuillaumeLeon/9fa93b245d7b17d875960f5aa9c4e41e to your computer and use it in GitHub Desktop.
Save GuillaumeLeon/9fa93b245d7b17d875960f5aa9c4e41e to your computer and use it in GitHub Desktop.
Didomi is a shitty company that forces peeps to either allow complete intrusive cookies or pay to access the website (probably not RGPD friendly but nobody cares)
class ClassWatcher {
constructor(targetNode, classToWatch, classAddedCallback, classRemovedCallback) {
this.targetNode = targetNode
this.classToWatch = classToWatch
this.classAddedCallback = classAddedCallback
this.classRemovedCallback = classRemovedCallback
this.observer = null
this.lastClassState = targetNode.classList.contains(this.classToWatch)
this.init()
}
init() {
console.log('init');
this.observer = new MutationObserver(this.mutationCallback)
this.observe()
}
observe() {
this.observer.observe(this.targetNode, { attributes: true })
}
disconnect() {
this.observer.disconnect()
}
mutationCallback = mutationsList => {
for (let mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
let currentClassState = mutation.target.classList.contains(this.classToWatch)
if (this.lastClassState !== currentClassState) {
this.lastClassState = currentClassState
if (currentClassState) {
this.classAddedCallback()
}
else {
this.classRemovedCallback()
}
}
}
}
}
}
const watcher = new ClassWatcher(document.body, 'didomi-popup-open', () => {
document.body.classList.remove('didomi-popup-open');
document.querySelector('#didomi-host').remove();
}, () => { })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment