@cookieyes/core 0.1.0 → 0.1.1
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.cjs +1 -595
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -584
- package/dist/index.js.map +1 -1
- package/package.json +9 -10
- package/dist/index.d.cts +0 -210
package/dist/index.cjs
CHANGED
|
@@ -1,596 +1,2 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/cookie.ts
|
|
4
|
-
var COOKIE_NAME = "cookieyes-consent";
|
|
5
|
-
var CONSENT_CATEGORIES = [
|
|
6
|
-
"necessary",
|
|
7
|
-
"functional",
|
|
8
|
-
"analytics",
|
|
9
|
-
"performance",
|
|
10
|
-
"advertisement"
|
|
11
|
-
];
|
|
12
|
-
function parseCookie(raw) {
|
|
13
|
-
const fields = {};
|
|
14
|
-
const pairs = raw.split(",");
|
|
15
|
-
for (const pair of pairs) {
|
|
16
|
-
const colonIdx = pair.indexOf(":");
|
|
17
|
-
if (colonIdx === -1) continue;
|
|
18
|
-
const key = pair.slice(0, colonIdx).trim();
|
|
19
|
-
const value = pair.slice(colonIdx + 1).trim();
|
|
20
|
-
if (key in fields || VALID_KEYS.has(key)) {
|
|
21
|
-
fields[key] = value;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return fields;
|
|
25
|
-
}
|
|
26
|
-
var VALID_KEYS = /* @__PURE__ */ new Set([
|
|
27
|
-
"consentid",
|
|
28
|
-
"consent",
|
|
29
|
-
"action",
|
|
30
|
-
"necessary",
|
|
31
|
-
"functional",
|
|
32
|
-
"analytics",
|
|
33
|
-
"performance",
|
|
34
|
-
"advertisement",
|
|
35
|
-
"lastRenewedDate"
|
|
36
|
-
]);
|
|
37
|
-
function serializeCookie(snapshot) {
|
|
38
|
-
const parts = [
|
|
39
|
-
`consentid:${snapshot.consentId}`,
|
|
40
|
-
`consent:${snapshot.hasActed ? "yes" : "no"}`,
|
|
41
|
-
`action:${snapshot.hasActed ? "yes" : "no"}`
|
|
42
|
-
];
|
|
43
|
-
for (const cat of CONSENT_CATEGORIES) {
|
|
44
|
-
parts.push(`${cat}:${snapshot.categories[cat] ? "yes" : "no"}`);
|
|
45
|
-
}
|
|
46
|
-
parts.push(`lastRenewedDate:${snapshot.lastRenewed ?? Date.now()}`);
|
|
47
|
-
return parts.join(",");
|
|
48
|
-
}
|
|
49
|
-
function readConsentCookie() {
|
|
50
|
-
if (typeof document === "undefined") return null;
|
|
51
|
-
const cookies = document.cookie.split(";");
|
|
52
|
-
for (const cookie of cookies) {
|
|
53
|
-
const trimmed = cookie.trim();
|
|
54
|
-
const eqIdx = trimmed.indexOf("=");
|
|
55
|
-
if (eqIdx === -1) continue;
|
|
56
|
-
const name = trimmed.slice(0, eqIdx).trim();
|
|
57
|
-
if (name === COOKIE_NAME) {
|
|
58
|
-
const value = trimmed.slice(eqIdx + 1).trim();
|
|
59
|
-
return parseCookie(decodeURIComponent(value));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
function writeConsentCookie(snapshot) {
|
|
65
|
-
if (typeof document === "undefined") return;
|
|
66
|
-
const value = encodeURIComponent(serializeCookie(snapshot));
|
|
67
|
-
const maxAge = 365 * 24 * 60 * 60;
|
|
68
|
-
document.cookie = `${COOKIE_NAME}=${value}; max-age=${maxAge}; path=/; SameSite=Lax`;
|
|
69
|
-
}
|
|
70
|
-
function clearConsentCookie() {
|
|
71
|
-
if (typeof document === "undefined") return;
|
|
72
|
-
document.cookie = `${COOKIE_NAME}=; max-age=0; path=/`;
|
|
73
|
-
}
|
|
74
|
-
function generateConsentId() {
|
|
75
|
-
const array = new Uint8Array(32);
|
|
76
|
-
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
77
|
-
crypto.getRandomValues(array);
|
|
78
|
-
} else {
|
|
79
|
-
for (let i = 0; i < array.length; i++) {
|
|
80
|
-
array[i] = Math.floor(Math.random() * 256);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return btoa(String.fromCharCode(...array)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "").slice(0, 44);
|
|
84
|
-
}
|
|
85
|
-
function rawFieldsToSnapshot(fields, regulation) {
|
|
86
|
-
const categories = {
|
|
87
|
-
necessary: true,
|
|
88
|
-
functional: fields.functional === "yes",
|
|
89
|
-
analytics: fields.analytics === "yes",
|
|
90
|
-
performance: fields.performance === "yes",
|
|
91
|
-
advertisement: fields.advertisement === "yes"
|
|
92
|
-
};
|
|
93
|
-
return {
|
|
94
|
-
consentId: fields.consentid ?? generateConsentId(),
|
|
95
|
-
hasActed: fields.action === "yes",
|
|
96
|
-
categories,
|
|
97
|
-
regulation,
|
|
98
|
-
lastRenewed: fields.lastRenewedDate ? Number(fields.lastRenewedDate) : void 0
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
function defaultSnapshot(consentId, regulation) {
|
|
102
|
-
const isOptOut = regulation === "CCPA";
|
|
103
|
-
return {
|
|
104
|
-
consentId,
|
|
105
|
-
hasActed: false,
|
|
106
|
-
categories: {
|
|
107
|
-
necessary: true,
|
|
108
|
-
functional: isOptOut,
|
|
109
|
-
analytics: isOptOut,
|
|
110
|
-
performance: isOptOut,
|
|
111
|
-
advertisement: isOptOut
|
|
112
|
-
},
|
|
113
|
-
regulation
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// src/translations/en.ts
|
|
118
|
-
var en = {
|
|
119
|
-
bannerTitle: "We value your privacy",
|
|
120
|
-
bannerDescription: "We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking \u201CAccept All\u201D, you consent to our use of cookies.",
|
|
121
|
-
acceptAll: "Accept All",
|
|
122
|
-
rejectAll: "Reject All",
|
|
123
|
-
managePreferences: "Customise",
|
|
124
|
-
savePreferences: "Save My Preferences",
|
|
125
|
-
doNotSell: "Do Not Sell or Share My Personal Information",
|
|
126
|
-
ccpaDescription: "This website or its third-party tools process personal data. You can opt out of the sale of your personal information by clicking on the \u201CDo Not Sell or Share My Personal Information\u201D link.",
|
|
127
|
-
accept: "Accept",
|
|
128
|
-
poweredBy: "Powered by CookieYes",
|
|
129
|
-
preferencesTitle: "Customise Consent Preferences",
|
|
130
|
-
preferencesIntro: "We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.",
|
|
131
|
-
categories: {
|
|
132
|
-
necessary: {
|
|
133
|
-
label: "Necessary",
|
|
134
|
-
description: "Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data."
|
|
135
|
-
},
|
|
136
|
-
functional: {
|
|
137
|
-
label: "Functional",
|
|
138
|
-
description: "Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features."
|
|
139
|
-
},
|
|
140
|
-
analytics: {
|
|
141
|
-
label: "Analytics",
|
|
142
|
-
description: "Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc."
|
|
143
|
-
},
|
|
144
|
-
performance: {
|
|
145
|
-
label: "Performance",
|
|
146
|
-
description: "Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors."
|
|
147
|
-
},
|
|
148
|
-
advertisement: {
|
|
149
|
-
label: "Advertisement",
|
|
150
|
-
description: "Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns."
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
optOut: {
|
|
154
|
-
title: "Opt-out Preferences",
|
|
155
|
-
description: 'We use third-party cookies that help us analyse how you use this website, store your preferences, and provide the content and advertisements that are relevant to you. However, you can opt out of these cookies by checking "Do Not Sell or Share My Personal Information" and clicking the "Save My Preferences" button. Once you opt out, you can opt in again at any time by unchecking "Do Not Sell or Share My Personal Information" and clicking the "Save My Preferences" button.',
|
|
156
|
-
cancel: "Cancel",
|
|
157
|
-
successText: "Your opt-out preference has been honored.",
|
|
158
|
-
successCountdown: "Banner closes automatically in {seconds} s..."
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
// src/i18n.ts
|
|
163
|
-
function resolveTranslations(i18n) {
|
|
164
|
-
const messages = i18n?.messages ?? {};
|
|
165
|
-
const detect = i18n?.detectBrowserLanguage ?? true;
|
|
166
|
-
const candidates = [];
|
|
167
|
-
if (i18n?.locale) candidates.push(i18n.locale);
|
|
168
|
-
if (detect && typeof navigator !== "undefined" && navigator.language) {
|
|
169
|
-
candidates.push(navigator.language);
|
|
170
|
-
}
|
|
171
|
-
for (const tag of candidates) {
|
|
172
|
-
const primary = tag.split("-")[0]?.toLowerCase() ?? "";
|
|
173
|
-
const hit = messages[tag] ?? (primary ? messages[primary] : void 0);
|
|
174
|
-
if (hit) return hit;
|
|
175
|
-
}
|
|
176
|
-
return messages.en ?? en;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// src/scripts.ts
|
|
180
|
-
var registry = /* @__PURE__ */ new Map();
|
|
181
|
-
var loaded = /* @__PURE__ */ new Set();
|
|
182
|
-
var injected = /* @__PURE__ */ new Map();
|
|
183
|
-
function registerScript(entry) {
|
|
184
|
-
registry.set(entry.id, entry);
|
|
185
|
-
}
|
|
186
|
-
function applyScripts(categories) {
|
|
187
|
-
if (typeof document === "undefined") return;
|
|
188
|
-
for (const [id, entry] of registry) {
|
|
189
|
-
const allowed = categories[entry.category] === true;
|
|
190
|
-
const strategy = entry.strategy ?? "afterConsent";
|
|
191
|
-
if (allowed) {
|
|
192
|
-
if (strategy === "lazyOnce" && loaded.has(id)) continue;
|
|
193
|
-
if (!injected.has(id)) {
|
|
194
|
-
injectScript(id, entry);
|
|
195
|
-
}
|
|
196
|
-
} else {
|
|
197
|
-
removeScript(id);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
function injectScript(id, entry) {
|
|
202
|
-
const existing = document.getElementById(id);
|
|
203
|
-
if (existing) return;
|
|
204
|
-
const el = document.createElement("script");
|
|
205
|
-
el.id = id;
|
|
206
|
-
el.src = entry.src;
|
|
207
|
-
el.async = true;
|
|
208
|
-
if (entry.onLoad) {
|
|
209
|
-
el.addEventListener("load", entry.onLoad, { once: true });
|
|
210
|
-
}
|
|
211
|
-
document.head.appendChild(el);
|
|
212
|
-
injected.set(id, el);
|
|
213
|
-
loaded.add(id);
|
|
214
|
-
}
|
|
215
|
-
function removeScript(id) {
|
|
216
|
-
const el = injected.get(id);
|
|
217
|
-
if (el) {
|
|
218
|
-
el.remove();
|
|
219
|
-
injected.delete(id);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// src/sync.ts
|
|
224
|
-
function buildConsentPayload(snapshot) {
|
|
225
|
-
return {
|
|
226
|
-
consentId: snapshot.consentId,
|
|
227
|
-
categories: snapshot.categories,
|
|
228
|
-
regulation: snapshot.regulation,
|
|
229
|
-
domain: typeof window !== "undefined" ? window.location.hostname : "unknown"
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
async function pushConsent(apiUrl, apiKey, snapshot) {
|
|
233
|
-
const payload = buildConsentPayload(snapshot);
|
|
234
|
-
const headers = {
|
|
235
|
-
"Content-Type": "application/json"
|
|
236
|
-
};
|
|
237
|
-
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
|
238
|
-
try {
|
|
239
|
-
await fetch(apiUrl, {
|
|
240
|
-
method: "POST",
|
|
241
|
-
headers,
|
|
242
|
-
body: JSON.stringify(payload),
|
|
243
|
-
keepalive: true
|
|
244
|
-
});
|
|
245
|
-
} catch {
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// src/manager.ts
|
|
250
|
-
var ALL_CATEGORIES = [
|
|
251
|
-
"necessary",
|
|
252
|
-
"functional",
|
|
253
|
-
"analytics",
|
|
254
|
-
"performance",
|
|
255
|
-
"advertisement"
|
|
256
|
-
];
|
|
257
|
-
function createConsentManager(config) {
|
|
258
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
259
|
-
let state;
|
|
260
|
-
let isPreferencesOpen = false;
|
|
261
|
-
let lastPersistedCategories;
|
|
262
|
-
const rawFields = readConsentCookie();
|
|
263
|
-
const savedRegulation = config.regulation ?? "DEFAULT";
|
|
264
|
-
if (rawFields) {
|
|
265
|
-
state = rawFieldsToSnapshot(rawFields, savedRegulation);
|
|
266
|
-
} else {
|
|
267
|
-
const consentId = generateConsentId();
|
|
268
|
-
state = defaultSnapshot(consentId, savedRegulation);
|
|
269
|
-
if (state.regulation === "CCPA") {
|
|
270
|
-
writeConsentCookie(state);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
lastPersistedCategories = { ...state.categories };
|
|
274
|
-
Promise.resolve().then(() => config.onConsentReady?.(state));
|
|
275
|
-
function notify() {
|
|
276
|
-
const snap = snapshot();
|
|
277
|
-
for (const fn of listeners) fn(snap);
|
|
278
|
-
applyScripts(state.categories);
|
|
279
|
-
}
|
|
280
|
-
function snapshot() {
|
|
281
|
-
return {
|
|
282
|
-
consentId: state.consentId,
|
|
283
|
-
hasActed: state.hasActed,
|
|
284
|
-
categories: { ...state.categories },
|
|
285
|
-
regulation: state.regulation,
|
|
286
|
-
lastRenewed: state.lastRenewed
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
function persist() {
|
|
290
|
-
state = {
|
|
291
|
-
...state,
|
|
292
|
-
hasActed: true,
|
|
293
|
-
lastRenewed: Date.now()
|
|
294
|
-
};
|
|
295
|
-
writeConsentCookie(state);
|
|
296
|
-
if (config.backend) {
|
|
297
|
-
try {
|
|
298
|
-
Promise.resolve(config.backend.persist(buildConsentPayload(state))).catch(() => void 0);
|
|
299
|
-
} catch {
|
|
300
|
-
}
|
|
301
|
-
} else if (config.apiUrl) {
|
|
302
|
-
void pushConsent(config.apiUrl, config.apiKey, state);
|
|
303
|
-
}
|
|
304
|
-
let didRevoke = false;
|
|
305
|
-
for (const cat of ALL_CATEGORIES) {
|
|
306
|
-
if (lastPersistedCategories[cat] && !state.categories[cat]) {
|
|
307
|
-
didRevoke = true;
|
|
308
|
-
break;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
lastPersistedCategories = { ...state.categories };
|
|
312
|
-
notify();
|
|
313
|
-
config.onConsentUpdate?.(state);
|
|
314
|
-
if (didRevoke && config.reloadOnRevoke && typeof window !== "undefined") {
|
|
315
|
-
window.location.reload();
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
const manager = {
|
|
319
|
-
get consentId() {
|
|
320
|
-
return state.consentId;
|
|
321
|
-
},
|
|
322
|
-
get hasActed() {
|
|
323
|
-
return state.hasActed;
|
|
324
|
-
},
|
|
325
|
-
get categories() {
|
|
326
|
-
return { ...state.categories };
|
|
327
|
-
},
|
|
328
|
-
get regulation() {
|
|
329
|
-
return state.regulation;
|
|
330
|
-
},
|
|
331
|
-
get lastRenewed() {
|
|
332
|
-
return state.lastRenewed;
|
|
333
|
-
},
|
|
334
|
-
get isPreferencesOpen() {
|
|
335
|
-
return isPreferencesOpen;
|
|
336
|
-
},
|
|
337
|
-
acceptAll() {
|
|
338
|
-
state = {
|
|
339
|
-
...state,
|
|
340
|
-
categories: {
|
|
341
|
-
necessary: true,
|
|
342
|
-
functional: true,
|
|
343
|
-
analytics: true,
|
|
344
|
-
performance: true,
|
|
345
|
-
advertisement: true
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
isPreferencesOpen = false;
|
|
349
|
-
persist();
|
|
350
|
-
},
|
|
351
|
-
rejectAll() {
|
|
352
|
-
state = {
|
|
353
|
-
...state,
|
|
354
|
-
categories: {
|
|
355
|
-
necessary: true,
|
|
356
|
-
functional: false,
|
|
357
|
-
analytics: false,
|
|
358
|
-
performance: false,
|
|
359
|
-
advertisement: false
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
isPreferencesOpen = false;
|
|
363
|
-
persist();
|
|
364
|
-
},
|
|
365
|
-
acceptSelected(categories) {
|
|
366
|
-
const cats = { ...state.categories };
|
|
367
|
-
for (const cat of ALL_CATEGORIES) {
|
|
368
|
-
if (cat === "necessary") continue;
|
|
369
|
-
cats[cat] = categories.includes(cat);
|
|
370
|
-
}
|
|
371
|
-
state = { ...state, categories: cats };
|
|
372
|
-
isPreferencesOpen = false;
|
|
373
|
-
persist();
|
|
374
|
-
},
|
|
375
|
-
updateCategory(category, value) {
|
|
376
|
-
if (category === "necessary") return;
|
|
377
|
-
state = {
|
|
378
|
-
...state,
|
|
379
|
-
categories: { ...state.categories, [category]: value }
|
|
380
|
-
};
|
|
381
|
-
notify();
|
|
382
|
-
},
|
|
383
|
-
savePreferences() {
|
|
384
|
-
isPreferencesOpen = false;
|
|
385
|
-
persist();
|
|
386
|
-
},
|
|
387
|
-
resetConsent() {
|
|
388
|
-
clearConsentCookie();
|
|
389
|
-
const consentId = generateConsentId();
|
|
390
|
-
state = defaultSnapshot(consentId, state.regulation);
|
|
391
|
-
isPreferencesOpen = false;
|
|
392
|
-
notify();
|
|
393
|
-
},
|
|
394
|
-
showPreferences() {
|
|
395
|
-
isPreferencesOpen = true;
|
|
396
|
-
notify();
|
|
397
|
-
},
|
|
398
|
-
hidePreferences() {
|
|
399
|
-
isPreferencesOpen = false;
|
|
400
|
-
notify();
|
|
401
|
-
},
|
|
402
|
-
subscribe(listener) {
|
|
403
|
-
listeners.add(listener);
|
|
404
|
-
return () => listeners.delete(listener);
|
|
405
|
-
},
|
|
406
|
-
registerScript(entry) {
|
|
407
|
-
registerScript(entry);
|
|
408
|
-
applyScripts(state.categories);
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
applyScripts(state.categories);
|
|
412
|
-
return manager;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// src/network-blocker.ts
|
|
416
|
-
function findBlockingRule(rules, url, method, hasConsent) {
|
|
417
|
-
let parsed;
|
|
418
|
-
try {
|
|
419
|
-
const base = typeof window !== "undefined" ? window.location.href : "http://localhost";
|
|
420
|
-
parsed = new URL(url, base);
|
|
421
|
-
} catch {
|
|
422
|
-
return null;
|
|
423
|
-
}
|
|
424
|
-
const host = parsed.hostname.toLowerCase();
|
|
425
|
-
const path = parsed.pathname + parsed.search;
|
|
426
|
-
const upperMethod = method.toUpperCase();
|
|
427
|
-
for (const rule of rules) {
|
|
428
|
-
const ruleHost = rule.domain.toLowerCase();
|
|
429
|
-
if (host !== ruleHost && !host.endsWith("." + ruleHost)) continue;
|
|
430
|
-
if (rule.pathIncludes && !path.includes(rule.pathIncludes)) continue;
|
|
431
|
-
if (rule.methods && !rule.methods.map((m) => m.toUpperCase()).includes(upperMethod)) {
|
|
432
|
-
continue;
|
|
433
|
-
}
|
|
434
|
-
if (hasConsent(rule.category)) continue;
|
|
435
|
-
return rule;
|
|
436
|
-
}
|
|
437
|
-
return null;
|
|
438
|
-
}
|
|
439
|
-
var active = null;
|
|
440
|
-
function installNetworkBlocker(config, hasConsent) {
|
|
441
|
-
if (typeof window === "undefined") return () => void 0;
|
|
442
|
-
if (active) return () => void 0;
|
|
443
|
-
if (!config.rules.length) return () => void 0;
|
|
444
|
-
const state = {
|
|
445
|
-
originalFetch: window.fetch,
|
|
446
|
-
originalXhrOpen: XMLHttpRequest.prototype.open,
|
|
447
|
-
originalXhrSend: XMLHttpRequest.prototype.send
|
|
448
|
-
};
|
|
449
|
-
active = state;
|
|
450
|
-
const logBlocked = config.logBlockedRequests !== false;
|
|
451
|
-
function notify(info) {
|
|
452
|
-
if (logBlocked) {
|
|
453
|
-
console.warn(
|
|
454
|
-
`[cookieyes] blocked ${info.method} ${info.url} (rule "${info.rule.id}", category: ${info.rule.category})`
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
config.onRequestBlocked?.(info);
|
|
458
|
-
}
|
|
459
|
-
window.fetch = function patchedFetch(input, init) {
|
|
460
|
-
let url = "";
|
|
461
|
-
let method = init?.method ?? "GET";
|
|
462
|
-
if (typeof input === "string") {
|
|
463
|
-
url = input;
|
|
464
|
-
} else if (input instanceof URL) {
|
|
465
|
-
url = input.toString();
|
|
466
|
-
} else {
|
|
467
|
-
url = input.url;
|
|
468
|
-
method = init?.method ?? input.method;
|
|
469
|
-
}
|
|
470
|
-
const blockingRule = findBlockingRule(config.rules, url, method, hasConsent);
|
|
471
|
-
if (blockingRule) {
|
|
472
|
-
notify({ rule: blockingRule, url, method });
|
|
473
|
-
return Promise.reject(
|
|
474
|
-
new TypeError(
|
|
475
|
-
`Blocked by consent (rule: ${blockingRule.id}, category: ${blockingRule.category})`
|
|
476
|
-
)
|
|
477
|
-
);
|
|
478
|
-
}
|
|
479
|
-
return state.originalFetch.call(window, input, init);
|
|
480
|
-
};
|
|
481
|
-
XMLHttpRequest.prototype.open = function patchedOpen(method, url, ...rest) {
|
|
482
|
-
this._cyUrl = url.toString();
|
|
483
|
-
this._cyMethod = method;
|
|
484
|
-
return state.originalXhrOpen.apply(this, [method, url, ...rest]);
|
|
485
|
-
};
|
|
486
|
-
XMLHttpRequest.prototype.send = function patchedSend(body) {
|
|
487
|
-
const url = this._cyUrl ?? "";
|
|
488
|
-
const method = this._cyMethod ?? "GET";
|
|
489
|
-
const blockingRule = findBlockingRule(config.rules, url, method, hasConsent);
|
|
490
|
-
if (blockingRule) {
|
|
491
|
-
notify({ rule: blockingRule, url, method });
|
|
492
|
-
this.abort();
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
return state.originalXhrSend.call(this, body);
|
|
496
|
-
};
|
|
497
|
-
return uninstallNetworkBlocker;
|
|
498
|
-
}
|
|
499
|
-
function uninstallNetworkBlocker() {
|
|
500
|
-
if (!active) return;
|
|
501
|
-
if (typeof window !== "undefined") {
|
|
502
|
-
window.fetch = active.originalFetch;
|
|
503
|
-
XMLHttpRequest.prototype.open = active.originalXhrOpen;
|
|
504
|
-
XMLHttpRequest.prototype.send = active.originalXhrSend;
|
|
505
|
-
}
|
|
506
|
-
active = null;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
// src/runtime.ts
|
|
510
|
-
function splitCategories(categories) {
|
|
511
|
-
const allowed = [];
|
|
512
|
-
const denied = [];
|
|
513
|
-
for (const cat of Object.keys(categories)) {
|
|
514
|
-
if (categories[cat]) allowed.push(cat);
|
|
515
|
-
else denied.push(cat);
|
|
516
|
-
}
|
|
517
|
-
return { allowedCategories: allowed, deniedCategories: denied };
|
|
518
|
-
}
|
|
519
|
-
var _runtime = null;
|
|
520
|
-
function getOrCreateConsentRuntime(options) {
|
|
521
|
-
if (_runtime) return _runtime;
|
|
522
|
-
const changeListeners = /* @__PURE__ */ new Set();
|
|
523
|
-
const userOnConsentUpdate = options.onConsentUpdate;
|
|
524
|
-
const cfg = {};
|
|
525
|
-
if (options.mode === "self-hosted") {
|
|
526
|
-
if (options.backend) cfg.backend = options.backend;
|
|
527
|
-
else if (options.backendURL) cfg.apiUrl = options.backendURL;
|
|
528
|
-
}
|
|
529
|
-
if (options.apiKey) cfg.apiKey = options.apiKey;
|
|
530
|
-
if (options.overrides?.regulation) cfg.regulation = options.overrides.regulation;
|
|
531
|
-
if (options.colorScheme) cfg.colorScheme = options.colorScheme;
|
|
532
|
-
if (options.theme) cfg.theme = options.theme;
|
|
533
|
-
if (options.reloadOnRevoke) cfg.reloadOnRevoke = options.reloadOnRevoke;
|
|
534
|
-
if (options.onConsentReady) cfg.onConsentReady = options.onConsentReady;
|
|
535
|
-
cfg.onConsentUpdate = (snap) => {
|
|
536
|
-
userOnConsentUpdate?.(snap);
|
|
537
|
-
const payload = splitCategories(snap.categories);
|
|
538
|
-
for (const fn of changeListeners) fn(payload);
|
|
539
|
-
};
|
|
540
|
-
const manager = createConsentManager(cfg);
|
|
541
|
-
function activeUI() {
|
|
542
|
-
if (manager.isPreferencesOpen) return "dialog";
|
|
543
|
-
if (!manager.hasActed) return "banner";
|
|
544
|
-
return null;
|
|
545
|
-
}
|
|
546
|
-
function buildState() {
|
|
547
|
-
const categories = manager.categories;
|
|
548
|
-
return {
|
|
549
|
-
consentId: manager.consentId,
|
|
550
|
-
hasActed: manager.hasActed,
|
|
551
|
-
categories,
|
|
552
|
-
consents: categories,
|
|
553
|
-
regulation: manager.regulation,
|
|
554
|
-
lastRenewed: manager.lastRenewed,
|
|
555
|
-
activeUI: activeUI(),
|
|
556
|
-
has: (category) => manager.categories[category] ?? false,
|
|
557
|
-
saveConsents: async (target) => {
|
|
558
|
-
if (target === "all") manager.acceptAll();
|
|
559
|
-
else if (target === "necessary") manager.rejectAll();
|
|
560
|
-
else manager.acceptSelected(target);
|
|
561
|
-
},
|
|
562
|
-
setConsent: (category, value) => manager.updateCategory(category, value),
|
|
563
|
-
subscribeToConsentChanges: (listener) => {
|
|
564
|
-
changeListeners.add(listener);
|
|
565
|
-
return () => {
|
|
566
|
-
changeListeners.delete(listener);
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
const consentStore = {
|
|
572
|
-
subscribe: (listener) => manager.subscribe(() => listener(buildState())),
|
|
573
|
-
getState: buildState
|
|
574
|
-
};
|
|
575
|
-
if (options.networkBlocker && options.networkBlocker.rules.length > 0) {
|
|
576
|
-
installNetworkBlocker(options.networkBlocker, (cat) => manager.categories[cat] === true);
|
|
577
|
-
}
|
|
578
|
-
_runtime = { consentManager: manager, consentStore };
|
|
579
|
-
return _runtime;
|
|
580
|
-
}
|
|
581
|
-
function resetConsentRuntime() {
|
|
582
|
-
_runtime = null;
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
exports.createConsentManager = createConsentManager;
|
|
586
|
-
exports.defaultTranslations = en;
|
|
587
|
-
exports.generateConsentId = generateConsentId;
|
|
588
|
-
exports.getOrCreateConsentRuntime = getOrCreateConsentRuntime;
|
|
589
|
-
exports.installNetworkBlocker = installNetworkBlocker;
|
|
590
|
-
exports.parseCookie = parseCookie;
|
|
591
|
-
exports.resetConsentRuntime = resetConsentRuntime;
|
|
592
|
-
exports.resolveTranslations = resolveTranslations;
|
|
593
|
-
exports.serializeCookie = serializeCookie;
|
|
594
|
-
exports.uninstallNetworkBlocker = uninstallNetworkBlocker;
|
|
1
|
+
"use strict";const e="cookieyes-consent",t=["necessary","functional","analytics","performance","advertisement"];function n(e){const t={},n=e.split(",");for(const e of n){const n=e.indexOf(":");if(-1===n)continue;const r=e.slice(0,n).trim(),s=e.slice(n+1).trim();(r in t||o.has(r))&&(t[r]=s)}return t}const o=new Set(["consentid","consent","action","necessary","functional","analytics","performance","advertisement","lastRenewedDate"]);function r(e){const n=[`consentid:${e.consentId}`,"consent:"+(e.hasActed?"yes":"no"),"action:"+(e.hasActed?"yes":"no")];for(const o of t)n.push(`${o}:${e.categories[o]?"yes":"no"}`);return n.push(`lastRenewedDate:${e.lastRenewed??Date.now()}`),n.join(",")}function s(t){if("undefined"==typeof document)return;const n=encodeURIComponent(r(t));document.cookie=`${e}=${n}; max-age=31536000; path=/; SameSite=Lax`}function a(){const e=new Uint8Array(32);if("undefined"!=typeof crypto&&crypto.getRandomValues)crypto.getRandomValues(e);else for(let t=0;t<e.length;t++)e[t]=Math.floor(256*Math.random());return btoa(String.fromCharCode(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"").slice(0,44)}function c(e,t){const n="CCPA"===t;return{consentId:e,hasActed:!1,categories:{necessary:!0,functional:n,analytics:n,performance:n,advertisement:n},regulation:t}}const i={bannerTitle:"We value your privacy",bannerDescription:"We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking “Accept All”, you consent to our use of cookies.",acceptAll:"Accept All",rejectAll:"Reject All",managePreferences:"Customise",savePreferences:"Save My Preferences",doNotSell:"Do Not Sell or Share My Personal Information",ccpaDescription:"This website or its third-party tools process personal data. You can opt out of the sale of your personal information by clicking on the “Do Not Sell or Share My Personal Information” link.",accept:"Accept",poweredBy:"Powered by CookieYes",preferencesTitle:"Customise Consent Preferences",preferencesIntro:"We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.",categories:{necessary:{label:"Necessary",description:"Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data."},functional:{label:"Functional",description:"Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features."},analytics:{label:"Analytics",description:"Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc."},performance:{label:"Performance",description:"Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors."},advertisement:{label:"Advertisement",description:"Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns."}},optOut:{title:"Opt-out Preferences",description:'We use third-party cookies that help us analyse how you use this website, store your preferences, and provide the content and advertisements that are relevant to you. However, you can opt out of these cookies by checking "Do Not Sell or Share My Personal Information" and clicking the "Save My Preferences" button. Once you opt out, you can opt in again at any time by unchecking "Do Not Sell or Share My Personal Information" and clicking the "Save My Preferences" button.',cancel:"Cancel",successText:"Your opt-out preference has been honored.",successCountdown:"Banner closes automatically in {seconds} s..."}};const l=new Map,d=new Set,u=new Map;function f(e){if("undefined"!=typeof document)for(const[t,n]of l){const o=!0===e[n.category],r=n.strategy??"afterConsent";if(o){if("lazyOnce"===r&&d.has(t))continue;u.has(t)||p(t,n)}else h(t)}}function p(e,t){if(document.getElementById(e))return;const n=document.createElement("script");n.id=e,n.src=t.src,n.async=!0,t.onLoad&&n.addEventListener("load",t.onLoad,{once:!0}),document.head.appendChild(n),u.set(e,n),d.add(e)}function h(e){const t=u.get(e);t&&(t.remove(),u.delete(e))}function y(e){return{consentId:e.consentId,categories:e.categories,regulation:e.regulation,domain:"undefined"!=typeof window?window.location.hostname:"unknown"}}const g=["necessary","functional","analytics","performance","advertisement"];function m(t){const o=new Set;let r,i,d=!1;const u=function(){if("undefined"==typeof document)return null;const t=document.cookie.split(";");for(const o of t){const t=o.trim(),r=t.indexOf("=");if(-1!==r&&t.slice(0,r).trim()===e){const e=t.slice(r+1).trim();return n(decodeURIComponent(e))}}return null}(),p=t.regulation??"DEFAULT";if(u)r=function(e,t){const n={necessary:!0,functional:"yes"===e.functional,analytics:"yes"===e.analytics,performance:"yes"===e.performance,advertisement:"yes"===e.advertisement};return{consentId:e.consentid??a(),hasActed:"yes"===e.action,categories:n,regulation:t,lastRenewed:e.lastRenewedDate?Number(e.lastRenewedDate):void 0}}(u,p);else{const e=a();r=c(e,p),"CCPA"===r.regulation&&s(r)}function h(){const e={consentId:r.consentId,hasActed:r.hasActed,categories:{...r.categories},regulation:r.regulation,lastRenewed:r.lastRenewed};for(const t of o)t(e);f(r.categories)}function m(){if(r={...r,hasActed:!0,lastRenewed:Date.now()},s(r),t.backend)try{Promise.resolve(t.backend.persist(y(r))).catch(()=>{})}catch{}else t.apiUrl&&async function(e,t,n){const o=y(n),r={"Content-Type":"application/json"};t&&(r.Authorization=`Bearer ${t}`);try{await fetch(e,{method:"POST",headers:r,body:JSON.stringify(o),keepalive:!0})}catch{}}(t.apiUrl,t.apiKey,r);let e=!1;for(const t of g)if(i[t]&&!r.categories[t]){e=!0;break}i={...r.categories},h(),t.onConsentUpdate?.(r),e&&t.reloadOnRevoke&&"undefined"!=typeof window&&window.location.reload()}i={...r.categories},Promise.resolve().then(()=>t.onConsentReady?.(r));const w={get consentId(){return r.consentId},get hasActed(){return r.hasActed},get categories(){return{...r.categories}},get regulation(){return r.regulation},get lastRenewed(){return r.lastRenewed},get isPreferencesOpen(){return d},acceptAll(){r={...r,categories:{necessary:!0,functional:!0,analytics:!0,performance:!0,advertisement:!0}},d=!1,m()},rejectAll(){r={...r,categories:{necessary:!0,functional:!1,analytics:!1,performance:!1,advertisement:!1}},d=!1,m()},acceptSelected(e){const t={...r.categories};for(const n of g)"necessary"!==n&&(t[n]=e.includes(n));r={...r,categories:t},d=!1,m()},updateCategory(e,t){"necessary"!==e&&(r={...r,categories:{...r.categories,[e]:t}},h())},savePreferences(){d=!1,m()},resetConsent(){"undefined"!=typeof document&&(document.cookie=`${e}=; max-age=0; path=/`);const t=a();r=c(t,r.regulation),d=!1,h()},showPreferences(){d=!0,h()},hidePreferences(){d=!1,h()},subscribe:e=>(o.add(e),()=>o.delete(e)),registerScript(e){!function(e){l.set(e.id,e)}(e),f(r.categories)}};return f(r.categories),w}function w(e,t,n,o){let r;try{const e="undefined"!=typeof window?window.location.href:"http://localhost";r=new URL(t,e)}catch{return null}const s=r.hostname.toLowerCase(),a=r.pathname+r.search,c=n.toUpperCase();for(const t of e){const e=t.domain.toLowerCase();if((s===e||s.endsWith("."+e))&&((!t.pathIncludes||a.includes(t.pathIncludes))&&(!t.methods||t.methods.map(e=>e.toUpperCase()).includes(c))&&!o(t.category)))return t}return null}let k=null;function v(e,t){if("undefined"==typeof window)return()=>{};if(k)return()=>{};if(!e.rules.length)return()=>{};const n={originalFetch:window.fetch,originalXhrOpen:XMLHttpRequest.prototype.open,originalXhrSend:XMLHttpRequest.prototype.send};k=n;const o=!1!==e.logBlockedRequests;function r(t){o&&console.warn(`[cookieyes] blocked ${t.method} ${t.url} (rule "${t.rule.id}", category: ${t.rule.category})`),e.onRequestBlocked?.(t)}return window.fetch=function(o,s){let a="",c=s?.method??"GET";"string"==typeof o?a=o:o instanceof URL?a=o.toString():(a=o.url,c=s?.method??o.method);const i=w(e.rules,a,c,t);return i?(r({rule:i,url:a,method:c}),Promise.reject(new TypeError(`Blocked by consent (rule: ${i.id}, category: ${i.category})`))):n.originalFetch.call(window,o,s)},XMLHttpRequest.prototype.open=function(e,t,...o){return this._cyUrl=t.toString(),this._cyMethod=e,n.originalXhrOpen.apply(this,[e,t,...o])},XMLHttpRequest.prototype.send=function(o){const s=this._cyUrl??"",a=this._cyMethod??"GET",c=w(e.rules,s,a,t);return c?(r({rule:c,url:s,method:a}),void this.abort()):n.originalXhrSend.call(this,o)},b}function b(){k&&("undefined"!=typeof window&&(window.fetch=k.originalFetch,XMLHttpRequest.prototype.open=k.originalXhrOpen,XMLHttpRequest.prototype.send=k.originalXhrSend),k=null)}let C=null;exports.createConsentManager=m,exports.defaultTranslations=i,exports.generateConsentId=a,exports.getOrCreateConsentRuntime=function(e){if(C)return C;const t=new Set,n=e.onConsentUpdate,o={};"self-hosted"===e.mode&&(e.backend?o.backend=e.backend:e.backendURL&&(o.apiUrl=e.backendURL)),e.apiKey&&(o.apiKey=e.apiKey),e.overrides?.regulation&&(o.regulation=e.overrides.regulation),e.colorScheme&&(o.colorScheme=e.colorScheme),e.theme&&(o.theme=e.theme),e.reloadOnRevoke&&(o.reloadOnRevoke=e.reloadOnRevoke),e.onConsentReady&&(o.onConsentReady=e.onConsentReady),o.onConsentUpdate=e=>{n?.(e);const o=function(e){const t=[],n=[];for(const o of Object.keys(e))e[o]?t.push(o):n.push(o);return{allowedCategories:t,deniedCategories:n}}(e.categories);for(const e of t)e(o)};const r=m(o);function s(){const e=r.categories;return{consentId:r.consentId,hasActed:r.hasActed,categories:e,consents:e,regulation:r.regulation,lastRenewed:r.lastRenewed,activeUI:r.isPreferencesOpen?"dialog":r.hasActed?null:"banner",has:e=>r.categories[e]??!1,saveConsents:async e=>{"all"===e?r.acceptAll():"necessary"===e?r.rejectAll():r.acceptSelected(e)},setConsent:(e,t)=>r.updateCategory(e,t),subscribeToConsentChanges:e=>(t.add(e),()=>{t.delete(e)})}}const a={subscribe:e=>r.subscribe(()=>e(s())),getState:s};return e.networkBlocker&&e.networkBlocker.rules.length>0&&v(e.networkBlocker,e=>!0===r.categories[e]),C={consentManager:r,consentStore:a},C},exports.installNetworkBlocker=v,exports.parseCookie=n,exports.resetConsentRuntime=function(){C=null},exports.resolveTranslations=function(e){const t=e?.messages??{},n=e?.detectBrowserLanguage??!0,o=[];e?.locale&&o.push(e.locale),n&&"undefined"!=typeof navigator&&navigator.language&&o.push(navigator.language);for(const e of o){const n=e.split("-")[0]?.toLowerCase()??"",o=t[e]??(n?t[n]:void 0);if(o)return o}return t.en??i},exports.serializeCookie=r,exports.uninstallNetworkBlocker=b;
|
|
595
2
|
//# sourceMappingURL=index.cjs.map
|
|
596
|
-
//# sourceMappingURL=index.cjs.map
|