(() => { const KEYS = { TCF: "cookiebotTcfConsentString", GACM: "cookiebotGacmConsentString", }; const log = (...a) => console.log("%c[CB-PERSIST]", "color:#7c3aed", ...a); const warn = (...a) => console.warn("%c[CB-PERSIST]", "color:#ef4444", ...a); function looksLikeTcfString(v) { return ( typeof v === "string" && v.length >= 50 && /^[A-Za-z0-9.\-_]+$/.test(v) ); } function safeSetLS(k, v) { try { localStorage.setItem(k, v); return true; } catch (e) { warn("localStorage.setItem failed", e); return false; } } function alreadyPersisted() { return looksLikeTcfString(localStorage.getItem(KEYS.TCF)); } function persistIfNeeded({ tcf, gacm, source }) { if (!looksLikeTcfString(tcf)) return false; if (alreadyPersisted()) { log("SKIP", { source, tcfLen: localStorage.getItem(KEYS.TCF)?.length || 0, hasGacm: !!localStorage.getItem(KEYS.GACM), }); return true; } const ok1 = safeSetLS(KEYS.TCF, tcf); let ok2 = true; if (typeof gacm === "string" && gacm.length > 2) { ok2 = safeSetLS(KEYS.GACM, gacm); } if (ok1 && ok2) { log("WRITE", { source, tcfLen: tcf.length, gacmWritten: typeof gacm === "string" && gacm.length > 2, }); return true; } return false; } function tryFromLogConsentUrl(url, source) { try { const u = new URL(url, location.href); if (!u.pathname.includes("logconsent.ashx")) return false; const action = u.searchParams.get("action"); if (action !== "accept" && action !== "update") return false; const tcf = u.searchParams.get("iab2") || ""; const gacm = u.searchParams.get("gacm") || ""; log("logconsent", { action, tcfLen: tcf.length, gacmLen: gacm.length, source, }); return persistIfNeeded({ tcf, gacm, source: `${source} (logconsent.ashx)`, }); } catch { return false; } } function tryFromGlobals(source) { try { const tcf = window.CookieConsent?.IABConsentString || window.Cookiebot?.consent?.IABConsentString || window.Cookiebot?.consent?.tcfString || ""; const gacm = window.CookieConsent?.GACMConsentString || window.Cookiebot?.consent?.GACMConsentString || ""; if (!tcf) return false; log("globals", { source, tcfLen: tcf.length }); return persistIfNeeded({ tcf, gacm, source: `${source} (globals)`, }); } catch { return false; } } function installNetworkCapture() { if (window.fetch) { const _fetch = window.fetch; window.fetch = function (...args) { const url = typeof args[0] === "string" ? args[0] : args[0]?.url; if (url) tryFromLogConsentUrl(url, "fetch"); return _fetch.apply(this, args); }; log("fetch hooked"); } const _open = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function (method, url, ...rest) { this.__cb_url = url; return _open.call(this, method, url, ...rest); }; const _send = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.send = function (body) { if (this.__cb_url) tryFromLogConsentUrl(this.__cb_url, "xhr"); return _send.call(this, body); }; log("XHR hooked"); if (navigator.sendBeacon) { const _beacon = navigator.sendBeacon.bind(navigator); navigator.sendBeacon = function (url, data) { if (url) tryFromLogConsentUrl(url, "sendBeacon"); return _beacon(url, data); }; log("sendBeacon hooked"); } const desc = Object.getOwnPropertyDescriptor( Image.prototype, "src" ); if (desc?.set) { Object.defineProperty(Image.prototype, "src", { configurable: true, enumerable: true, get: desc.get, set(v) { if (typeof v === "string") tryFromLogConsentUrl(v, "Image.src"); return desc.set.call(this, v); }, }); log("Image.src hooked"); } } function runFallbacks(where) { tryFromGlobals(where); } function installCookiebotListeners() { window.addEventListener("CookiebotOnAccept", () => { log("CookiebotOnAccept"); runFallbacks("CookiebotOnAccept"); let i = 0; const iv = setInterval(() => { i++; runFallbacks("post-accept-poll"); if (alreadyPersisted() || i > 40) clearInterval(iv); }, 50); }); window.addEventListener("CookiebotOnConsentReady", () => { log("CookiebotOnConsentReady"); runFallbacks("CookiebotOnConsentReady"); }); } log("installed"); log("initial", { tcfLS_len: localStorage.getItem(KEYS.TCF)?.length || 0, hasGacmLS: !!localStorage.getItem(KEYS.GACM), }); installNetworkCapture(); installCookiebotListeners(); window.__CB_PERSIST_STATUS__ = () => ({ tcfLS_len: localStorage.getItem(KEYS.TCF)?.length || 0, gacmLS_len: localStorage.getItem(KEYS.GACM)?.length || 0, alreadyPersisted: alreadyPersisted(), }); })();

Rulla till toppen