UNPKG

xcoobee-cookie-kit-core

Version:

GDPR / CCPA Easy Cookie, Script, and Fingerprint Consent Management for Websites

89 lines (80 loc) 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _configs = require("./configs"); var cookieConsentsCache = { clear: function clear() { // console.log("cookieConsentsCache#clear"); localStorage.removeItem(_configs.xcoobeeCookiesKey); }, put: function put(cookieConsents) { // console.log("cookieConsentsCache#put"); // TODO: Save as a LUT (aka consentSettings) instead of an array. var donotsell = cookieConsents.find(function (consent) { return consent.type === "donotsell"; }); var fingerprint = cookieConsents.find(function (consent) { return consent.type === "fingerprint"; }); var xcoobeeCookies = { timestamp: Date.now(), cookies: [], donotsellConsent: donotsell ? donotsell.checked : false, fingerprintConsent: fingerprint ? fingerprint.checked : false }; _configs.cookieDefns.forEach(function (cookieDefn) { var cookieConsent = cookieConsents.find(function (item) { return item.type === cookieDefn.type; }); if (cookieConsent && cookieConsent.checked) { xcoobeeCookies.cookies.push(true); } else { xcoobeeCookies.cookies.push(false); } }); localStorage.setItem(_configs.xcoobeeCookiesKey, JSON.stringify(xcoobeeCookies)); }, get: function get() { // console.log("cookieConsentsCache#get"); var cookieConsents = null; var lastUpdated = null; var xcoobeeCookies = localStorage[_configs.xcoobeeCookiesKey] && JSON.parse(localStorage[_configs.xcoobeeCookiesKey]); if (xcoobeeCookies) { var consents = xcoobeeCookies.cookies, donotsellConsent = xcoobeeCookies.donotsellConsent, fingerprintConsent = xcoobeeCookies.fingerprintConsent, timestamp = xcoobeeCookies.timestamp; lastUpdated = timestamp; try { // If the cached cookie consents have not expired, then extract it. if (Date.now() - timestamp < _configs.expirationTime) { cookieConsents = _configs.cookieDefns.map(function (cookieDefn) { return { type: cookieDefn.type, checked: consents[cookieDefn.id] }; }); cookieConsents.push({ type: "fingerprint", checked: !!fingerprintConsent }); cookieConsents.push({ type: "donotsell", checked: !!donotsellConsent }); } } catch (err) { cookieConsents = null; console.error(err); } } return { cookieConsents: cookieConsents, lastUpdated: lastUpdated }; } }; var _default = cookieConsentsCache; exports.default = _default;