UNPKG

@ambitiondev/vue-cookiebot

Version:

Composable for Cookiebot in Vue applications

163 lines (158 loc) 5.82 kB
import { useLogger, consentBannerURL, CB_NAME, CD_NAME, cookieDeclarationURL } from '@ambitiondev/cookiebot-common'; import { inject, ref, computed, unref } from 'vue'; function useScriptHelper(scriptType = 'text/javascript') { async function createScriptWithOptions(options, src, async = true) { const script = document.createElement('script'); script.src = src; script.type = scriptType; script.async = async; await Promise.all(options.map((option) => { return script.setAttribute(option.name, option.value); })); return script; } async function removeScript(context, script, resetHTML = false) { const _script = typeof script === 'string' ? context.querySelector(`#${script}`) : script; if (_script !== null) { await context.removeChild(_script); } if (resetHTML === true) { context.innerHTML = ''; } } return { createScriptWithOptions, removeScript, }; } // Vendor function useCookiebot(settings) { // Composable const { createScriptWithOptions, removeScript } = useScriptHelper(); const { deprecationNotice, info, error, warn } = useLogger(); const pluginOptions = inject('cookieBotOptions', {}); const _options = Object.assign(Object.assign({}, pluginOptions), settings); if (typeof _options.cookieBotId === 'undefined') { error('No settings have been found. Did you forget to instantiate the plugin in your app definition (app.use(...))?'); } // Refs const processingCB = ref(false); const processingCD = ref(false); // Computed const bannerURL = computed(() => consentBannerURL(Object.assign(Object.assign({}, _options), { cookieBotId: _options.cookieBotId || '' }))); async function consentBanner() { if (processingCB.value) { return warn('Processing request. Aborting...'); } processingCB.value = true; if (document.getElementById(CB_NAME) !== null) { return info('Consent banner already initialized. Skipping...'); } const script = await createScriptWithOptions([ { name: 'id', value: CB_NAME, }, ], bannerURL.value); await document.body.appendChild(script); processingCB.value = false; } /** @deprecated */ async function consentPage(ref) { deprecationNotice('consentPage', 'cookieDeclaration'); cookieDeclaration(ref); } async function cookieDeclaration(ref) { const _ref = unref(ref); if (processingCD.value) { return warn('Processing request. Aborting...'); } processingCD.value = true; if (!_ref) { return warn('No HTML element or element ref is given to inject cookie declaration script. Skipping...'); } if (!_options.cookieBotId) { return error('No Cookiebot ID found. Please set a valid ID'); } if (document.getElementById(CD_NAME) !== null || document.querySelector(`[data-cp-id=${CD_NAME}]`) !== null) { return info('Consent page already initialized. Skipping...'); } const _settings = [ { name: 'data-cp-id', value: CD_NAME, }, ]; if (settings === null || settings === void 0 ? void 0 : settings.culture) { _settings.push({ name: 'data-culture', value: settings.culture, }); } const script = await createScriptWithOptions(_settings, cookieDeclarationURL(_options.cookieBotId), true); await _ref.appendChild(script); processingCD.value = false; } async function destroyConsentBanner() { await removeScript(document.body, CB_NAME); } /** @deprecated */ async function destroyConsentPage(ref) { deprecationNotice('destroyConsentPage', 'destroyCookieDeclaration'); destroyCookieDeclaration(ref); } async function destroyCookieDeclaration(ref) { const _ref = unref(ref); if (!_ref) { return error('No HTML element or element ref is given. Aborting...'); } const scriptEl = document.getElementById(CD_NAME); const createdScriptEl = document.querySelector(`[data-cp-id=${CD_NAME}]`); if (scriptEl) { await removeScript(_ref, CD_NAME); } if (createdScriptEl) { await removeScript(_ref, createdScriptEl); } _ref.innerHTML = ''; } async function resetConsentBanner() { await destroyConsentBanner(); consentBanner(); } function renew() { if ('Cookiebot' in window) { return window.Cookiebot.renew(); } error('Not able to renew consent. Cookiebot instance is not defined.'); } return { consentBanner, consentPage, cookieDeclaration, destroyConsentBanner, destroyConsentPage, destroyCookieDeclaration, renew, resetConsentBanner, }; } const cookieBot = { install: (app, options) => { app.provide('cookieBotOptions', options); requestAnimationFrame(() => { if (typeof app.config.globalProperties.$router === 'object') { app.config.globalProperties.$router.afterEach(() => { if ('Cookiebot' in window) { requestAnimationFrame(() => { window.Cookiebot.runScripts(); }); } }); } }); }, }; export { cookieBot, useCookiebot }; //# sourceMappingURL=index.esm.js.map