fix: avoid utterances initialization failure (#1234)

This issue exists since Chirpy `v6.0.0`
This commit is contained in:
Cotes Chung 2023-09-15 01:24:13 +08:00 committed by GitHub
parent bb589e8d31
commit b34661efd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,16 +8,17 @@
></script> ></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { (function () {
const origin = 'https://utteranc.es'; const origin = 'https://utteranc.es';
const iframe = 'iframe.utterances-frame'; const iframe = 'iframe.utterances-frame';
const lightTheme = 'github-light'; const lightTheme = 'github-light';
const darkTheme = 'github-dark'; const darkTheme = 'github-dark';
let initTheme = lightTheme; let initTheme = lightTheme;
const html = document.documentElement;
if ( if (
$('html[data-mode=dark]').length > 0 || (html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
($('html[data-mode]').length == 0 && window.matchMedia('(prefers-color-scheme: dark)').matches) (!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
) { ) {
initTheme = darkTheme; initTheme = darkTheme;
} }
@ -45,5 +46,5 @@
const utterances = document.querySelector(iframe).contentWindow; const utterances = document.querySelector(iframe).contentWindow;
utterances.postMessage(message, origin); utterances.postMessage(message, origin);
}); });
}); })();
</script> </script>