iframe_consent-1.0.x-dev/js/iframe-consent-onetrust.js
js/iframe-consent-onetrust.js
/**
* @file
* Do not let iframes be loaded until accept OneTrust cookies.
*/
(function (Drupal) {
Drupal.iframeConsentOneTrust = {};
/**
* Update iframeConsent with the current categories accepted on OneTrust.
*/
Drupal.iframeConsentOneTrust.getOneTrustActiveGroups = function () {
if (typeof OnetrustActiveGroups !== 'undefined') {
const groups = OnetrustActiveGroups.split(',');
Drupal.iframeConsent.setAcceptedCategories(groups.filter((n) => n));
}
};
Drupal.iframeConsentOneTrust.manageCookiesCallback = function () {
if (typeof OneTrust === 'object') {
OneTrust.ToggleInfoDisplay();
}
};
Drupal.iframeConsentOneTrust.onConsentChange = function () {
if (typeof(OneTrust) === 'object') {
OneTrust.OnConsentChanged(function () {
Drupal.iframeConsentOneTrust.getOneTrustActiveGroups();
});
}
};
/**
* Behavior description.
*/
Drupal.behaviors.iframeConsentOneTrust = {
attach(context, settings) {
Drupal.iframeConsentOneTrust.getOneTrustActiveGroups();
Drupal.iframeConsent.setManageCookiesCallback(
Drupal.iframeConsentOneTrust.manageCookiesCallback,
);
/**
* Setup Iframe based in OneTrust current values / events.
*
* This code should run just once after the OneTrust cookies are accepted.
* The reason an interval is used is because OneTrust variable is asynchronously
* loaded and this is the way to notice when OneTrust is loaded,
* and no documentation of OneTrust load events have been found for now
* (which would prevent this workaround).
*
* @type {NodeJS.Timeout}
*/
const youtubeOnetrustCookiesInterval = setInterval(function () {
if (
typeof OneTrust === 'undefined' ||
typeof OneTrust.OnConsentChanged === 'undefined'
) {
return;
}
Drupal.iframeConsentOneTrust.getOneTrustActiveGroups();
Drupal.iframeConsentOneTrust.onConsentChange();
clearInterval(youtubeOnetrustCookiesInterval);
}, 100);
},
};
})(Drupal);
