2013-11-27 59 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>