Skip to content

Commit b9c0ad8

Browse files
committed
Clear invalidated cookies after page load
- add more analytics scripts to sandbox - rename storage key - add id to after scripts -
1 parent 53bac3d commit b9c0ad8

File tree

7 files changed

+73
-36
lines changed

7 files changed

+73
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ I didn't like sending 100KB+ for a simple cookie consent solution so I wrote thi
4747
biscuitman = {
4848
/* Default properties */
4949
50-
// storageKey: 'myconsent', // the key to store consent choices in localStorage
50+
// key: 'myconsent', // the key to store consent choices in localStorage
5151
// global: 'Consent', // global window object to access consents at runtime
5252
// force: false, // this forces the modal open at start and prevents website access without choosing consent
5353
// enableMore: true, // Hide multiple lines of info text behind a 'show more' link to keep UI short

biscuitman.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
((d, w, Object, bm)=>{
22
const defaults = {
3-
storageKey: 'myconsent',
3+
key: 'myconsent',
44
global: 'Consent',
55
force:false,
66
enableMore: true,
@@ -157,10 +157,10 @@
157157

158158
function readConsents() {
159159
try {
160-
return JSON.parse(localStorage.getItem(o.storageKey))
160+
return JSON.parse(localStorage.getItem(o.key))
161161
} catch (err) {
162162
console.error(err)
163-
localStorage.removeItem(o.storageKey)
163+
localStorage.removeItem(o.key)
164164
return {}
165165
}
166166
}
@@ -170,7 +170,10 @@
170170
const cookies = Object.fromEntries(
171171
d.cookie.split('; ').map(cookie => cookie.split('='))
172172
)
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+
}, {})
174177

175178
for (let [section, sectionConsent] of Object.entries(consents)) {
176179
if (sectionConsent) continue
@@ -209,7 +212,7 @@
209212
w[o.global][section] = sectionConsent
210213
if (!willReadValues) sectionElement.checked = value
211214
})
212-
localStorage.setItem(o.storageKey, JSON.stringify(w[o.global]))
215+
localStorage.setItem(o.key, JSON.stringify(w[o.global]))
213216
dispatch('save', {data: w[o.global]})
214217
clearStorages()
215218
insertScripts()
@@ -234,16 +237,15 @@
234237

235238
// If tag has src AND tag content, inject new tag adjacent to parent after load
236239
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})
241245
})
242246
});
243247
}
244248

245-
246-
247249
/* Start */
248250

249251
w[o.global] = readConsents() || {}
@@ -259,6 +261,9 @@
259261
// Render UI
260262
render()
261263

264+
// Wipe matching cookies without consent
265+
clearStorages()
266+
262267
// Consent logic
263268
if (w[o.global].consentTime) {
264269
displayUI(false)
@@ -270,7 +275,7 @@
270275
w.bmInvalidate = () => {
271276
dispatch('invalidate', {data: readConsents()})
272277
saveConsents(false)
273-
localStorage.removeItem(o.storageKey)
278+
localStorage.removeItem(o.key)
274279
displayUI(true)
275280
}
276281
// <a onclick="bmUpdate()" href="javascript:void(0)">Update Consent Preferences</a>

dist/biscuitman.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)