Hacker News Story Rank Change Indicator

Show how much stories are going up and down in the list

当前为 2024-02-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hacker News Story Rank Change Indicator
// @namespace    http://tampermonkey.net/
// @version      2024-02-10_16-14
// @description  Show how much stories are going up and down in the list
// @author       SMUsamaShah
// @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 KEY_LAST_CLEAR = 'hackernews-rank-notifier-clear-time';
    var KEY_RANK_STORE = 'hackernews-rank-notifier';
    var MAX_STORIES = 3000;

    var oldRanks = JSON.parse(localStorage.getItem(KEY_RANK_STORE)) || {};

    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) + ') ' + title.textContent;
            }
        } else {
            // the story is new
            title.textContent = '(NEW) ' + title.textContent;
        }
        // update the rank in memory
        oldRanks[id] = rank;
    });

    // Get the top 3000 story ids
    var topStoryIds = Object.keys(oldRanks).sort((a, b) => b - a).slice(0, MAX_STORIES);

    // Create a new object with only the top 3000 latest story rankings
    var newRanks = {};
    topStoryIds.forEach(id => {
        newRanks[id] = oldRanks[id];
    });

    localStorage.setItem(KEY_RANK_STORE, JSON.stringify(oldRanks));
})();