☰

Hacker News - Most upvoted & most commented links

Show top 🔥👄 links of Hacker News

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

"use strict";
/* globals jQuery, $, waitForKeyElements */
// ==UserScript==
// @name         Hacker News - Most upvoted & most commented links
// @namespace    https://github.com/scambier/userscripts
// @author       Simon Cambier
// @version      0.0.10
// @description  Show top 🔥👄 links of Hacker News
// @license      ISC
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js
// @match        https://news.ycombinator.com/
// @match        https://news.ycombinator.com/ask*
// @match        https://news.ycombinator.com/news*
// @match        https://news.ycombinator.com/show*
// @match        https://news.ycombinator.com/front*
// @grant        none
// @run-at       document-end
// ==/UserScript==
(() => {
    // Firefox mobile fix, do nothing if icons are already injected
    if ([...$('[data-userscript="scambier"]')].length)
        return;
    const rows = [...$("tr.athing")];
    // Select lines
    const items = rows
        // Filter out ads
        .filter((tr) => $(tr).find("td.votelinks").length)
        // Get id and score
        .map((tr) => {
        var _a, _b;
        return {
            id: $(tr).attr("id"),
            score: Number((_a = $(tr).next().find(".score")[0].textContent) === null || _a === void 0 ? void 0 : _a.split(" ")[0]),
            // Weirdly, .split(' ') does not work
            comments: Number((_b = $(tr)
                .next()
                .find('a:contains("comments")')
                .text()
                .split("comments")[0]
                .trim()) !== null && _b !== void 0 ? _b : 0),
        };
    });
    // Top 10% for new entries, top 20% for other pages
    const ratio = location.href.includes("news.ycombinator.com/newest")
        ? 0.1
        : 0.2;
    // Inject icons
    items.forEach((o) => {
        const title = $(`tr#${o.id}`).find("span.titleline");
        if (getTop(items, "comments", ratio).includes(o) && o.comments > 0) {
            title.before('<span title="Most commented" data-userscript="scambier">👄 </span>');
        }
        if (getTop(items, "score", ratio).includes(o) && o.score > 0) {
            title.before('<span title="Most upvoted" data-userscript="scambier">🔥 </span>');
        }
    });
    /**
     * Get [ratio] best [items], ordered by [key]
     *
     * @param items
     * @param key
     * @param ratio
     */
    function getTop(items, key, ratio) {
        const count = rows.length;
        return [...items].sort((a, b) => b[key] - a[key]).slice(0, count * ratio);
    }
})();