Hacker News Rank Notifier

Shows up/down arrows with stories going up and down in the list

Versión del día 04/02/2024. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name         Hacker News Rank Notifier
// @namespace    http://tampermonkey.net/
// @version      2024-02-04
// @description  Shows up/down arrows with stories going up and down in the list
// @author       You
// @match        https://news.ycombinator.com/
// @match        https://news.ycombinator.com/news
// @match        https://news.ycombinator.com/news?p=*
// @match        https://news.ycombinator.com/?p=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ycombinator.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    var oldRanks = JSON.parse(localStorage.getItem('hackernews-rank-notifier')) || {};

    var stories = Array.from(document.getElementsByClassName('athing'));

    stories.forEach(function(story) {
        var id = story.id;
        var rank = story.querySelector('span.rank').innerText;
        rank = parseInt(rank.slice(0, -1)); // removing the dot at the end
        var title = story.querySelector('.title a');

        if (id in oldRanks) {
            var change = oldRanks[id] - rank;
            if (change !== 0) {
                // the story has moved
                title.textContent += ' (' + (change > 0 ? '+' : '-') + Math.abs(change) + ')';
            }
        } else {
            // the story is new
            title.textContent += ' (NEW)';
        }
        // update the rank in memory
        oldRanks[id] = rank;
    });

    localStorage.setItem('hackernews-rank-notifier', JSON.stringify(oldRanks));
})();