@nortic/citronkaka-service 0.1.3 → 0.2.0
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.d.ts +9 -7
- package/dist/index.es.js +19 -11
- package/dist/index.iife.js +1 -1
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,15 @@ declare class CookieConsent {
|
|
|
14
14
|
private cookieInterceptor;
|
|
15
15
|
private scriptInterceptor;
|
|
16
16
|
constructor(config?: CookieConsentConfig, listeners?: Record<CookieConsentEvent, CookieConsentEventListener[]>, persistentService?: typeof CookieService, cookieCleaner?: typeof CookieCleaner, cookieInterceptor?: typeof CookieInterceptor, scriptInterceptor?: typeof ScriptInterceptor);
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the CookieConsent instance with the provided configuration.
|
|
19
|
+
* This method merges the provided configuration with the default configuration
|
|
20
|
+
* and re-runs the cookie and script cleaning/interception processes based on the updated configuration.
|
|
21
|
+
*
|
|
22
|
+
* @param config - A partial configuration object to override the default configuration.
|
|
23
|
+
* This allows customization of the cookie consent behavior.
|
|
24
|
+
*/
|
|
25
|
+
init(config?: Partial<CookieConsentConfig>): void;
|
|
17
26
|
/**
|
|
18
27
|
* Retrieves the current cookie consent model from persistent storage.
|
|
19
28
|
* If the stored value is invalid, it falls back to the default model.
|
|
@@ -147,10 +156,3 @@ declare class ScriptInterceptor {
|
|
|
147
156
|
}
|
|
148
157
|
|
|
149
158
|
export { }
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
declare global {
|
|
153
|
-
interface Window {
|
|
154
|
-
Citronkaka: InstanceType<typeof CookieConsent>;
|
|
155
|
-
}
|
|
156
|
-
}
|
package/dist/index.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
function f() {
|
|
2
2
|
return location.hostname.split(".").slice(-2).join(".");
|
|
3
3
|
}
|
|
4
|
-
const
|
|
4
|
+
const h = {
|
|
5
5
|
domain: f(),
|
|
6
6
|
defaultModel: {
|
|
7
7
|
useraction: !1,
|
|
@@ -10,10 +10,7 @@ const m = {
|
|
|
10
10
|
persistentKey: "nortic_cookie_consent",
|
|
11
11
|
persistentExpirationDays: 365,
|
|
12
12
|
categories: ["necessary", "functional", "analytics", "marketing"],
|
|
13
|
-
services: [
|
|
14
|
-
{ name: "Translations", category: "functional", scripts: [], cookies: [/^i18n_redirected/] },
|
|
15
|
-
{ name: "Color mode", category: "functional", scripts: [], cookies: [/^color-mode/] }
|
|
16
|
-
]
|
|
13
|
+
services: []
|
|
17
14
|
};
|
|
18
15
|
class p {
|
|
19
16
|
static createCookieString(e, t, o = {}) {
|
|
@@ -76,7 +73,7 @@ class k {
|
|
|
76
73
|
});
|
|
77
74
|
}
|
|
78
75
|
}
|
|
79
|
-
class
|
|
76
|
+
class m {
|
|
80
77
|
/**
|
|
81
78
|
* Starts the cookie interception process to block cookies based on a blacklist.
|
|
82
79
|
* @param blacklist An array of RegExp patterns for cookie names to block.
|
|
@@ -188,10 +185,21 @@ class l {
|
|
|
188
185
|
});
|
|
189
186
|
}
|
|
190
187
|
}
|
|
191
|
-
class
|
|
192
|
-
constructor(e =
|
|
188
|
+
class g {
|
|
189
|
+
constructor(e = h, t = { update: [] }, o = p, s = k, r = m, i = l) {
|
|
193
190
|
this.config = e, this.listeners = t, this.persistentService = o, this.cookieCleaner = s, this.cookieInterceptor = r, this.scriptInterceptor = i, s.run(this.blacklist.cookies), r.run(this.blacklist.cookies), i.run(this.blacklist.scripts);
|
|
194
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Initializes the CookieConsent instance with the provided configuration.
|
|
194
|
+
* This method merges the provided configuration with the default configuration
|
|
195
|
+
* and re-runs the cookie and script cleaning/interception processes based on the updated configuration.
|
|
196
|
+
*
|
|
197
|
+
* @param config - A partial configuration object to override the default configuration.
|
|
198
|
+
* This allows customization of the cookie consent behavior.
|
|
199
|
+
*/
|
|
200
|
+
init(e = {}) {
|
|
201
|
+
this.config = { ...this.config, ...e }, this.cookieCleaner.run(this.blacklist.cookies), this.cookieInterceptor.run(this.blacklist.cookies), this.scriptInterceptor.run(this.blacklist.scripts);
|
|
202
|
+
}
|
|
195
203
|
/**
|
|
196
204
|
* Retrieves the current cookie consent model from persistent storage.
|
|
197
205
|
* If the stored value is invalid, it falls back to the default model.
|
|
@@ -265,8 +273,8 @@ class y {
|
|
|
265
273
|
return o.necessary === !0 && typeof o.functional == "boolean" && typeof o.analytics == "boolean" && typeof o.marketing == "boolean";
|
|
266
274
|
}
|
|
267
275
|
}
|
|
268
|
-
window.Citronkaka = window.Citronkaka || new
|
|
269
|
-
const
|
|
276
|
+
window.Citronkaka = window.Citronkaka || new g();
|
|
277
|
+
const y = window.Citronkaka;
|
|
270
278
|
export {
|
|
271
|
-
|
|
279
|
+
y as default
|
|
272
280
|
};
|
package/dist/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var Citronkaka=function(){"use strict";function u(){return location.hostname.split(".").slice(-2).join(".")}const d={domain:u(),defaultModel:{useraction:!1,value:{necessary:!0,functional:!1,analytics:!1,marketing:!1}},persistentKey:"nortic_cookie_consent",persistentExpirationDays:365,categories:["necessary","functional","analytics","marketing"],services:[
|
|
1
|
+
var Citronkaka=function(){"use strict";function u(){return location.hostname.split(".").slice(-2).join(".")}const d={domain:u(),defaultModel:{useraction:!1,value:{necessary:!0,functional:!1,analytics:!1,marketing:!1}},persistentKey:"nortic_cookie_consent",persistentExpirationDays:365,categories:["necessary","functional","analytics","marketing"],services:[]};class p{static createCookieString(e,t,o={}){const{expires:s,path:r="/",sameSite:i="Strict",domain:n}=o,c=encodeURIComponent(t),m=s?`; expires=${s.toUTCString()}`:"",g=n?`; domain=${n}`:"";return`${e}=${c}${m}; path=${r}; SameSite=${i}${g}`}static set(e,t,o,s){const r=typeof t=="string"?t:JSON.stringify(t),i=new Date(Date.now()+o*24*60*60*1e3);document.cookie=this.createCookieString(e,r,{expires:i,domain:s})}static get(e){const t=document.cookie.match(new RegExp(`(^| )${e}=([^;]+)`));if(!t)return null;const o=decodeURIComponent(t[2]);try{return JSON.parse(o)}catch{return o}}static delete(e,t){document.cookie=this.createCookieString(e,"",{expires:new Date(0),domain:t})}}class f{static run(e,t=p){function o(s){document.cookie.split(";").forEach(r=>{const i=r.split("=")[0].trim();s.test(i)&&(t.delete(i),console.warn(`Removed cookie: ${i}`))})}e.forEach(s=>{o(s)})}}class k{static run(e){const t=Object.getOwnPropertyDescriptor(Document.prototype,"cookie");if(!t){console.error("Failed to retrieve the cookie property descriptor.");return}Object.defineProperty(document,"cookie",{configurable:!0,enumerable:!0,get(){var o;return((o=t.get)==null?void 0:o.call(document))||""},set(o){var n;const s=o.split("=")[0].trim(),r=o.match(/expires=([^;]+)/i);(r?new Date(r[1]).getTime()<Date.now():!1)||!e.some(c=>c.test(s))?(n=t.set)==null||n.call(document,o):console.warn(`Blocked cookie: ${s}`)}})}}class l{static run(e){this.interceptScriptCreation(e),this.observeDynamicScripts(e)}static shouldBlockScript(e,t){return e.some(o=>o.test(t))}static interceptScriptCreation(e){const t=document.createElement.bind(document);document.createElement=function(o){const s=t(o);if(o.toLowerCase()==="script"){const r=s,i=Object.getOwnPropertyDescriptor(HTMLScriptElement.prototype,"src");i&&Object.defineProperty(r,"src",{configurable:!0,enumerable:!0,get(){var n;return(n=i.get)==null?void 0:n.call(r)},set(n){var c;if(l.shouldBlockScript(e,n)){console.warn(`Blocked script: ${n}`),r.type="javascript/blocked";return}(c=i.set)==null||c.call(r,n)}})}return s}}static observeDynamicScripts(e){new MutationObserver(o=>{o.forEach(({addedNodes:s})=>{s.forEach(r=>{if(r.nodeType===Node.ELEMENT_NODE&&r.tagName==="SCRIPT"){const i=r;if(i.src&&this.shouldBlockScript(e,i.src)){console.warn(`Blocked dynamically added script: ${i.src}`),i.type="javascript/blocked";const n=c=>{i.getAttribute("type")==="javascript/blocked"&&(c.preventDefault(),console.warn(`Prevented execution of blocked script: ${i.src}`)),i.removeEventListener("beforescriptexecute",n)};i.addEventListener("beforescriptexecute",n)}}})})}).observe(document.documentElement,{childList:!0,subtree:!0})}}class h{constructor(e=d,t={update:[]},o=p,s=f,r=k,i=l){this.config=e,this.listeners=t,this.persistentService=o,this.cookieCleaner=s,this.cookieInterceptor=r,this.scriptInterceptor=i,s.run(this.blacklist.cookies),r.run(this.blacklist.cookies),i.run(this.blacklist.scripts)}init(e={}){this.config={...this.config,...e},this.cookieCleaner.run(this.blacklist.cookies),this.cookieInterceptor.run(this.blacklist.cookies),this.scriptInterceptor.run(this.blacklist.scripts)}get model(){const e=this.persistentService.get(this.config.persistentKey);return this.isCookieConsentModel(e)?e:this.config.defaultModel}set model(e){if(!this.isCookieConsentModel(e))throw new Error("Invalid model: The provided value is not a valid CookieConsentModel.");this.persistentService.set(this.config.persistentKey,e,this.config.persistentExpirationDays,this.config.domain)}update(e){this.model={useraction:!0,value:e},this.notifyListeners("update")}on(e,t){this.listeners[e].push(t)}notifyListeners(e){this.listeners[e].forEach(t=>t(this.model))}get blacklist(){return this.config.services.reduce((e,t)=>{if(this.model.value[t.category]===!1){const{cookies:o,scripts:s}=t;e.cookies=e.cookies.concat(o),e.scripts=e.scripts.concat(s)}return e},{cookies:[],scripts:[]})}isCookieConsentModel(e){if(typeof e!="object"||e===null)return!1;const t=e;if(typeof t.useraction!="boolean"||typeof t.value!="object"||t.value===null)return!1;const o=t.value;return o.necessary===!0&&typeof o.functional=="boolean"&&typeof o.analytics=="boolean"&&typeof o.marketing=="boolean"}}return window.Citronkaka=window.Citronkaka||new h,window.Citronkaka}();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nortic/citronkaka-service",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"registry": "https://registry.npmjs.org/",
|
|
@@ -21,16 +21,16 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@tsconfig/node20": "^20.1.
|
|
24
|
+
"@tsconfig/node20": "^20.1.5",
|
|
25
25
|
"@types/jsdom": "^21.1.7",
|
|
26
|
-
"@types/node": "^
|
|
26
|
+
"@types/node": "^22.13.14",
|
|
27
27
|
"@vue/tsconfig": "^0.7.0",
|
|
28
|
-
"jsdom": "^
|
|
29
|
-
"npm-run-all2": "^
|
|
30
|
-
"typescript": "~5.
|
|
31
|
-
"vite": "^
|
|
32
|
-
"vite-plugin-dts": "^4.3
|
|
33
|
-
"vitest": "^
|
|
28
|
+
"jsdom": "^26.0.0",
|
|
29
|
+
"npm-run-all2": "^7.0.2",
|
|
30
|
+
"typescript": "~5.8.2",
|
|
31
|
+
"vite": "^6.2.4",
|
|
32
|
+
"vite-plugin-dts": "^4.5.3",
|
|
33
|
+
"vitest": "^3.1.1"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"dev": "vite build --watch",
|