Skip to content

Instantly share code, notes, and snippets.

@ooade
Last active April 13, 2017 16:49
Show Gist options
  • Save ooade/9a4c83858bad9a21098ea11bec7bd88d to your computer and use it in GitHub Desktop.
Save ooade/9a4c83858bad9a21098ea11bec7bd88d to your computer and use it in GitHub Desktop.
Grab posts from subreddit

I made use of fetch API(A browser based request API, LOL). You can replace with any of your choice :)

.sub {
background-color: #fff;
font-size: 14px;
padding: 10px;
border-bottom: 1px solid #ddd;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel='stylesheet' href='index.css'/>
</head>
<body>
<div id='post'></div>
<script src='index.js'></script>
</body>
</html>
const post = document.getElementById('post');
fetch('https://www.reddit.com/r/EPL/.json')
.then(res => res.json()) // Fetch JSON
.then(res => res.data.children) // Grab the children
.then(res => {
res.map((sub, n) => { // Loop through each Subreddits
let { data: { id, title } } = sub;
// Let's render our title to the page with numbers...
// 1.) blah blah blah
let textnode = document.createTextNode(`${n + 1}.) ${title}`);
let div = document.createElement('div');
div.id = id;
div.className= 'sub';
div.appendChild(textnode);
post.appendChild(div)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment