1

以下代码用于获取 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 代码只会得到评论?

4

1 回答 1

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
于 2013-06-30T18:29:24.663 回答