浏览代码

Only load disqus comments on button click

Zachary Betz 6 年之前
父节点
当前提交
c2a934f566
共有 4 个文件被更改,包括 33 次插入12 次删除
  1. 6 0
      layouts/partials/disqus-js-common.js
  2. 10 0
      layouts/partials/disqus-js-main.js
  3. 17 3
      layouts/partials/disqus.html
  4. 0 9
      static/js/dom-scripts.js

+ 6 - 0
layouts/partials/disqus-js-common.js

@@ -0,0 +1,6 @@
+// Remove button
+var disqusButton = document.getElementById('disqus-button');
+disqusButton.remove(); 
+// Un-hide comments
+var disqusComments = document.getElementById('disqus-comments');
+disqusComments.style.display = 'block'; 

+ 10 - 0
layouts/partials/disqus-js-main.js

@@ -0,0 +1,10 @@
+// Config
+var disqus_config = function () {
+};
+// Build and append comments 
+var d = document;
+var s = d.createElement('script');
+s.async = true;
+s.src = '//' + "{{ . }}" + '.disqus.com/embed.js';
+s.setAttribute('data-timestamp', + new Date());
+(d.head || d.body).appendChild(s);

+ 17 - 3
layouts/partials/disqus.html

@@ -1,13 +1,27 @@
 <div id="disqus-container">
-  {{ $ctx := . }}
   {{ with .Site.DisqusShortname }}
     <button id="disqus-button" onclick="showComments()">Show comments</button>
     <div id="disqus-comments">
-      {{ if eq . "yourdiscussshortname" }}
+      {{ $isDummyName := eq . "yourdiscussshortname" }}
+      {{ $isServer := $.Site.IsServer }}
+      {{ if or $isDummyName $isServer }}
         <p>Disqus comments are disabled.</p>
+        <script type="application/javascript">
+          function showComments() {
+            {{ partial "disqus-js-common.js" . | safeJS }}
+          }
+        </script>
       {{ else }}
-        {{ template "_internal/disqus.html" $ctx }}
+        <div id="disqus_thread">
+        </div>
+        <script type="application/javascript">
+          function showComments() {
+            {{ partial "disqus-js-main.js" . | safeJS }}
+            {{ partial "disqus-js-common.js" . | safeJS }}
+          }
+        </script>
       {{ end }}
+      <noscript>Enable JavaScript to view Disqus comments.</noscript>
     </div>
   {{ end }}
 </div>

+ 0 - 9
static/js/dom-scripts.js

@@ -131,12 +131,3 @@
     }
   });
 }());
-
-/* Disqus comments */
-function showComments() {
-  var disqusButton = document.getElementById('disqus-button');
-  disqusButton.remove(); 
-
-  var disqusComments = document.getElementById('disqus-comments');
-  disqusComments.style.display = 'block'; 
-}