From fe2e3ba5f4ef7768fcd8debfd33a36527e98b038 Mon Sep 17 00:00:00 2001 From: Mark Dumay <61946753+markdumay@users.noreply.github.com> Date: Fri, 20 Sep 2024 06:30:19 +0200 Subject: [PATCH] Test cky integration --- config/_default/params.toml | 2 +- exampleSite/assets/js/critical/_cookie.js | 48 ++++++++++++++++++++++ exampleSite/assets/js/critical/_cookie2.js | 35 ++++++++++++++++ exampleSite/config/_default/params.toml | 2 +- exampleSite/config/_default/server.toml | 8 ++-- netlify.toml | 8 ++-- 6 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 exampleSite/assets/js/critical/_cookie.js create mode 100644 exampleSite/assets/js/critical/_cookie2.js diff --git a/config/_default/params.toml b/config/_default/params.toml index 9d061961..41df2bad 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -41,7 +41,7 @@ # toml-docs-start debugging [debugging] - showJS = false + showJS = true showSCSS = false purgeHTMLComments = false includeSVGOrigin = true diff --git a/exampleSite/assets/js/critical/_cookie.js b/exampleSite/assets/js/critical/_cookie.js new file mode 100644 index 00000000..9a754987 --- /dev/null +++ b/exampleSite/assets/js/critical/_cookie.js @@ -0,0 +1,48 @@ +/* eslint-disable no-undef, no-unused-vars */ + +let _manager + +function loadScriptSync (src) { + const s = document.createElement('script') + s.src = src + s.type = 'text/javascript' + s.async = false + document.getElementsByTagName('head')[0].appendChild(s) +} + +function hasConsent (category) { + if (typeof _manager !== 'undefined' && _manager !== null) { + return _manager.hasConsent(category) + } else { + console.log('no mgr defined: ' + category) + return false + } +} + +function getLocalStorage (key, def, category) { + if (hasConsent(category)) { + return localStorage.getItem(key) + } else { + return def + } +} + +function setLocalStorage (key, val, category) { + if (hasConsent(category)) { + localStorage.setItem(key, val) + } +} + +function getSessionStorage (key, def, category) { + if (hasConsent(category)) { + return sessionStorage.getItem(key) + } else { + return def + } +} + +function setSessionStorage (key, val, category) { + if (hasConsent(category)) { + sessionStorage.setItem(key, val) + } +} diff --git a/exampleSite/assets/js/critical/_cookie2.js b/exampleSite/assets/js/critical/_cookie2.js new file mode 100644 index 00000000..5c17d0d5 --- /dev/null +++ b/exampleSite/assets/js/critical/_cookie2.js @@ -0,0 +1,35 @@ +// TODO: move to module + +/* eslint-disable no-undef, no-unused-vars */ +{{- if or site.Params.modules.cookieyes.force (not hugo.IsServer) -}} + {{ with site.Params.modules.cookieyes.id }} + +class CookieYesManager { + #consent + + updateConsent () { + this.#consent = getCkyConsent() + } + + constructor () { + loadScriptSync('https://cdn-cookieyes.com/client_data/{{ (. | urlize) }}/script.js') + this.updateConsent() + } + + hasConsent (category) { + if (typeof this.#consent !== 'undefined' && this.#consent !== null) { + console.log('invoke cky consent: ' + category + this.#consent) + return true + } else { + console.log('cky consent undefined: ' + category) + return false + } + } +} + +_manager = new CookieYesManager() + + {{ else }} + {{ warnf "Cannot find CookieYes ID, check 'params.modules.cookieyes.id'" }} + {{ end }} +{{ end }} \ No newline at end of file diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index eda86366..9010653a 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -15,7 +15,7 @@ transpiler = "dartsass" [debugging] - showJS = false + showJS = true showSCSS = false purgeHTMLComments = true # prevents a Goldmark warning when processing HTML comments (as used by markdownlint) includeSVGOrigin = true diff --git a/exampleSite/config/_default/server.toml b/exampleSite/config/_default/server.toml index cd91c062..80be797f 100644 --- a/exampleSite/config/_default/server.toml +++ b/exampleSite/config/_default/server.toml @@ -7,15 +7,15 @@ for = '/**' X-XSS-Protection = "1; mode=block" Content-Security-Policy = """\ default-src 'self'; \ - script-src 'self' https://*.google-analytics.com https://*.googletagmanager.com; \ - style-src 'self' https://fonts.googleapis.com https://www.youtube.com; \ + script-src 'self' cdn-cookieyes.com https://*.google-analytics.com https://*.googletagmanager.com; \ + style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://www.youtube.com; \ object-src 'none'; \ base-uri 'self'; \ - connect-src 'self' + connect-src 'self' *.cookieyes.com cdn-cookieyes.com \ https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; \ font-src 'self' https://fonts.gstatic.com; \ frame-src 'self' https://player.cloudinary.com https://player.vimeo.com https://www.youtube-nocookie.com https://www.youtube.com; \ - img-src 'self' data: https://*.imgix.net https://*.imagekit.io https://*.cloudinary.com https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \ + img-src 'self' data: cdn-cookieyes.com https://*.imgix.net https://*.imagekit.io https://*.cloudinary.com https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \ manifest-src 'self'; \ media-src 'self' \ """ diff --git a/netlify.toml b/netlify.toml index b76cf8db..bba1bbdb 100644 --- a/netlify.toml +++ b/netlify.toml @@ -21,19 +21,19 @@ Content-Security-Policy = """\ default-src 'self'; \ child-src 'self' app.netlify.com; \ - script-src 'self' \ + script-src 'self' cdn-cookieyes.com \ https://*.netlify.app app.netlify.com netlify-cdp-loader.netlify.app \ https://*.google-analytics.com https://*.googletagmanager.com; \ - style-src 'self' \ + style-src 'self' 'unsafe-inline' \ https://*.netlify.app https://fonts.googleapis.com https://www.youtube.com; \ object-src 'none'; \ base-uri 'self'; \ - connect-src 'self' + connect-src 'self' *.cookieyes.com cdn-cookieyes.com \ https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; \ font-src 'self' https://*.netlify.app https://fonts.gstatic.com; \ frame-src 'self' https://player.cloudinary.com https://player.vimeo.com https://www.youtube-nocookie.com https://www.youtube.com \ app.netlify.com; \ - img-src 'self' data: https://*.imgix.net https://*.imagekit.io https://*.cloudinary.com https://*.netlify.app https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \ + img-src 'self' data: cdn-cookieyes.com https://*.imgix.net https://*.imagekit.io https://*.cloudinary.com https://*.netlify.app https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \ manifest-src 'self'; \ media-src 'self' \ """