Disqus评论计数多个页面

时间:2013-06-30 18:18:19

标签: php javascript html disqus

以下代码用于获取多个页面的Disqus评论计数。

var DISQUSWIDGETS;

if (typeof DISQUSWIDGETS != 'undefined') {
    DISQUSWIDGETS.displayCount({
        "showReactions":true,
        "text":{
            "and":"and",
            "reactions":{
                "zero":"0 Reactions",
                "multiple":"{num} Reactions",
                "one":"1 Reaction"},
            "comments":{
                "zero":"0 Comments",
                "multiple":"{num} Comments",
                "one":"1 Comment"
            }
        },
        "counts":[
            {"reactions":0,"uid":1,"comments":2},
            {"reactions":0,"uid":2,"comments":5},
            {"reactions":0,"uid":3,"comments":9}
        ]
    });
}

我想从这里得到一些评论示例:

{"reactions":0,"uid":1,"comments":2}评论数量应为2。

是否有任何javascript代码只能获得评论?

1 个答案:

答案 0 :(得分:1)

如果你能够这样重写:

var DISQUSWIDGETS;

if (typeof DISQUSWIDGETS != 'undefined') {
    var disqus_options = {
        "showReactions":true,
        "text":{
            "and":"and",
            "reactions":{
                "zero":"0 Reactions",
                "multiple":"{num} Reactions",
                "one":"1 Reaction"},
            "comments":{
                "zero":"0 Comments",
                "multiple":"{num} Comments",
                "one":"1 Comment"
            }
        },
        "counts":[
            {"reactions":0,"uid":1,"comments":2},
            {"reactions":0,"uid":2,"comments":5},
            {"reactions":0,"uid":3,"comments":9}
        ]
    };
    DISQUSWIDGETS.displayCount(disqus_options);
}

然后你就可以像这样访问计数:

disqus_options.counts[0].comments

在这种情况下具有值2


修改

id进行过滤,在本例中为id == 3

var counts_with_id = $.grep(disqus_options.counts, function(count) {
    return (count.uid == 3);
});
if (counts_with_id.length) // the id exists
    counts_with_id[0].comments // has value 9
else
    the id does not exist in the disqus_options.

修改

你可以“破解”displayCount方法:

// Load the disqus plugin which contains the DISQUSWIDGETS.displayCount method.

// Change what DISQUSWIDGETS.displayCount does.
var actualDisplayCount = DISQUSWIDGETS.displayCount;
DISQUSWIDGETS.displayCount = function(options) {
    // do things with options
    return actualDisplayCount(options);
};

// Now load the source from http://forum.disqus.com/count-data.js?q=1&1=2,http://www.website.com&2=2