import linkifyElement from "linkifyjs/element"; import { getItemInfo } from "../libs/api"; import { paths } from "../libs/paths"; import { getUrlParams, monthNames } from "../libs/utils"; function init() { const links = document.querySelectorAll( 'span.commtext a[href*="news.ycombinator.com/item?id="]' ); const linkifyOptions = { attributes: { rel: "noopener", }, }; for (const link of links) { const itemDiv = document.createElement("div"); itemDiv.classList.add("__rhn__hover-info", "__rhn__no-display"); itemDiv.style.left = link.getBoundingClientRect().left + "px"; itemDiv.innerHTML = ``; link.parentElement.insertBefore(itemDiv, link.nextSibling); link.dataset.rhnInfoLoaded = "0"; link.addEventListener("mouseover", async () => { itemDiv.classList.remove("__rhn__no-display"); itemDiv.style.left = link.getBoundingClientRect().left + "px"; if (link.dataset.rhnInfoLoaded === "0") { link.dataset.rhnInfoLoaded = "1"; const id = getUrlParams("id", link.href); const itemInfo = await getItemInfo(id); const itemDate = new Date(itemInfo.time * 1000); const renderedDate = `${ monthNames[itemDate.getMonth()] } ${itemDate.getDate()}, ${itemDate.getFullYear()}`; let table = ""; switch (itemInfo.type) { case "comment": { table = `
by: ${itemInfo.by}
date: ${renderedDate}
text: ${itemInfo.text}
`; break; } case "story": { const text = itemInfo.text ? ` text: ${itemInfo.text} ` : ""; const url = itemInfo.url ? ` url: ${itemInfo.url} ` : ""; const comments = itemInfo.kids ? ` comments: ${itemInfo.kids.length} ` : ""; table = ` ${url} ${comments} ${text}
title: ${itemInfo.title}
by: ${itemInfo.by}
date: ${renderedDate}
score: ${itemInfo.score}
`; break; } default: break; } itemDiv.innerHTML = table; linkifyElement(itemDiv, linkifyOptions); } }); link.addEventListener("mouseout", () => { itemDiv.classList.add("__rhn__no-display"); }); } return true; } const details = { id: "show-item-info-on-hover", pages: { include: [...paths.comments, ...paths.specialComments], exclude: [""], }, loginRequired: false, init, }; export default details;