Get reddit post in json
- Post: https://www.reddit.com/r/spacex/comments/57v70p/reddit-post/
- Json: https://www.reddit.com/r/spacex/comments/57v70p/reddit-post.json
Scripts
function resolve (a) {
var post = a[0].data.children[0].data;
var el = '<h1>' + post.title + '</h1>';
el += '<p>score: ' + post.score + ' – subreddit: ' + post.subreddit + ' – comments: ';
el += post.num_comments + '<br>created: ' + new Date(post.created_utc * 1000) + ' by ' + post.author;
el += '</p><br>';
document.querySelector('.markdown-body').innerHTML += el;
document.querySelector('.markdown-body').innerHTML += htmlDecode(post.selftext_html);
document.body.classList.remove('request');
}
if (hash) {
if (hash.slice(-1) === '/') {
hash = hash.slice(0,-1);
}
document.body.classList.add('request');
fetch(hash + '.json').then(dataHandler).then(resolve);
}
Demo