web/_includes/comments/utterances.html

51 lines
1.5 KiB
HTML
Raw Normal View History

2021-12-11 12:24:08 +03:00
<!-- https://utteranc.es/ -->
<script
src="https://utteranc.es/client.js"
repo="{{ site.comments.utterances.repo }}"
issue-term="{{ site.comments.utterances.issue_term }}"
crossorigin="anonymous"
async
></script>
2021-12-11 12:24:08 +03:00
<script type="text/javascript">
(function () {
const origin = 'https://utteranc.es';
const iframe = 'iframe.utterances-frame';
const lightTheme = 'github-light';
const darkTheme = 'github-dark';
2021-12-11 12:24:08 +03:00
let initTheme = lightTheme;
const html = document.documentElement;
2021-12-11 12:24:08 +03:00
if (
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
2021-12-11 12:24:08 +03:00
initTheme = darkTheme;
}
addEventListener('message', (event) => {
2021-12-11 12:24:08 +03:00
let theme;
/* credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347> */
if (event.origin === origin) {
/* page initial */
theme = initTheme;
} else if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
2021-12-11 12:24:08 +03:00
/* global theme mode changed */
const mode = event.data.message;
theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme;
2021-12-11 12:24:08 +03:00
} else {
return;
}
const message = {
type: 'set-theme',
2021-12-11 12:24:08 +03:00
theme: theme
};
const utterances = document.querySelector(iframe).contentWindow;
utterances.postMessage(message, origin);
});
})();
2021-12-11 12:24:08 +03:00
</script>