hugo-bearblog : avoid onclick inline script

This commit is contained in:
minoplhy 2024-03-14 12:10:38 +07:00
parent 2615349a7d
commit 0ee42d6223
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
4 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,3 @@
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localStorage.getItem('dark-mode') === null) {
localStorage.setItem('dark-mode', 'true');
}
function darkmode() { function darkmode() {
if (localStorage.getItem('dark-mode') === 'false') { if (localStorage.getItem('dark-mode') === 'false') {
darkmode_enable(); darkmode_enable();
@ -21,3 +17,7 @@ function darkmode_disable() {
document.body.classList.remove("dark-mode"); document.body.classList.remove("dark-mode");
localStorage.setItem('dark-mode', 'false'); localStorage.setItem('dark-mode', 'false');
} }
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localStorage.getItem('dark-mode') === null) {
localStorage.setItem('dark-mode', 'true');
}

View File

@ -0,0 +1 @@
document.getElementById("dark-mode-button").addEventListener("click", darkmode);

View File

@ -63,6 +63,15 @@
Simply place a custom_body.html into Simply place a custom_body.html into
your local /layouts/partials-directory --> your local /layouts/partials-directory -->
{{- partial "custom_body.html" . -}} {{- partial "custom_body.html" . -}}
{{- /* Onclick Javascript */}}
{{- $darkmode_onclick := (resources.Get "js/darkmode_click.js") | resources.Minify }}
{{- if not site.Params.assets.disableFingerprinting }}
{{- $darkmode_onclick := $darkmode_onclick | fingerprint }}
<script crossorigin="anonymous" src="{{ $darkmode_onclick.RelPermalink }}" integrity="{{ $darkmode_onclick.Data.Integrity }}"></script>
{{ else }}
<script crossorigin="anonymous" src="{{ $darkmode_onclick.RelPermalink }}"></script>
{{ end }}
</body> </body>
</html> </html>

View File

@ -5,6 +5,6 @@
{{ with .Site.GetPage "/blog" }} {{ with .Site.GetPage "/blog" }}
<a href="{{ "blog" | relURL }}">Blog</a> <a href="{{ "blog" | relURL }}">Blog</a>
{{ end }} {{ end }}
<a id="dark-mode-button" onclick="darkmode()" > <a id="dark-mode-button">
<span>Toggle-Dark-Mode</span> <span>Toggle-Dark-Mode</span>
</a> </a>