Skip to content

Instantly share code, notes, and snippets.

@danlucraft
Last active March 19, 2020 20:33
Show Gist options
  • Save danlucraft/6448537 to your computer and use it in GitHub Desktop.
Save danlucraft/6448537 to your computer and use it in GitHub Desktop.
Disqus embed code for a single page javascript app, with optional SSO.
// Enable or reset Disqus for this page as required, with optional SSO.
// There must be a div with id "disqus_thread" when called.
//
// config is required and should have the format:
//
// {
// shortname: "..",
// title: "..",
// identifier: "..",
// url: ".."
// }
//
// config.shortname *should not* change between invocations.
//
// sso_config is optional and if present should have the format:
//
// {
// api_key: "..",
// message: "..",
// sig: "..",
// timestamp: 2487239473
// }
function enableDisqus(config, sso_config) {
if (enableDisqus.loaded) {
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = config.identifier;
this.page.url = config.url;
this.page.title = config.title;
}
});
} else {
var body = "var disqus_shortname = \"" + config.shortname + "\";\n" +
"var disqus_title = \"" + config.title + "\";\n" +
"var disqus_identifier = \"" + config.identifier + "\";\n" +
"var disqus_url = \"" + config.url + "\";\n";
if (config.developer) {
body += "var disqus_developer = 1;\n"
}
appendScriptTagWithBody(body);
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
if (sso_config) {
var remote_auth_s3 = sso_config.message + " " + sso_config.sig + " " + sso_config.timestamp;
var body = "var disqus_config = function() {\n" +
" this.page.remote_auth_s3 = \"" + remote_auth_s3 + "\";\n" +
" this.page.api_key = \"" + sso_config.api_key + "\";\n" +
"};\n"
appendScriptTagWithBody(body)
}
enableDisqus.loaded = true;
}
}
function appendScriptTagWithBody(body) {
var dso = document.createElement("script");
dso.type = "text/javascript";
dso.async = true;
dso.text = body;
console.log(body);
document.getElementsByTagName('body')[0].appendChild(dso);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment