From b46dab5ba6a3979cafb04762ff7721524bc559f0 Mon Sep 17 00:00:00 2001 From: minoplhy Date: Sun, 10 Mar 2024 11:47:53 +0700 Subject: [PATCH] hugo-bearblog : loading javascript,stylesheet out of HTML page (no unsafe CSP) --- .../style.html => assets/css/style.css | 181 +++++++++--------- assets/js/darkmode.js | 23 +++ assets/js/darkmode_load.js | 5 + assets/js/random_color.js | 25 +++ layouts/_default/baseof.html | 14 +- layouts/_default/list.html | 5 +- layouts/partials/random_color.html | 27 --- layouts/partials/script.html | 25 --- layouts/partials/script_bottom.html | 5 - 9 files changed, 157 insertions(+), 153 deletions(-) rename layouts/partials/style.html => assets/css/style.css (59%) create mode 100644 assets/js/darkmode.js create mode 100644 assets/js/darkmode_load.js create mode 100644 assets/js/random_color.js delete mode 100644 layouts/partials/random_color.html delete mode 100644 layouts/partials/script.html delete mode 100644 layouts/partials/script_bottom.html diff --git a/layouts/partials/style.html b/assets/css/style.css similarity index 59% rename from layouts/partials/style.html rename to assets/css/style.css index 9e16279..75019fb 100644 --- a/layouts/partials/style.html +++ b/assets/css/style.css @@ -1,5 +1,4 @@ - +} \ No newline at end of file diff --git a/assets/js/darkmode.js b/assets/js/darkmode.js new file mode 100644 index 0000000..109db12 --- /dev/null +++ b/assets/js/darkmode.js @@ -0,0 +1,23 @@ +if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localStorage.getItem('dark-mode') === null) { + localStorage.setItem('dark-mode', 'true'); +} + +function darkmode() { + if (localStorage.getItem('dark-mode') === 'false') { + darkmode_enable(); + } else if (localStorage.getItem('dark-mode') === null) { + darkmode_enable(); + } else { + darkmode_disable(); + } +} + +function darkmode_enable() { + document.body.classList.add("dark-mode"); + localStorage.setItem('dark-mode', 'true'); +} + +function darkmode_disable() { + document.body.classList.remove("dark-mode"); + localStorage.setItem('dark-mode', 'false'); +} \ No newline at end of file diff --git a/assets/js/darkmode_load.js b/assets/js/darkmode_load.js new file mode 100644 index 0000000..cf35254 --- /dev/null +++ b/assets/js/darkmode_load.js @@ -0,0 +1,5 @@ +/* darkmode_enable is defined in darkmode.js(should be in the same directory as this file) */ +/* Must be executed after document.body is finished, else there would be error :) */ +if (localStorage.getItem('dark-mode') === 'true') { + darkmode_enable() +} \ No newline at end of file diff --git a/assets/js/random_color.js b/assets/js/random_color.js new file mode 100644 index 0000000..617defd --- /dev/null +++ b/assets/js/random_color.js @@ -0,0 +1,25 @@ +const links = document.querySelectorAll('.random-color'); + +// To avoid too bright and too dark color being generated +function getRandomColor(minBrightness = 100, maxBrightness = 230) { + // Generate random color components within a specific brightness range + let red = Math.floor(Math.random() * (maxBrightness - minBrightness) + minBrightness); + let green = Math.floor(Math.random() * (maxBrightness - minBrightness) + minBrightness); + let blue = Math.floor(Math.random() * (maxBrightness - minBrightness) + minBrightness); + + // Convert components to hexadecimal string format + red = red.toString(16).padStart(2, '0'); + green = green.toString(16).padStart(2, '0'); + blue = blue.toString(16).padStart(2, '0'); + + return `#${red}${green}${blue}`; +} + + +function setRandomColors() { + links.forEach(link => { + link.style.color = getRandomColor(); + }); +} + +window.onload = setRandomColors; \ No newline at end of file diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index d175160..2c3c000 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -15,9 +15,14 @@ {{ printf `` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }} {{ end -}} - {{- partial "style.html" . -}} - {{- partial "script.html" . -}} + {{- /* CSS stylesheet */}} + {{- $stylesheet := (resources.Get "css/style.css") | resources.Minify | fingerprint }} + + {{- /* Javascript */}} + {{- $darkmode_script := (resources.Get "js/darkmode.js") | resources.Minify | fingerprint}} + + @@ -35,10 +40,13 @@ {{- partial "footer.html" . -}} + {{- /* Bottom Javascript */}} + {{- $darkmode_load_script := (resources.Get "js/darkmode_load.js") | resources.Minify | fingerprint}} + + - {{- partial "script_bottom.html" -}} {{- partial "custom_body.html" . -}} diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 4243521..f1ad886 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -27,7 +27,10 @@ {{ end }} - {{- partial "random_color.html" -}} + {{- /* Random Color Script */}} + {{- $random_color_script := (resources.Get "js/random_color.js") | resources.Minify | fingerprint}} + + {{ if .Data.Singular }} {{else}} diff --git a/layouts/partials/random_color.html b/layouts/partials/random_color.html deleted file mode 100644 index 34b693d..0000000 --- a/layouts/partials/random_color.html +++ /dev/null @@ -1,27 +0,0 @@ - \ No newline at end of file diff --git a/layouts/partials/script.html b/layouts/partials/script.html deleted file mode 100644 index 9add0c5..0000000 --- a/layouts/partials/script.html +++ /dev/null @@ -1,25 +0,0 @@ - \ No newline at end of file diff --git a/layouts/partials/script_bottom.html b/layouts/partials/script_bottom.html deleted file mode 100644 index 7a2b18e..0000000 --- a/layouts/partials/script_bottom.html +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file