Skip to content

Commit

Permalink
fix: avoid adding the mutation observer if cookie is present
Browse files Browse the repository at this point in the history
  • Loading branch information
chicharr committed May 24, 2024
1 parent 7fad42a commit b93e598
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,29 @@ if ([770, 1136].includes(Array.from(window.origin).map((a) => a.charCodeAt(0)).r

if (cmpCookie) {
sampleRUM('consent', { source: 'onetrust', target: 'hidden' });
}
} else {
let consentMutationObserver;
const trackShowConsent = () => {
if (document.querySelector('body > div#onetrust-consent-sdk')) {
sampleRUM('consent', { source: 'onetrust', target: 'show' });
if (consentMutationObserver) {
consentMutationObserver.disconnect();
}
return true;
}
return false;
};

let consentMutationObserver;
const trackShowConsent = () => {
if (document.querySelector('body > div#onetrust-consent-sdk')) {
sampleRUM('consent', { source: 'onetrust', target: 'show' });
if (!trackShowConsent()) {
// eslint-disable-next-line max-len
consentMutationObserver = window.MutationObserver ? new MutationObserver(trackShowConsent) : null;
if (consentMutationObserver) {
consentMutationObserver.disconnect();
consentMutationObserver.observe(
document.body,
// eslint-disable-next-line object-curly-newline
{ attributes: false, childList: true, subtree: false },
);
}
return true;
}
return false;
};

if (!trackShowConsent()) {
// eslint-disable-next-line max-len
consentMutationObserver = window.MutationObserver ? new MutationObserver(trackShowConsent) : null;
if (consentMutationObserver) {
consentMutationObserver.observe(
document.body,
// eslint-disable-next-line object-curly-newline
{ attributes: false, childList: true, subtree: false },
);
}
}
}

0 comments on commit b93e598

Please sign in to comment.