Created
April 4, 2024 23:11
-
-
Save postworthy/0fac90fb4e41a302b33bb61ad1bec325 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<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