Skip to content

Instantly share code, notes, and snippets.

@danielbarry
Last active June 2, 2016 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbarry/b3c8b6581bca54633ebc75efef44009e to your computer and use it in GitHub Desktop.
Save danielbarry/b3c8b6581bca54633ebc75efef44009e to your computer and use it in GitHub Desktop.
A simple notifications system for YCombinator Hacker News
// ==UserScript==
// @name hacker-news-notifications
// @namespace https://news.ycombinator.com/news
// @description Adds simple notifications to Hacker News
// @include https://news.ycombinator.com/news
// @version 1
// @grant none
// ==/UserScript==
/* NOTE: This script is run each time the user refreshes the page. */
/* Run main script */
check();
function check(){
/* Check that storage is enabled */
if(typeof(Storage) !== "undefined"){
/* Initialise arrays */
var current = [];
var previous = [];
/* Get the current state of the page */
current = document.getElementsByClassName("storylink");
/* Itterate over elements and test whether they have changed */
for(var i = 0; i < current.length; i++){
/* Get current state */
var prev = window.sessionStorage.getItem(i);
/* Has it changed? */
if(prev != current[i].text){
/* Update state before changing it */
window.sessionStorage.setItem(i, current[i].text);
/* Discover if we have just started this session */
if(sessionAlive()){
/* By default, say it moved up the rankings */
current[i].style.color = "#0F0";
/* Check to see if it actually moved down */
for(var z = i - 1; z >= 0; z--){
if(prev == current[z].text){
/* It actually moved down the rankings because it exists above */
current[i].style.color = "#F00";
/* Break out of this loop, no more searching needed */
break;
}
}
}
}
}
/* Update the fact this is a live session */
setAlive();
}else{
/* Indicate there has been error silently in the console */
err("Your browser doesn't support storage.");
}
}
/**
* sessionAlive()
*
* Checks whether this is a new session or an old session.
*
* @return True if old session, False is new session.
**/
function sessionAlive(){
/* Return the current state of the session */
return window.sessionStorage.getItem("alive") == "true";
}
/**
* setAlive()
*
* Set the session status to be alive.
**/
function setAlive(){
window.sessionStorage.setItem("alive", "true");
}
/**
* err()
*
* Displays a message in the console indicating there has been an error of some
* kind.
*
* @param msg The message to be displayed.
**/
function err(msg){
console.log("hacker-news-notifications\n [!!] " + msg);
}
@danielbarry
Copy link
Author

danielbarry commented Jun 2, 2016

hacker-news-notifications

Description

An extremely simple notifications system that tells you on a refresh whether a link has changed by highlighting it green (it has moved up a position) or red (it has moved down a position). All of the data is stored locally in your browser via the HTML5 storage capability and is deleted per session.

Example Output

hacker-news-notifications

Issues

If it doesn't appear to work then please check your console output as your browser may not be compatible. Please check this before raising a bug.

Known Bugs

None.

Bug fixes:

[X] Data is only stored for the session and not indefinitely as this seems somewhat irresponsible for user's space
[X] Newly added items at the bottom of the list appear green instead of red
[X] Check to see if the session is new - therefore no previous data exists and we cannot make judgement

Feature Request

[.] Background checking of the page (careful not to strain the server)
[.] Desktop notifications
[.] Tracking of comments and replys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment