Created
March 12, 2013 01:43
-
-
Save csytan/5139603 to your computer and use it in GitHub Desktop.
Reddit JS comment loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findRedditComments(query){ | |
var url = 'http://www.reddit.com/search.json?syntax=plain&sort=top'+ | |
'&q=' + query + '&jsonp=?'; | |
$.getJSON(url, function(data){ | |
var articles = data.data.children; | |
articles.sort(function(a, b){ | |
return b.data.num_comments - a.data.num_comments; | |
}); | |
var article = articles[0]; | |
if (article && article.data.num_comments){ | |
loadRedditComments(articles[0].data.permalink); | |
} | |
}); | |
} | |
function comments2HTML(comments){ | |
var html = ''; | |
for (var i=0, comment; comment=comments[i]; i++){ | |
var body = $("<div/>").html(comment.data.body_html).text(); | |
html += '<div class="comment">' + body; | |
if (comment.data.replies) | |
html += comments2HTML(comment.data.replies.data.children); | |
html += '</div>'; | |
} | |
return html; | |
} | |
function loadRedditComments(url){ | |
var url = 'http://www.reddit.com' + url + '.json?jsonp=?' | |
$.getJSON(url, function(data){ | |
var comments = data[1].data.children; | |
var html = comments2HTML(comments); | |
$('.comments').append(html); | |
$('.vid_tabs .reddit').show(); | |
}); | |
} | |
findRedditComments('2ZA_1Xpyqs0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment