|
| 1 | +(function () { |
| 2 | + var w = window; |
| 3 | + var d = document; |
| 4 | + |
| 5 | + var NON_ESSENTIAL_CATEGORIES = ['ANALYTICS', 'MARKETING', 'PERSONALIZATION', 'STORAGE']; |
| 6 | + |
| 7 | + // Registers the window.Osano('eventName', callback) pre-load queue so listeners |
| 8 | + // added before osano.js has loaded are replayed once the real API is ready. |
| 9 | + // See https://developers.osano.com/cmp/javascript-api/developer-documentation-consent-javascript-api#pre-load |
| 10 | + function setupOsanoPreload() { |
| 11 | + if (typeof w.Osano === 'function') return; |
| 12 | + w.Osano = function () { |
| 13 | + w.Osano.data.push(arguments); |
| 14 | + }; |
| 15 | + w.Osano.data = []; |
| 16 | + } |
| 17 | + |
| 18 | + function injectOsano() { |
| 19 | + var stub = d.getElementById('osano-sdk-stub'); |
| 20 | + if (stub) return; |
| 21 | + |
| 22 | + var s = d.createElement('script'); |
| 23 | + s.id = 'osano-sdk-stub'; |
| 24 | + s.type = 'text/javascript'; |
| 25 | + s.async = true; |
| 26 | + s.src = 'https://cmp.osano.com/<CUSTOMER_ID>/<CONFIG_ID>/osano.js'; |
| 27 | + d.head.appendChild(s); |
| 28 | + } |
| 29 | + |
| 30 | + function l() { |
| 31 | + if (!w.GitBook || typeof w.GitBook.registerCookieBanner !== 'function') return; |
| 32 | + |
| 33 | + w.GitBook.registerCookieBanner(function ({ onApprove, onReject }) { |
| 34 | + setupOsanoPreload(); |
| 35 | + |
| 36 | + var CONSENT_STORAGE_KEY = 'osano-gitbook-last-consent-decision'; |
| 37 | + |
| 38 | + function emitConsent(consent) { |
| 39 | + try { |
| 40 | + var hasNonEssential = |
| 41 | + !!consent && |
| 42 | + NON_ESSENTIAL_CATEGORIES.some(function (category) { |
| 43 | + return consent[category] === 'ACCEPT'; |
| 44 | + }); |
| 45 | + var decision = hasNonEssential ? 'approve' : 'reject'; |
| 46 | + |
| 47 | + // onConsentSaved replays the visitor's existing decision on every |
| 48 | + // page load, not just when it changes. GitBook's onApprove/onReject |
| 49 | + // can trigger a reload to reinitialize scripts, so forwarding every |
| 50 | + // replay would reload -> replay -> reload forever. Only forward the |
| 51 | + // decision when it's actually different from last time, and persist |
| 52 | + // that in sessionStorage since it must survive the reload. |
| 53 | + if (w.sessionStorage.getItem(CONSENT_STORAGE_KEY) === decision) return; |
| 54 | + w.sessionStorage.setItem(CONSENT_STORAGE_KEY, decision); |
| 55 | + |
| 56 | + if (hasNonEssential) { |
| 57 | + onApprove(); |
| 58 | + } else { |
| 59 | + onReject(); |
| 60 | + } |
| 61 | + } catch (e) { |
| 62 | + onReject(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + w.Osano('onConsentSaved', emitConsent); |
| 67 | + |
| 68 | + injectOsano(); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (w.attachEvent) { |
| 73 | + w.attachEvent('onload', l); |
| 74 | + } else { |
| 75 | + w.addEventListener('load', l, false); |
| 76 | + } |
| 77 | +})(); |
0 commit comments