web/_includes/comments/utterances.html

50 lines
1.6 KiB
HTML
Raw Permalink 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 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;
2024-04-28 22:25:26 +03:00
{%- comment -%} credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347> {%- endcomment -%}
2021-12-11 12:24:08 +03:00
if (event.origin === origin) {
2024-04-28 22:25:26 +03:00
{%- comment -%} page initial {%- endcomment -%}
2021-12-11 12:24:08 +03:00
theme = initTheme;
} else if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
2024-04-28 22:25:26 +03:00
{%- comment -%} global theme mode changed {%- endcomment -%}
2021-12-11 12:24:08 +03:00
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.getElementsByClassName('utterances-frame')[0].contentWindow;
2021-12-11 12:24:08 +03:00
utterances.postMessage(message, origin);
});
})();
2021-12-11 12:24:08 +03:00
</script>