disqusjs.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {{- $pc := .Site.Config.Privacy.Disqus -}}
  2. {{- $disqusjs := .Site.Params.Comments.disqusjs -}}
  3. {{- if and (not $pc.Disable) (and $disqusjs.Shortname $disqusjs.ApiKey) -}}
  4. {{- $style := resources.Get "scss/partials/comments/disqusjs.scss" | resources.ToCSS | minify -}}
  5. <link rel="stylesheet" href="{{ $style.RelPermalink }}">
  6. <div class="disqus-container">
  7. <div id="disqus_thread"></div>
  8. <script type="application/javascript">
  9. let disqusjs;
  10. function loadDisqusJS() {
  11. disqusjs = new DisqusJS({
  12. shortname: {{ $disqusjs.Shortname }},
  13. siteName: {{ .Site.Title }},
  14. apikey: {{ $disqusjs.ApiKey }},
  15. {{ with $disqusjs.ApiUrl }}api: {{ . }},{{ end }}
  16. {{ with $disqusjs.Admin }}admin: {{ . }},{{ end }}
  17. {{ with $disqusjs.AdminLabel }}adminLabel: {{ . }}{{ end }}
  18. });
  19. }
  20. function lazyLoadDisqusJS() {
  21. if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
  22. document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
  23. return;
  24. }
  25. let d = document.createElement('script');
  26. d.src = 'https://cdn.jsdelivr.net/npm/disqusjs@1.3/dist/disqus.js';
  27. d.async = false;
  28. document.body.appendChild(d);
  29. d.onload = () => {
  30. loadDisqusJS();
  31. window.addEventListener('onColorSchemeChange', (e) => {
  32. if (disqusjs) {
  33. loadDisqusJS();
  34. }
  35. })
  36. }
  37. }
  38. let runningOnBrowser = typeof window !== "undefined";
  39. let isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
  40. let supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window;
  41. if (!isBot && supportsIntersectionObserver) {
  42. let disqus_observer = new IntersectionObserver(function(entries) {
  43. if (entries[0].isIntersecting) {
  44. lazyLoadDisqusJS();
  45. disqus_observer.disconnect();
  46. }
  47. });
  48. disqus_observer.observe(document.getElementById('disqus_thread'));
  49. } else {
  50. lazyLoadDisqusJS();
  51. }
  52. </script>
  53. <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  54. </div>
  55. {{- end -}}