Last active
January 12, 2025 01:30
-
-
Save miguelmota/6e159d56e73c244a213eca65cfb8a7ba to your computer and use it in GitHub Desktop.
Hacker News sort by points
This file contains 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
function extractAndSortPosts() { | |
// Get all posts | |
const rows = document.querySelectorAll(".athing.submission"); | |
const posts = []; | |
rows.forEach(row => { | |
// Extract title | |
const titleElement = row.querySelector(".titleline a"); | |
const title = titleElement ? titleElement.innerText : "No title"; | |
// Extract URL | |
const url = titleElement ? titleElement.href : ""; | |
// Extract domain | |
const domainElement = row.querySelector(".sitestr"); | |
const domain = domainElement ? domainElement.innerText : "unknown"; | |
// Extract points | |
const id = row.id; | |
const scoreElement = document.querySelector(`#score_${id}`); | |
const points = scoreElement ? parseInt(scoreElement.innerText.split(" ")[0], 10) : 0; | |
// Extract comments | |
const subtextRow = row.nextElementSibling.querySelector(".subtext"); | |
let numComments = 0; | |
let commentsLink = "#"; | |
if (subtextRow) { | |
const commentElements = subtextRow.querySelectorAll("a"); | |
const lastLink = commentElements[commentElements.length - 1]; | |
if (lastLink) { | |
const commentsText = lastLink.innerText; | |
commentsLink = lastLink.href; | |
numComments = parseInt(commentsText.split(" ")[0], 10) || 0; | |
} | |
} | |
posts.push({ title, url, domain, points, numComments, commentsLink }); | |
}); | |
// Sort by points in descending order | |
posts.sort((a, b) => b.points - a.points); | |
return posts; | |
} | |
function clearPageAndSetBackground() { | |
// Clear the entire page content | |
document.body.innerHTML = ""; | |
// Set the background color to white | |
document.body.style.backgroundColor = "white"; | |
} | |
function displaySortedPosts(sortedPosts) { | |
// Create a container for the new list | |
const container = document.createElement("div"); | |
container.style.fontFamily = "Arial, sans-serif"; | |
container.style.margin = "20px"; | |
// Add a title | |
const title = document.createElement("h1"); | |
title.innerText = "Sorted Hacker News Posts"; | |
container.appendChild(title); | |
// Add the sorted posts | |
const table = document.createElement("table"); | |
table.style.width = "100%"; | |
table.style.borderCollapse = "collapse"; | |
sortedPosts.forEach(post => { | |
const row = document.createElement("tr"); | |
row.style.borderBottom = "1px solid #ccc"; | |
row.innerHTML = ` | |
<td style="padding: 8px;">${post.points} points</td> | |
<td style="padding: 8px;"> | |
<a href="${post.url}" target="_blank">${post.title}</a> | |
</td> | |
<td style="padding: 8px;"> | |
<a href="${post.commentsLink}" target="_blank">${post.numComments} comments</a> | |
</td> | |
<td style="padding: 8px;">${post.domain}</td> | |
<td style="padding: 8px;"><a href="${post.url}" target="_blank">${post.url}</a></td> | |
`; | |
table.appendChild(row); | |
}); | |
container.appendChild(table); | |
document.body.appendChild(container); | |
} | |
// Execute the steps | |
const sortedPosts = extractAndSortPosts(); | |
clearPageAndSetBackground(); | |
displaySortedPosts(sortedPosts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment