|
1 | | -((d, w, bm)=>{ |
| 1 | +((d, w, Object, bm)=>{ |
2 | 2 | const defaults = { |
3 | 3 | storageKey: 'myconsent', |
4 | 4 | global: 'Consent', |
|
122 | 122 | let id = e.target.dataset.id |
123 | 123 | dispatch('button', {id}) |
124 | 124 | switch (id) { |
125 | | - case 'accept': saveConsent(true); break; |
| 125 | + case 'accept': saveConsents(true); break; |
126 | 126 | case 'close': dialog.close(); break; |
127 | 127 | case 'settings': openModal(); break; |
128 | | - case 'save': saveConsent(); break; |
129 | | - case 'reject': saveConsent(false); break; |
| 128 | + case 'save': saveConsents(); break; |
| 129 | + case 'reject': saveConsents(false); break; |
130 | 130 | } |
131 | 131 | } |
132 | 132 |
|
|
153 | 153 | console.debug(name, payload); |
154 | 154 | } |
155 | 155 |
|
156 | | - /* Consents & Injection */ |
| 156 | + /* Data */ |
157 | 157 |
|
158 | | - function readConsent() { |
| 158 | + function readConsents() { |
159 | 159 | try { |
160 | 160 | return JSON.parse(localStorage.getItem(o.storageKey)) |
161 | 161 | } catch (err) { |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 |
|
168 | | - function saveConsent(value) { |
| 168 | + function clearStorages() { |
| 169 | + const localStores = Object.fromEntries(Object.entries(localStorage)) |
| 170 | + const cookies = Object.fromEntries( |
| 171 | + d.cookie.split('; ').map(cookie => cookie.split('=')) |
| 172 | + ) |
| 173 | + const { consentTime, ...consents } = readConsents() |
| 174 | + |
| 175 | + for (let [section, sectionConsent] of Object.entries(consents)) { |
| 176 | + if (sectionConsent) continue |
| 177 | + let sectionCookieNames = Object.keys(o[`${section}Cookies`] || {}) |
| 178 | + |
| 179 | + sectionCookieNames |
| 180 | + .filter(name => name.endsWith('*')) |
| 181 | + .map(wildcardName => { |
| 182 | + Object.keys({...cookies, ...localStores}).map(name => { |
| 183 | + if (name.startsWith(wildcardName.slice(0, -1))) sectionCookieNames.push(name) |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + for (const name of sectionCookieNames) { |
| 188 | + if (cookies[name]) { |
| 189 | + d.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;` |
| 190 | + dispatch('delete',{cookie : name}) |
| 191 | + } |
| 192 | + if (localStores[name]) { |
| 193 | + localStorage.removeItem(name) |
| 194 | + dispatch('delete',{localStorage : name}) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + function saveConsents(value) { |
169 | 201 | const willReadValues = value === undefined |
170 | 202 | w[o.global].consentTime = +new Date() |
171 | 203 | o.sections.forEach(section => { |
172 | 204 | if (section === 'essential') return false |
173 | 205 | let sectionElement = ui.querySelector(`[data-s=${section}]`) |
174 | | - w[o.global][section] = willReadValues |
| 206 | + let sectionConsent = willReadValues |
175 | 207 | ? sectionElement.checked |
176 | 208 | : value |
| 209 | + w[o.global][section] = sectionConsent |
177 | 210 | if (!willReadValues) sectionElement.checked = value |
178 | 211 | }) |
179 | 212 | localStorage.setItem(o.storageKey, JSON.stringify(w[o.global])) |
180 | 213 | dispatch('save', {data: w[o.global]}) |
| 214 | + clearStorages() |
181 | 215 | insertScripts() |
182 | 216 | dialog.close() |
183 | 217 | displayUI(false) |
|
208 | 242 | }); |
209 | 243 | } |
210 | 244 |
|
| 245 | + |
| 246 | + |
211 | 247 | /* Start */ |
212 | 248 |
|
213 | | - w[o.global] = readConsent() || {} |
| 249 | + w[o.global] = readConsents() || {} |
214 | 250 |
|
215 | 251 | // Optional Non-EU auto-consent |
216 | 252 | const tz = Intl.DateTimeFormat().resolvedOptions().timeZone |
217 | 253 | const isEuropeTimezone = /^(GMT|UTC)$/.test(tz) || /(Europe|BST|CEST|CET|EET|IST|WEST|WET|GMT-1|GMT-2|UTC+1|UTC+2|UTC+3)/.test(tz) |
218 | 254 | if (o.acceptNonEU && !isEuropeTimezone) { |
219 | | - saveConsent(true) |
| 255 | + saveConsents(true) |
220 | 256 | displayUI(false) |
221 | 257 | } |
222 | 258 |
|
|
232 | 268 | // Helper methods |
233 | 269 | // <a onclick="bmInvalidate()" href="javascript:void(0)">Delete Consent Preferences</a> |
234 | 270 | w.bmInvalidate = () => { |
235 | | - dispatch('invalidate', {data: readConsent()}) |
236 | | - saveConsent(false) |
| 271 | + dispatch('invalidate', {data: readConsents()}) |
| 272 | + saveConsents(false) |
237 | 273 | localStorage.removeItem(o.storageKey) |
238 | 274 | displayUI(true) |
239 | 275 | } |
240 | 276 | // <a onclick="bmUpdate()" href="javascript:void(0)">Update Consent Preferences</a> |
241 | 277 | w.bmUpdate = () => { |
242 | | - dispatch('update', {data: readConsent()}) |
| 278 | + dispatch('update', {data: readConsents()}) |
243 | 279 | openModal() |
244 | 280 | } |
245 | | -})(document, window, 'biscuitman') |
| 281 | +})(document, window, Object, 'biscuitman') |
0 commit comments