summaryrefslogtreecommitdiff
path: root/_includes/extensions/comments/utterances.html
blob: 474356220723fb7bd0ee298002648fd9725f644f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<script async
        crossorigin="anonymous"
        issue-term="{{ site.utterances.issue_term }}"
        label="{{ site.utterances.label }}"
        {%- if site.utterances.follow_site_theme -%}
        onload="initUtterancesTheme()"
        {%- endif -%}
        repo="{{ site.utterances.repo }}"
        src="https://utteranc.es/client.js"
        theme="{{ site.utterances.theme }}">
</script>

{%- if site.utterances.follow_site_theme -%}
<script>
  const setUtterancesTheme = (dataTheme) => {
    const iframe = document.querySelector('.utterances-frame');
    if (iframe) {
      const utterancesTheme = dataTheme === 'dark' ? 'github-dark' : 'github-light'
      const message = {
        type: 'set-theme',
        theme: utterancesTheme
      };
      iframe.contentWindow.postMessage(message, 'https://utteranc.es');
    }
  }

  // init theme
  const initUtterancesTheme = () => {
    const iframe = document.querySelector('.utterances-frame');
    if (!iframe) return
    iframe.onload = () => {
      setUtterancesTheme(document.documentElement.getAttribute('data-theme'))
    }
  }

  // dynamic change
  const observer = new MutationObserver((mutationsList, observer) => {
    for (let mutation of mutationsList) {
      if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
        setUtterancesTheme(document.documentElement.getAttribute('data-theme'))
      }
    }
  });
  observer.observe(document.documentElement, {attributes: true, childList: false, subtree: false});
</script>
{%- endif -%}