Skip to content

Commit

Permalink
Clear invalidated cookies after page load
Browse files Browse the repository at this point in the history
- add more analytics scripts to sandbox
- rename storage key
- add id to after scripts
-
  • Loading branch information
replete committed Jun 22, 2024
1 parent 53bac3d commit b9c0ad8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ I didn't like sending 100KB+ for a simple cookie consent solution so I wrote thi
biscuitman = {
/* Default properties */
// storageKey: 'myconsent', // the key to store consent choices in localStorage
// key: 'myconsent', // the key to store consent choices in localStorage
// global: 'Consent', // global window object to access consents at runtime
// force: false, // this forces the modal open at start and prevents website access without choosing consent
// enableMore: true, // Hide multiple lines of info text behind a 'show more' link to keep UI short
Expand Down
29 changes: 17 additions & 12 deletions biscuitman.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((d, w, Object, bm)=>{
const defaults = {
storageKey: 'myconsent',
key: 'myconsent',
global: 'Consent',
force:false,
enableMore: true,
Expand Down Expand Up @@ -157,10 +157,10 @@

function readConsents() {
try {
return JSON.parse(localStorage.getItem(o.storageKey))
return JSON.parse(localStorage.getItem(o.key))
} catch (err) {
console.error(err)
localStorage.removeItem(o.storageKey)
localStorage.removeItem(o.key)
return {}
}
}
Expand All @@ -170,7 +170,10 @@
const cookies = Object.fromEntries(
d.cookie.split('; ').map(cookie => cookie.split('='))
)
const { consentTime, ...consents } = readConsents()
const { consentTime, ...consents } = readConsents() || o.sections.slice(1).reduce((consents, section) => {
consents[section] = false;
return { consentTime: undefined, ...consents }
}, {})

for (let [section, sectionConsent] of Object.entries(consents)) {
if (sectionConsent) continue
Expand Down Expand Up @@ -209,7 +212,7 @@
w[o.global][section] = sectionConsent
if (!willReadValues) sectionElement.checked = value
})
localStorage.setItem(o.storageKey, JSON.stringify(w[o.global]))
localStorage.setItem(o.key, JSON.stringify(w[o.global]))
dispatch('save', {data: w[o.global]})
clearStorages()
insertScripts()
Expand All @@ -234,16 +237,15 @@

// If tag has src AND tag content, inject new tag adjacent to parent after load
if (script.src && script.textContent.trim() !== '') newScript.addEventListener('load', () => {
let depScript = d.createElement('script')
depScript.textContent = script.textContent
newScript.insertAdjacentElement('afterend', depScript)
dispatch('inject', {el: depScript, parent: script})
let afterScript = d.createElement('script')
afterScript.textContent = script.textContent
if (script.id) afterScript.id = script.id + '-after'
newScript.insertAdjacentElement('afterend', afterScript)
dispatch('inject', {el: afterScript, parent: script})
})
});
}



/* Start */

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

// Wipe matching cookies without consent
clearStorages()

// Consent logic
if (w[o.global].consentTime) {
displayUI(false)
Expand All @@ -270,7 +275,7 @@
w.bmInvalidate = () => {
dispatch('invalidate', {data: readConsents()})
saveConsents(false)
localStorage.removeItem(o.storageKey)
localStorage.removeItem(o.key)
displayUI(true)
}
// <a onclick="bmUpdate()" href="javascript:void(0)">Update Consent Preferences</a>
Expand Down
2 changes: 1 addition & 1 deletion dist/biscuitman.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b9c0ad8

Please sign in to comment.