Skip to content

Instantly share code, notes, and snippets.

@postworthy
Created April 4, 2024 23:11
Show Gist options
  • Save postworthy/0fac90fb4e41a302b33bb61ad1bec325 to your computer and use it in GitHub Desktop.
Save postworthy/0fac90fb4e41a302b33bb61ad1bec325 to your computer and use it in GitHub Desktop.
<html>
<body>
<script>
(async () => {
// Fetch the top story IDs
const topStoriesUrl = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty';
const response = await fetch(topStoriesUrl);
const storyIds = await response.json();
// Limit to the top 100 stories
const top100Ids = storyIds.slice(0, 100);
const titlesWithLinks = [];
for (const storyId of top100Ids) {
// Fetch each story's details by ID
const storyUrl = `https://hacker-news.firebaseio.com/v0/item/${storyId}.json?print=pretty`;
const storyResponse = await fetch(storyUrl);
const storyData = await storyResponse.json();
// Collect the title and URL of the story
const title = storyData.title || 'No Title';
const articleUrl = storyData.url || '#'; // Fallback to "#" if URL is missing
titlesWithLinks.push(`<a href="${articleUrl}" target="_blank">${title}</a>`);
}
// Combine titles into a single string of HTML links to display
const titlesHtml = titlesWithLinks.map((link, index) => `${index + 1}. ${link}`).join('<br>');
// Write titles directly to the body of the document as hyperlinks
document.body.innerHTML = titlesHtml;
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment