Show how much stories are going up and down in the list
当前为
// ==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));
})();