mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-29 20:13:31 +08:00
auto set theme
This commit is contained in:
parent
2be9c55b5b
commit
ee2a0a201e
@ -8,28 +8,38 @@
|
|||||||
data-mapping="{{- default "title" .mapping -}}"
|
data-mapping="{{- default "title" .mapping -}}"
|
||||||
data-reactions-enabled="1"
|
data-reactions-enabled="1"
|
||||||
data-emit-metadata="0"
|
data-emit-metadata="0"
|
||||||
data-theme="{{- .lightTheme -}}"
|
data-theme="{{- default "light" .lightTheme -}}"
|
||||||
crossorigin="anonymous"
|
crossorigin="anonymous"
|
||||||
async
|
async
|
||||||
></script>
|
></script>
|
||||||
<script>
|
<script>
|
||||||
const light = "{{- .lightTheme -}}";
|
const light = '{{- default "light" .lightTheme -}}';
|
||||||
const dark = "{{- .darkTheme -}}";
|
const dark = '{{- default "dark_dimmed" .darkTheme -}}';
|
||||||
const head = document.getElementsByTagName("html")[0];
|
const head = document.getElementsByTagName("html")[0];
|
||||||
|
|
||||||
function changeTheme(theme) {
|
function setGiscusTheme(theme) {
|
||||||
const iframe = document.querySelector('iframe.giscus-frame');
|
let giscus = document.querySelector('iframe.giscus-frame');
|
||||||
iframe.contentWindow.postMessage(
|
if (giscus) {
|
||||||
{ giscus: { setConfig: { theme: theme } } },
|
giscus.contentWindow.postMessage(
|
||||||
"https://giscus.app"
|
{ giscus: { setConfig: { theme: theme } } },
|
||||||
);
|
"https://giscus.app"
|
||||||
}
|
);
|
||||||
|
|
||||||
window.onload = function() {
|
|
||||||
if (head.getAttribute("data-scheme") == "dark") {
|
|
||||||
console.log("dark");
|
|
||||||
changeTheme(dark);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addEventListener('message', event => {
|
||||||
|
if (event.origin !== 'https://giscus.app') return;
|
||||||
|
if (head.getAttribute("data-scheme") == "dark") {
|
||||||
|
setGiscusTheme(dark);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
window.addEventListener('onColorSchemeChange', event => {
|
||||||
|
if (head.getAttribute("data-scheme") == "light") {
|
||||||
|
setGiscusTheme(light);
|
||||||
|
} else {
|
||||||
|
setGiscusTheme(dark);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
{{- end -}}
|
{{- end -}}
|
Loading…
Reference in New Issue
Block a user