Top subreddit posts widget
Responsive top posts widget.
HTML
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://rawgit.com/davatron5000/FitText.js/master/jquery.fittext.js"></script>
<script src="https://cdn.rawgit.com/KobaKhit/kobakhit.github.io/8a39178a/js/redditjs.js"></script>
<div id="posts">
<h2> Today's top ten news <small>from <a href = '//reddit.com/r/worldnews' target = '_blank'>/r/worldnews</a></small></h2>
<hr>
<ul class="list-unstyled"></ul>
</div>
CSS
/*Scale text*/
.re {
margin-bottom:0px;
}
.re-title {
font-size:20px;
}
.re-under {
margin-top:1px;
}
@media all and (max-width: 500px) {
/* screen size until 500px */
.re-title {
font-size: 16px;
/* 0.8x default size */
}
}
/*Thumbnail rules*/
a.title[href*="aljazeera.com"]{
padding-left:81px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px 0px
}
a.title[href*="ap.org"]{
padding-left:83px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -18px
}
a.title[href*="theatlantic.com"]{
padding-left:49px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -36px
}
a.title[href*="bbc.co.uk"],a.title[href*="bbc.com"]{
padding-left:47px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -54px
}
a.title[href*="bloomberg.com"]{
padding-left:73px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -72px
}
a.title[href*="bostonglobe.com"]{
padding-left:96px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -90px
}
a.title[href*="cbc.ca"]{
padding-left:43px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png") no-repeat 0px -108px
}
a.title[href*="cbsnews.com"]{
padding-left:96px;
background:url("https://raw.githubusercontent.com/KobaKhit/kobakhit.github.io/master/img/Q1ag0NwDPntERAkYW-qo9DiA-ECGYorf0w0XjVR3jfs.png")...
JavaScript
SUBREDDIT = 'worldnews';
function correct_url(text) {
// Remove the slash from the end of the article link to pass to href
var html = $.trim(text);
if (html) {
html = html.replace(/(\/$)/, '');
}
return html;
}
function reddit_list(news) {
var l = $('#posts .list-unstyled');
// clear the existing list
$('#posts .list-unstyled li').remove();
$.each(news, function (index, obj) {
var link = correct_url(obj.url);
l.append($("<li></li>")
.append($("<p class = 're'></p>")
.append("<a class = 'title' target = '_blank' href = " + "http://www." + obj.domain + '></a>')
.append("<a target = '_blank' href = " + link + "><text class = 're-title'>" + obj.title + "</text><a/> <a target = '_blank' style = 'color:grey;outline:0' href=http://www." + obj.domain + '>(' + obj.domain + ')</a>')).append($("<p class='re-under'><a target = '_blank' href = " + 'http://reddit.com' + obj.permalink + " style = 'outline:0'><font color='grey'><b>(score: " + obj.score + ' | comments: ' + obj.num_comments + ') on reddit.com</b></font></a><p>'))
);
});
jQuery(".re-under").fitText(6, {
minFontSize: '9px',
maxFontSize: '14px'
});
}
// Fetch the 10 top posts on /r/worldnews
var news = [];
reddit.top(SUBREDDIT).t('day').limit(10).fetch(function (res) {
// console.log(res);
for (var i = 0; i < res.data.children.length; i++) {
// res contains JSON parsed response from Reddit
news.push(res.data.children[i].data);
}
reddit_list(news);
});