Last active
September 26, 2017 06:52
-
-
Save paulwellnerbou/a09e3819307851993df0aba65b266e86 to your computer and use it in GitHub Desktop.
Javascript resource to embed disqus in your Ghost blog (or any other website) just with data-attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Usage: | |
Place this tag at the end of your HTML: | |
<script id="disqus-script" data-disqusName="your-disqus-name" data-element="selector-of-element-where-to-append-disqus-container" | |
src="https://cdn.rawgit.com/paulwellnerbou/a09e3819307851993df0aba65b266e86/raw/ee408decdfbb8df5180894741dc59a5423d706a6/embed-disqus.js"/> | |
Original version: https://gist.github.com/paulwellnerbou/a09e3819307851993df0aba65b266e86 | |
*/ | |
var disqus = document.getElementById('disqus-script'); | |
var disqusName = disqus.getAttribute('data-disqusName'); | |
// The element where to append the Disqus discussion container, for Ghost 0.11 and the casper theme this is '.post-content' | |
var elementToAppendDisqusContainer = disqus.getAttribute('data-element'); | |
var postContent = document.querySelector(elementToAppendDisqusContainer); | |
if(postContent !== null) { | |
var div = document.createElement('div'); | |
div.setAttribute('id', 'disqus_thread'); | |
postContent.append(div); | |
var canonical = document.querySelector('link[rel=canonical]').getAttribute('href') | |
var identifier = canonical.slice(canonical.slice(0, -1).lastIndexOf('/') + 1, -1); | |
var disqus_config = function () { | |
this.page.url = canonical; | |
this.page.identifier = identifier; | |
}; | |
(function() { | |
var d = document, s = d.createElement('script'); | |
s.src = 'https://' + disqusName + '.disqus.com/embed.js'; | |
s.setAttribute('data-timestamp', +new Date()); | |
(d.head || d.body).appendChild(s); | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment