2013-11-27 54 views
4

我知道这似乎是一个简单的问题,但我发现没有简单的方法获得ajax页面中给定disqus标识符的评论计数。Disqus得到的评论计数

我已经看过他们的API,这是一种选择,但我们正在为最终用户提供基于CMS AJAX网站,这似乎有点乏味,迫使每个用户必须创建自己的自己的disqus应用程序API并填写公开密钥和秘密密钥,以获得评论数。此外,加载一个单独的远程JS,似乎矫枉过正,返回一个完整的JSON对象,只是为了获得当前页面的评论数。

有一个count.js脚本here,但没有关于如何动态更新ajax页面计数的信息。几乎...在经过大量搜索之后,我发现了一些未公开的方法DISQUSWIDGETS.getCount()。但是,在每个标识符的一次调用后,这将停止工作。此外,这种方法还需要加载一个外部JS只是为了获得评论数...

似乎奇怪的#comments数量不能被提取更容易。在评论显示在页面上之后,评论的数量仍然可用,但我们无法使用JS访问该iframe。任何启示是赞赏...

+0

Disqus是我用过的最糟糕的服务之一。他们的API要么不存在,要么不工作,要么只工作一次(!?!?!)。显然'DISQUS.getCommentCounts()'对于他们来说太明显了。 – AJB

回答

0
This is the html file which will help you to count the number of comments in a particular node in for any site which has disqus comment. 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Disqus Comment Counts Example</title> 
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
    var disqusPublicKey = "?"; 

    var disqusShortname = "?"; // Replace with your own shortname 

    var urlArray = []; 

    $('.count-comments').each(function() { 
     var url = $(this).attr('data-disqus-url'); 
     urlArray.push('link:' + url); 
     }); 



    $('#get-counts-button').click(function() { 
      $.ajax({ 
      type: 'GET', 
       url: "https://disqus.com/api/3.0/threads/set.jsonp", 
        data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray }, 
cache: false, 
dataType: 'jsonp', 
success: function (result) { 

    for (var i in result.response) { 

    var countText = " comments"; 
    var count = result.response[i].posts; 

    if (count == 1) 
     countText = " comment"; 

    $('div[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count +  countText + '</h4>'); 

     } 
    } 
    }); 
}); 


    }); 
    </script> 
    </head> 
     <body> 
    <h1>Comment Counts Example</h1> 
    <div> 
     <a href="#"> 
      <h2>Fullscreen BEAM: The first YouTube app for Google Glass comes with public or private sharing</h2> 
       <div class="count-comments" data-disqus-  url="http://www.dev.indiawaterportal.org/questions/can-using-ro-water-result-stomach-problems"></div> 
     </a> 
    </div> 

     <button type="button" id="get-counts-button">Get Comment Counts</button> 
    </body>