Skip to content

Instantly share code, notes, and snippets.

@frankIT
Last active June 1, 2023 09:37
Show Gist options
  • Save frankIT/3ca61aa7168f41fc234e4a57fca00e0d to your computer and use it in GitHub Desktop.
Save frankIT/3ca61aa7168f41fc234e4a57fca00e0d to your computer and use it in GitHub Desktop.
Async iubenda reload
// Looking for a way to reload/refresh iubenda asynchronously width a different config?
// Yup, doesn't look like there's any documentation about that, so this might save you some time :)
// Get localized configuration dinamically
window.getIubConf = (lang) => {
if( typeof lang === undefined )
lang = 'en';
// That's the common config as it comes from iubenda, stripped down of all the diffs with the localized ones
let commonConf = {
"askConsentAtCookiePolicyUpdate": true,
"countryDetection": true,
"enableLgpd": true,
"enableUspr": true,
"floatingPreferencesButtonDisplay": "anchored-bottom-right",
"floatingPreferencesButtonIcon": false,
"floatingPreferencesButtonZIndex": 1015,
"lgpdAppliesGlobally": false,
"perPurposeConsent": true,
"siteId": XXXXXXX,
"floatingPreferencesButtonCaption": true,
"banner": {
"acceptButtonColor": "#3B2DEE",
"acceptButtonDisplay": true,
"backgroundColor": "#FFFFFF",
"closeButtonDisplay": false,
"customizeButtonCaptionColor": "#4D4D4D",
"customizeButtonColor": "#DADADA",
"customizeButtonDisplay": true,
"explicitWithdrawal": true,
"listPurposes": true,
"linksColor": "#000000",
"position": "float-bottom-right",
"rejectButtonDisplay": true,
"showPurposesToggles": true,
"textColor": "#000000",
"zIndex": 1040
}
};
// Theese are the params that differs between the localized configs
let localConf = {};
localConf['it'] = {"cookiePolicyId": XXXXXXXX, "lang": "it"};
localConf['en'] = {"cookiePolicyId": XXXXXXXX, "lang": "en"};
return { ...commonConf, ...localConf[lang] };
}
// Refresh all with the passed locale
window.reloadCookiePolicy = (lang) => {
window._iub = {};
window._iub.csConfiguration = window.getIubConf(lang);
// Remove all iubenda scripts
Array.from(document.getElementsByTagName('SCRIPT')).forEach((script) => {
if(script.src.includes('iubenda'))
script.remove();
});
// Remove banner
document.getElementById('iubenda-cs-banner')?.remove()
// Remove preference button
Array.from(document.getElementsByClassName('iubenda-tp-btn iubenda-cs-preferences-link')).forEach((el) => {
el.remove();
});
// console.log('iubenda garbage removed');
var iub_stub = document.createElement('script');
iub_stub.type = 'text/javascript';
iub_stub.id = 'iub-stub-script';
iub_stub.src = '//cdn.iubenda.com/cs/gpp/stub.js';
var iub_cs = document.createElement('script');
iub_cs.type = 'text/javascript';
iub_cs.id = 'iub-cs-script';
iub_cs.src = '//cdn.iubenda.com/cs/iubenda_cs.js';
iub_cs.charset = "UTF-8";
iub_cs.async = true;
// Reinject scripts
document.head.appendChild(iub_stub);
document.head.appendChild(iub_cs);
// console.log('iubenda script re-injected');
}
// Inint with localized config, likely on site load.
// You need the scripts already loaded here, as commented below.
var _iub = _iub || [];
_iub.csConfiguration = window.getIubConf('it');
// <script type="text/javascript" src="//cdn.iubenda.com/cs/gpp/stub.js"></script>
// <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>
// Reload with localized config, likely on lang switch
window.reloadCookiePolicy('en'):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment