Purpose
Return the complete current Hacker News top-stories collection as structured JSON, including each story's title, author, score, and comment count.
When to Use
Use for requests asking for all current Hacker News top stories or equivalent fields from the front page. The official Firebase API is preferable to scraping visible story rows because it exposes the complete top-story ID collection and stable item fields.
Workflow
- Navigate directly to
https://hacker-news.firebaseio.com/v0/topstories.json. - In one evaluate call on the loaded page, parse the array of story IDs from
document.body.textContent, fetchhttps://hacker-news.firebaseio.com/v0/item/{id}.jsonfor every ID concurrently, and return the result of this extractor:
(async () => {
const ids = JSON.parse(document.body.textContent || "[]");
const items = await Promise.all(
ids.map((id) =>
fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`).then((r) =>
r.json(),
),
),
);
return {
stories: items.map((item, index) => ({
rank: index + 1,
id: item?.id ?? ids[index],
title: item?.title ?? null,
author: item?.by ?? null,
score: Number.isFinite(item?.score) ? item.score : null,
comments: Number.isFinite(item?.descendants) ? item.descendants : 0,
hn_url: item?.id ? `https://news.ycombinator.com/item?id=${item.id}` : null,
})),
};
})();Site-Specific Gotchas
/v0/topstories.jsonreturns an ordered array of opaque item IDs; do not assume the visible homepage contains the complete collection.- Fetch each item from
/v0/item/{id}.json;score,by,title, anddescendantsare the relevant fields. descendantsis Hacker News' comment-count field and may be absent for malformed, deleted, or dead items; preserve missing titles/authors asnulland use0for missing comment counts.- The API is live data, so ranks and values can change between runs. Keep the API order as the story rank.
Expected Output
An object of the form { "stories": [{ "rank": 1, "id": 123, "title": "...", "author": "...", "score": 42, "comments": 7, "hn_url": "https://news.ycombinator.com/item?id=123" }] }, containing one entry for every ID returned by topstories.json.