Skip to content

Instantly share code, notes, and snippets.

@ryanvalentin
Last active December 19, 2015 16:59
Show Gist options
  • Save ryanvalentin/5987676 to your computer and use it in GitHub Desktop.
Save ryanvalentin/5987676 to your computer and use it in GitHub Desktop.
Check Disqus for identifier conflicts before loading
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
var disqus_shortname = 'YOUR_DISQUS_SHORTNAME'; // required: replace example with your forum shortname
var disqus_identifier = 'THE_DISQUS_IDENTIFIER';
var disqus_url = 'THE_PAGE_URL';
var disqus_public_key = 'YOUR_API_PUBLIC_KEY'; // Get one at http://disqus.com/api/applications/
$(document).ready(function () {
var req = $.ajax({
type: 'GET',
url: 'https://disqus.com/api/3.0/threads/details.jsonp',
data: { api_key: disqus_public_key, forum : disqus_shortname, thread : 'link:' + disqus_url },
cache: false,
dataType: 'jsonp',
timeout : 2500,
});
req.success(function(result) {
// Check the URL and note whether it has changed
if (result.response.link != disqus_url) {
console.log('Warning: thread link doesn\'t match declared URL.');
}
var existingIdentifiers = result.response.identifiers;
// Check if the identifier is already attached to the thread and that the array isn't empty
if (jQuery.inArray(disqus_identifier, existingIdentifiers) == -1 && existingIdentifiers.length > 0) {
// Identifier is not in this array, and there's a different identifier attached to this thread already
// We want to prevent attaching this identifier to another thread, because it probably wasn't intended.
console.log('Error: Identifier conflict was prevented. Existing thread was ' + result.response.id);
return;
}
else {
// Identifier is in the array already, or there is no identifier attached to this thread. Probably safe to let Disqus load.
console.log('Info: Thread exists, but it\'s safe to load.');
/* * * DON'T EDIT BELOW THIS LINE */
(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);
})();
}
});
req.error(function() {
// No thread found, loading Disqus is perfectly fine
/* * * DON'T EDIT BELOW THIS LINE */
(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);
})();
});
});
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment