3 ms·
Show HN: Hacker News Story Rank Change Indicator (User Script)
Can copy paste the script in dev console too.
(function() {
'use strict';
var KEY_LAST_CLEAR = 'hackernews-rank-notifier-clear-time';
var KEY_RANK_STORE = 'hackernews-rank-notifier';
var fourWeeksMillisecond = 4 * 7 * 24 * 60 * 60 * 1000; // Four weeks in milliseconds
var lastClearTime = JSON.parse(localStorage.getItem(KEY_LAST_CLEAR)) || Date.now();
// If more than 4 weeks passed since last clean, clear the rank notifier storage
if ((Date.now() - lastClearTime) > fourWeeksMillisecond) {
localStorage.removeItem(KEY_RANK_STORE);
lastClearTime = Date.now();
localStorage.setItem(KEY_LAST_CLEAR, JSON.stringify(lastClearTime));
}
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;
});
localStorage.setItem(KEY_RANK_STORE, JSON.stringify(oldRanks));
})();