|
1 | 1 | ((d, w, Object, bm)=>{ |
2 | 2 | const defaults = { |
3 | | - storageKey: 'myconsent', |
| 3 | + key: 'myconsent', |
4 | 4 | global: 'Consent', |
5 | 5 | force:false, |
6 | 6 | enableMore: true, |
|
157 | 157 |
|
158 | 158 | function readConsents() { |
159 | 159 | try { |
160 | | - return JSON.parse(localStorage.getItem(o.storageKey)) |
| 160 | + return JSON.parse(localStorage.getItem(o.key)) |
161 | 161 | } catch (err) { |
162 | 162 | console.error(err) |
163 | | - localStorage.removeItem(o.storageKey) |
| 163 | + localStorage.removeItem(o.key) |
164 | 164 | return {} |
165 | 165 | } |
166 | 166 | } |
|
170 | 170 | const cookies = Object.fromEntries( |
171 | 171 | d.cookie.split('; ').map(cookie => cookie.split('=')) |
172 | 172 | ) |
173 | | - const { consentTime, ...consents } = readConsents() |
| 173 | + const { consentTime, ...consents } = readConsents() || o.sections.slice(1).reduce((consents, section) => { |
| 174 | + consents[section] = false; |
| 175 | + return { consentTime: undefined, ...consents } |
| 176 | + }, {}) |
174 | 177 |
|
175 | 178 | for (let [section, sectionConsent] of Object.entries(consents)) { |
176 | 179 | if (sectionConsent) continue |
|
209 | 212 | w[o.global][section] = sectionConsent |
210 | 213 | if (!willReadValues) sectionElement.checked = value |
211 | 214 | }) |
212 | | - localStorage.setItem(o.storageKey, JSON.stringify(w[o.global])) |
| 215 | + localStorage.setItem(o.key, JSON.stringify(w[o.global])) |
213 | 216 | dispatch('save', {data: w[o.global]}) |
214 | 217 | clearStorages() |
215 | 218 | insertScripts() |
|
234 | 237 |
|
235 | 238 | // If tag has src AND tag content, inject new tag adjacent to parent after load |
236 | 239 | if (script.src && script.textContent.trim() !== '') newScript.addEventListener('load', () => { |
237 | | - let depScript = d.createElement('script') |
238 | | - depScript.textContent = script.textContent |
239 | | - newScript.insertAdjacentElement('afterend', depScript) |
240 | | - dispatch('inject', {el: depScript, parent: script}) |
| 240 | + let afterScript = d.createElement('script') |
| 241 | + afterScript.textContent = script.textContent |
| 242 | + if (script.id) afterScript.id = script.id + '-after' |
| 243 | + newScript.insertAdjacentElement('afterend', afterScript) |
| 244 | + dispatch('inject', {el: afterScript, parent: script}) |
241 | 245 | }) |
242 | 246 | }); |
243 | 247 | } |
244 | 248 |
|
245 | | - |
246 | | - |
247 | 249 | /* Start */ |
248 | 250 |
|
249 | 251 | w[o.global] = readConsents() || {} |
|
259 | 261 | // Render UI |
260 | 262 | render() |
261 | 263 |
|
| 264 | + // Wipe matching cookies without consent |
| 265 | + clearStorages() |
| 266 | + |
262 | 267 | // Consent logic |
263 | 268 | if (w[o.global].consentTime) { |
264 | 269 | displayUI(false) |
|
270 | 275 | w.bmInvalidate = () => { |
271 | 276 | dispatch('invalidate', {data: readConsents()}) |
272 | 277 | saveConsents(false) |
273 | | - localStorage.removeItem(o.storageKey) |
| 278 | + localStorage.removeItem(o.key) |
274 | 279 | displayUI(true) |
275 | 280 | } |
276 | 281 | // <a onclick="bmUpdate()" href="javascript:void(0)">Update Consent Preferences</a> |
|
0 commit comments