1- 'use strict'
1+ 'use strict' ;
22
33class CookieConsent {
4- constructor ( cookieBanner , config = {
5- cookieExpiryDays : 90
6- } ) {
4+ constructor ( cookieBanner , config = { cookieExpiryDays : 90 } ) {
75 this . cookieBanner = cookieBanner ;
8- this . acceptAllButton = this . cookieBanner . querySelectorAll ( '[wt-cookieconsent-element="accept-all"]' ) ;
6+ this . acceptAllButtons = this . cookieBanner . querySelectorAll ( '[wt-cookieconsent-element="accept-all"]' ) ;
97 this . acceptNecessaryButton = this . cookieBanner . querySelector ( '[wt-cookieconsent-element="accept-necessary"]' ) ;
108 this . manageCookiesButton = document . querySelector ( '[wt-cookieconsent-element="manage-cookies"]' ) ;
9+ this . categoriesForm = this . cookieBanner . querySelector ( '[wt-cookieconsent-element="categories-form"]' ) ;
10+
1111 this . cookieExpiryDays = config . cookieExpiryDays ;
1212 this . consentGiven = false ;
13+
1314 this . initialize ( ) ;
1415 }
1516
@@ -29,15 +30,22 @@ class CookieConsent {
2930 }
3031
3132 addEventListeners ( ) {
32- if ( this . acceptAllButton && this . acceptAllButton . length ) {
33- for ( let acceptBtn of this . acceptAllButton ) {
34- acceptBtn . addEventListener ( 'click' , ( ) => this . acceptAllCookies ( ) ) ;
35- }
33+ if ( this . acceptAllButtons && this . acceptAllButtons . length ) {
34+ this . acceptAllButtons . forEach ( btn => {
35+ btn . addEventListener ( 'click' , ( ) => this . acceptAllCookies ( ) ) ;
36+ } ) ;
3637 }
3738
38- if ( this . acceptNecessaryButton ) {
39+ if ( this . acceptNecessaryButton ) {
3940 this . acceptNecessaryButton . addEventListener ( 'click' , ( ) => this . acceptNecessaryCookies ( ) ) ;
4041 }
42+
43+ if ( this . categoriesForm ) {
44+ this . categoriesForm . addEventListener ( 'submit' , ( e ) => {
45+ e . preventDefault ( ) ;
46+ this . handleCategoryFormSubmit ( ) ;
47+ } ) ;
48+ }
4149 }
4250
4351 acceptAllCookies ( ) {
@@ -54,8 +62,29 @@ class CookieConsent {
5462 this . loadConsentScripts ( ) ;
5563 }
5664
65+ handleCategoryFormSubmit ( ) {
66+ let chosenCategories = [ ] ;
67+ const checkboxes = this . categoriesForm . querySelectorAll ( '[wt-cookieconsent-category]' ) ;
68+
69+ checkboxes . forEach ( checkbox => {
70+ if ( checkbox . checked ) {
71+ const category = checkbox . getAttribute ( 'wt-cookieconsent-category' ) ;
72+ chosenCategories . push ( category ) ;
73+ }
74+ } ) ;
75+
76+ if ( ! chosenCategories . includes ( 'necessary' ) ) {
77+ chosenCategories . unshift ( 'necessary' ) ;
78+ }
79+
80+ const finalValue = chosenCategories . join ( ',' ) ;
81+ this . setCookie ( 'cookieConsent' , finalValue , this . cookieExpiryDays ) ;
82+ this . consentGiven = true ;
83+ this . cookieBanner . remove ( ) ;
84+ this . loadConsentScripts ( ) ;
85+ }
86+
5787 manageCookies ( ) {
58- // Logic to open a modal or menu to manage cookie preferences.
5988 this . cookieBanner . style . display = 'block' ;
6089 }
6190
@@ -69,50 +98,70 @@ class CookieConsent {
6998 getCookie ( name ) {
7099 const value = `; ${ document . cookie } ` ;
71100 const parts = value . split ( `; ${ name } =` ) ;
72- if ( parts . length === 2 ) return parts . pop ( ) . split ( ';' ) . shift ( ) ;
101+ if ( parts . length === 2 ) {
102+ return parts . pop ( ) . split ( ';' ) . shift ( ) ;
103+ }
73104 }
74105
75106 loadConsentScripts ( ) {
76- const consentStatus = this . getCookie ( 'cookieConsent' ) ;
107+ const consentValue = this . getCookie ( 'cookieConsent' ) || '' ;
108+
109+ let consentCategories = [ ] ;
110+ if ( consentValue === 'all' ) {
111+ consentCategories = [ 'all' ] ;
112+ } else {
113+ consentCategories = consentValue
114+ . split ( ',' )
115+ . map ( val => val . trim ( ) )
116+ . filter ( Boolean ) ;
117+ }
118+
77119 const consentScripts = document . querySelectorAll ( 'script[wt-cookieconsent-script]' ) ;
78- consentScripts . forEach ( script => {
79- const scriptType = script . getAttribute ( 'wt-cookieconsent-script' ) ;
80- if ( consentStatus === 'all' || ( consentStatus === 'necessary' && scriptType === 'necessary' ) ) {
81- if ( ! script . dataset . loaded ) {
82- if ( ! script . src ) {
83- // Inline script
84- const inlineScript = document . createElement ( 'script' ) ;
85- inlineScript . textContent = script . textContent ;
86- document . head . appendChild ( inlineScript ) ;
87- } else {
88- // External script
89- const externalScript = document . createElement ( 'script' ) ;
90- externalScript . src = script . src ;
91- externalScript . async = script . async ;
92- externalScript . defer = script . defer ;
93- document . head . appendChild ( externalScript ) ;
94- }
95- script . dataset . loaded = 'true' ; // Mark script as loaded
96- }
97- script . remove ( ) ; // Remove original script to prevent duplicate loading
120+ consentScripts . forEach ( originalScript => {
121+ const scriptCategory = originalScript . getAttribute ( 'wt-cookieconsent-script' ) ;
122+
123+ if (
124+ consentCategories . includes ( 'all' ) ||
125+ consentCategories . includes ( scriptCategory ) ||
126+ scriptCategory === 'necessary'
127+ ) {
128+ this . injectScript ( originalScript ) ;
98129 }
99130 } ) ;
100131 }
132+
133+ injectScript ( originalScript ) {
134+ if ( originalScript . dataset . loaded === 'true' ) return ;
135+
136+ const newScript = document . createElement ( 'script' ) ;
137+
138+ if ( ! originalScript . src ) {
139+ newScript . textContent = originalScript . textContent ;
140+ } else {
141+ newScript . src = originalScript . src ;
142+ newScript . async = originalScript . async ;
143+ newScript . defer = originalScript . defer ;
144+ }
145+
146+ newScript . type = 'text/javascript' ;
147+ document . head . appendChild ( newScript ) ;
148+
149+ originalScript . dataset . loaded = 'true' ;
150+ originalScript . remove ( ) ;
151+ }
101152}
102153
103154const InitializeCookieConsent = ( ) => {
104155 window . webtricks = window . webtricks || [ ] ;
105156 const cookieBanner = document . querySelector ( '[wt-cookieconsent-element="banner"]' ) ;
106157 if ( cookieBanner ) {
107158 let instance = new CookieConsent ( cookieBanner ) ;
108- window . webtricks . push ( {
109- 'CookieConsent' : instance
110- } ) ;
159+ window . webtricks . push ( { 'CookieConsent' : instance } ) ;
111160 }
112161} ;
113162
114163if ( / c o m p l e t e | i n t e r a c t i v e | l o a d e d / . test ( document . readyState ) ) {
115164 InitializeCookieConsent ( ) ;
116165} else {
117166 window . addEventListener ( 'DOMContentLoaded' , InitializeCookieConsent ) ;
118- }
167+ }
0 commit comments