Hacker News Story Rank Change Indicator

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

目前為 2024-02-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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));
})();