summaryrefslogtreecommitdiff
path: root/layouts/partials/telegram_widget.html
blob: 430c72ed66b8a21e8496a694c4ba0b3668512d3d (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
47
48
49
50
51
{{- with site.Params.commentSystem.telegramWidget }}
<div class="telegram-widget-container"></div>

<script>
  (function() {
    const attrs = {
      'data-telegram-discussion': "{{ .channel }}",
      'data-comments-limit': "{{ .limit | default 10 }}",
      // prevent the <script> from loading mutiple times by InstantClick
      'data-no-instant': '',
    }
    const accentColor = '{{ with .accentColor }}{{ . }}{{ end }}';
    if (accentColor) {
      attrs['data-color'] = accentColor;
    }
    if (isDarkTheme()) {
      attrs['data-dark'] = '1'
    }

    var d = document, s = d.createElement('script');
    s.src = "https://telegram.org/js/telegram-widget.js?19";
    for (const key in attrs) {
      s.setAttribute(key, attrs[key]);
    }

    const container = document.querySelector('.telegram-widget-container')
    container.innerHTML = '';
    container.appendChild(s);
  })();
</script>

<script data-no-instant>
  (function() {
    if (!toggleThemeCallbacks) {
      return;
    }

    toggleThemeCallbacks.push((isDark) => {
      // console.log('window toggle-theme for telegram widget')
      if (!window.Telegram) {
        return;
      }
      if (isDark) {
        Telegram.setWidgetOptions({'nodark': 1})
      } else {
        Telegram.setWidgetOptions({'dark': 1})
      }
    });
  })();
</script>
{{- end }}