@nortic/citronkaka-ui 0.0.2-beta.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index-B7HVnsDG.js +301 -0
- package/dist/index-DE_M_lA4.js +7926 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +4 -0
- package/package.json +63 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
const m = { BASE_URL: "/", DEV: !1, DOMAIN: "localhost", MODE: "development", NODE_ENV: "development", PROD: !0, SSR: !1 }, { DOMAIN: h } = m, k = {
|
|
2
|
+
domain: h,
|
|
3
|
+
defaultModel: {
|
|
4
|
+
useraction: !1,
|
|
5
|
+
value: { necessary: !0, functional: !1, analytics: !1, marketing: !1 }
|
|
6
|
+
},
|
|
7
|
+
persistentKey: "nortic_cookie_consent",
|
|
8
|
+
persistentExpirationDays: 365,
|
|
9
|
+
categories: ["necessary", "functional", "analytics", "marketing"],
|
|
10
|
+
services: [
|
|
11
|
+
{ name: "Translations", category: "functional", scripts: [], cookies: [/^i18n_redirected/] },
|
|
12
|
+
{ name: "Color mode", category: "functional", scripts: [], cookies: [/^color-mode/] }
|
|
13
|
+
]
|
|
14
|
+
};
|
|
15
|
+
class d {
|
|
16
|
+
static createCookieString(e, t, o = {}) {
|
|
17
|
+
const { expires: s, path: i = "/", sameSite: n = "Strict", domain: c } = o, a = encodeURIComponent(t), u = s ? `; expires=${s.toUTCString()}` : "", p = c ? `; domain=${c}` : "";
|
|
18
|
+
return `${e}=${a}${u}; path=${i}; SameSite=${n}${p}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sets a cookie with the specified options.
|
|
22
|
+
* @param name The name of the cookie.
|
|
23
|
+
* @param value The value of the cookie (string or object).
|
|
24
|
+
* @param days Number of days until the cookie expires.
|
|
25
|
+
* @param domain Optional domain for the cookie.
|
|
26
|
+
*/
|
|
27
|
+
static set(e, t, o, s) {
|
|
28
|
+
const i = typeof t == "string" ? t : JSON.stringify(t), n = new Date(Date.now() + o * 24 * 60 * 60 * 1e3);
|
|
29
|
+
document.cookie = this.createCookieString(e, i, { expires: n, domain: s });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves the value of a cookie by name.
|
|
33
|
+
* @param name The name of the cookie.
|
|
34
|
+
* @returns The value of the cookie or null if not found.
|
|
35
|
+
*/
|
|
36
|
+
static get(e) {
|
|
37
|
+
const t = document.cookie.match(new RegExp(`(^| )${e}=([^;]+)`));
|
|
38
|
+
if (!t)
|
|
39
|
+
return null;
|
|
40
|
+
const o = decodeURIComponent(t[2]);
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(o);
|
|
43
|
+
} catch {
|
|
44
|
+
return o;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Deletes a cookie by name.
|
|
49
|
+
* @param name The name of the cookie.
|
|
50
|
+
* @param domain Optional domain for the cookie.
|
|
51
|
+
*/
|
|
52
|
+
static delete(e, t) {
|
|
53
|
+
document.cookie = this.createCookieString(e, "", {
|
|
54
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
55
|
+
domain: t
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
class f {
|
|
60
|
+
/**
|
|
61
|
+
* Cleans up cookies based on a list of blacklist patterns.
|
|
62
|
+
* @param blacklist An array of RegExp patterns for cookies to delete.
|
|
63
|
+
*/
|
|
64
|
+
static run(e, t = d) {
|
|
65
|
+
function o(s) {
|
|
66
|
+
document.cookie.split(";").forEach((i) => {
|
|
67
|
+
const n = i.split("=")[0].trim();
|
|
68
|
+
s.test(n) && (t.delete(n), console.warn(`Removed cookie: ${n}`));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
e.forEach((s) => {
|
|
72
|
+
o(s);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class y {
|
|
77
|
+
/**
|
|
78
|
+
* Starts the cookie interception process to block cookies based on a blacklist.
|
|
79
|
+
* @param blacklist An array of RegExp patterns for cookie names to block.
|
|
80
|
+
*/
|
|
81
|
+
static run(e) {
|
|
82
|
+
const t = Object.getOwnPropertyDescriptor(Document.prototype, "cookie");
|
|
83
|
+
if (!t) {
|
|
84
|
+
console.error("Failed to retrieve the cookie property descriptor.");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
Object.defineProperty(document, "cookie", {
|
|
88
|
+
configurable: !0,
|
|
89
|
+
enumerable: !0,
|
|
90
|
+
/**
|
|
91
|
+
* Getter for `document.cookie`.
|
|
92
|
+
* @returns The current cookies as a string.
|
|
93
|
+
*/
|
|
94
|
+
get() {
|
|
95
|
+
var o;
|
|
96
|
+
return ((o = t.get) == null ? void 0 : o.call(document)) || "";
|
|
97
|
+
},
|
|
98
|
+
/**
|
|
99
|
+
* Setter for `document.cookie`. Intercepts and blocks cookies based on the blacklist.
|
|
100
|
+
* @param cookieString The cookie string to be set.
|
|
101
|
+
*/
|
|
102
|
+
set(o) {
|
|
103
|
+
var s;
|
|
104
|
+
const i = o.split("=")[0].trim(), n = o.match(/expires=([^;]+)/i);
|
|
105
|
+
n && new Date(n[1]).getTime() < Date.now() || !e.some((c) => c.test(i)) ? (s = t.set) == null || s.call(document, o) : console.warn(`Blocked cookie: ${i}`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
class l {
|
|
111
|
+
/**
|
|
112
|
+
* Starts the script interception process with a list of blacklist patterns for script `src` attributes.
|
|
113
|
+
* @param blacklist An array of RegExp patterns for script `src` attributes to block.
|
|
114
|
+
*/
|
|
115
|
+
static run(e) {
|
|
116
|
+
this.interceptScriptCreation(e), this.observeDynamicScripts(e);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Determines whether a script should be blocked based on its `src` attribute.
|
|
120
|
+
* @param blacklist An array of RegExp patterns to match against the script `src`.
|
|
121
|
+
* @param src The `src` attribute of the script to check.
|
|
122
|
+
* @returns True if the script should be blocked, false otherwise.
|
|
123
|
+
*/
|
|
124
|
+
static shouldBlockScript(e, t) {
|
|
125
|
+
return e.some((o) => o.test(t));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Intercepts the creation of `<script>` elements via `document.createElement`.
|
|
129
|
+
* Blocks scripts with `src` matching a blacklist pattern.
|
|
130
|
+
* @param blacklist An array of RegExp patterns for script `src` attributes to block.
|
|
131
|
+
*/
|
|
132
|
+
static interceptScriptCreation(e) {
|
|
133
|
+
const t = document.createElement.bind(document);
|
|
134
|
+
document.createElement = function(o) {
|
|
135
|
+
const s = t(o);
|
|
136
|
+
if (o.toLowerCase() === "script") {
|
|
137
|
+
const i = s, n = Object.getOwnPropertyDescriptor(
|
|
138
|
+
HTMLScriptElement.prototype,
|
|
139
|
+
"src"
|
|
140
|
+
);
|
|
141
|
+
n && Object.defineProperty(i, "src", {
|
|
142
|
+
configurable: !0,
|
|
143
|
+
enumerable: !0,
|
|
144
|
+
get() {
|
|
145
|
+
var c;
|
|
146
|
+
return (c = n.get) == null ? void 0 : c.call(i);
|
|
147
|
+
},
|
|
148
|
+
set(c) {
|
|
149
|
+
var a;
|
|
150
|
+
if (l.shouldBlockScript(e, c)) {
|
|
151
|
+
console.warn(`Blocked script: ${c}`), i.type = "javascript/blocked";
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
(a = n.set) == null || a.call(i, c);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return s;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Observes dynamically added `<script>` elements in the DOM and blocks them if necessary.
|
|
163
|
+
* Prevents execution of blocked scripts by attaching a `beforescriptexecute` listener (Firefox specific fix).
|
|
164
|
+
* @param blacklist An array of RegExp patterns for script `src` attributes to block.
|
|
165
|
+
*/
|
|
166
|
+
static observeDynamicScripts(e) {
|
|
167
|
+
new MutationObserver((t) => {
|
|
168
|
+
t.forEach(({ addedNodes: o }) => {
|
|
169
|
+
o.forEach((s) => {
|
|
170
|
+
if (s.nodeType === Node.ELEMENT_NODE && s.tagName === "SCRIPT") {
|
|
171
|
+
const i = s;
|
|
172
|
+
if (i.src && this.shouldBlockScript(e, i.src)) {
|
|
173
|
+
console.warn(`Blocked dynamically added script: ${i.src}`), i.type = "javascript/blocked";
|
|
174
|
+
const n = (c) => {
|
|
175
|
+
i.getAttribute("type") === "javascript/blocked" && (c.preventDefault(), console.warn(`Prevented execution of blocked script: ${i.src}`)), i.removeEventListener("beforescriptexecute", n);
|
|
176
|
+
};
|
|
177
|
+
i.addEventListener("beforescriptexecute", n);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}).observe(document.documentElement, {
|
|
183
|
+
childList: !0,
|
|
184
|
+
subtree: !0
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
class g {
|
|
189
|
+
constructor(e = k, t = { update: [] }, o = d, s = f, i = y, n = l) {
|
|
190
|
+
this.config = e, this.listeners = t, this.persistentService = o, this.cookieCleaner = s, this.cookieInterceptor = i, this.scriptInterceptor = n, s.run(this.blacklist.cookies), i.run(this.blacklist.cookies), n.run(this.blacklist.scripts);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Retrieves the current cookie consent model from persistent storage.
|
|
194
|
+
* If the stored value is invalid, it falls back to the default model.
|
|
195
|
+
*/
|
|
196
|
+
get model() {
|
|
197
|
+
const e = this.persistentService.get(this.config.persistentKey);
|
|
198
|
+
return this.isCookieConsentModel(e) ? e : this.config.defaultModel;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Updates the cookie consent model in persistent storage after validation.
|
|
202
|
+
* @param value The new model to save. Must pass validation as a CookieConsentModel.
|
|
203
|
+
* @throws Error if the provided model is invalid.
|
|
204
|
+
*/
|
|
205
|
+
set model(e) {
|
|
206
|
+
if (!this.isCookieConsentModel(e))
|
|
207
|
+
throw new Error("Invalid model: The provided value is not a valid CookieConsentModel.");
|
|
208
|
+
this.persistentService.set(
|
|
209
|
+
this.config.persistentKey,
|
|
210
|
+
e,
|
|
211
|
+
this.config.persistentExpirationDays,
|
|
212
|
+
this.config.domain
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Update the cookie consent model and notify listeners.
|
|
217
|
+
* @param consent The new cookie consent values.
|
|
218
|
+
*/
|
|
219
|
+
update(e) {
|
|
220
|
+
this.model = { useraction: !0, value: e }, this.notifyListeners("update");
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Register an event listener for a specific event.
|
|
224
|
+
* @param eventName The name of the event.
|
|
225
|
+
* @param listener The listener function to register.
|
|
226
|
+
*/
|
|
227
|
+
on(e, t) {
|
|
228
|
+
this.listeners[e].push(t);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Notify all listeners for a specific event.
|
|
232
|
+
* @param eventName The name of the event to notify.
|
|
233
|
+
*/
|
|
234
|
+
notifyListeners(e) {
|
|
235
|
+
this.listeners[e].forEach((t) => t(this.model));
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Generates a blacklist of cookies and scripts to block based on user consent.
|
|
239
|
+
* @returns An object containing arrays of cookies and scripts to block.
|
|
240
|
+
*/
|
|
241
|
+
get blacklist() {
|
|
242
|
+
return this.config.services.reduce((e, t) => {
|
|
243
|
+
if (this.model.value[t.category] === !1) {
|
|
244
|
+
const { cookies: o, scripts: s } = t;
|
|
245
|
+
e.cookies = e.cookies.concat(o), e.scripts = e.scripts.concat(s);
|
|
246
|
+
}
|
|
247
|
+
return e;
|
|
248
|
+
}, { cookies: [], scripts: [] });
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Validates whether a value conforms to the CookieConsentModel interface.
|
|
252
|
+
* @param value The value to validate.
|
|
253
|
+
* @returns True if the value is a valid CookieConsentModel, otherwise false.
|
|
254
|
+
*/
|
|
255
|
+
isCookieConsentModel(e) {
|
|
256
|
+
if (typeof e != "object" || e === null)
|
|
257
|
+
return !1;
|
|
258
|
+
const t = e;
|
|
259
|
+
if (typeof t.useraction != "boolean" || typeof t.value != "object" || t.value === null)
|
|
260
|
+
return !1;
|
|
261
|
+
const o = t.value;
|
|
262
|
+
return o.necessary === !0 && typeof o.functional == "boolean" && typeof o.analytics == "boolean" && typeof o.marketing == "boolean";
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
window.Citronkaka = window.Citronkaka || new g();
|
|
266
|
+
const v = window.Citronkaka;
|
|
267
|
+
class b {
|
|
268
|
+
constructor(e = null, t = v) {
|
|
269
|
+
this.ui = e, this.service = t, this.init();
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Initializes the cookie consent dialog if user action has not been recorded.
|
|
273
|
+
*/
|
|
274
|
+
init() {
|
|
275
|
+
this.service.model.useraction || (this.loadUI(), this.service.on("update", (e) => {
|
|
276
|
+
e.useraction && this.unloadUI();
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Dynamically loads and displays the cookie consent dialog.
|
|
281
|
+
*/
|
|
282
|
+
async loadUI() {
|
|
283
|
+
try {
|
|
284
|
+
const { NorticCookieConsentDialog: e } = await import("./index-DE_M_lA4.js");
|
|
285
|
+
window.customElements.get("nortic-cookie-consent-dialog") || window.customElements.define("nortic-cookie-consent-dialog", e), this.ui = document.createElement("nortic-cookie-consent-dialog"), this.ui.setAttribute("data-testid", "cookie-consent"), document.body.appendChild(this.ui);
|
|
286
|
+
} catch (e) {
|
|
287
|
+
console.error("Failed to load the cookie consent dialog:", e);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Removes the cookie consent dialog from the DOM.
|
|
292
|
+
*/
|
|
293
|
+
unloadUI() {
|
|
294
|
+
this.ui && (document.body.removeChild(this.ui), this.ui = null);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const w = new b();
|
|
298
|
+
export {
|
|
299
|
+
v as b,
|
|
300
|
+
w as i
|
|
301
|
+
};
|