// ==UserScript== // @name Hacker News Highlighter: See new posts and comments // @namespace http://mschade.me/userscripts // @description Improve [Hacker News](http://news.ycombinator.com/) by highlighting new and updated posts. // @match http://news.ycombinator.com/* // @match https://news.ycombinator.com/* // @include http://news.ycombinator.com/* // @include https://news.ycombinator.com/* // ==/UserScript== // URL to jQuery Library var jqURL = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"; function main() { /* Options */ var C_NEW_POST = 'lightgreen' , C_UPDATED_POST = 'yellow' , C_NEW_COMMENT = 'yellow' ; /* Add ability to compute difference between two arrays. Snippet via http://stackoverflow.com/q/4026828 */ Array.prototype.diff = function(a) { return this.filter(function(i) {return !(a.indexOf(i) > -1);}); }; /* Strips the prefix of query string to retrieve the intended id. */ function getId(s) { return s.replace("item", "").replace("?id=", ""); } /* Makes text plural only if count is not one. For example: pluralizeText("new post", 0) => "0 new posts" pluralizeText("new post", 1) => "1 new post" pluralizeText("new post", 2) => "2 new posts" */ function pluralizeText(text, count) { var ntext = count ? count : "No"; ntext += ' ' + text; if (count != 1) { ntext += 's'; } return ntext; } /* Processes the comments on page to determine which, if any, are new, marking the new comments accordingly. */ function processComments() { var post = getId(window.location.search) , comments = [] , oldComments = [] , comheads = {} ; // Populate comments $(".comhead a:last-child").each(function(index) { var cid = getId($(this).attr('href')); comments.push(cid); comheads[cid] = $(this).parent(); }); // Try to retrieve existing set of comments for this page if (post in localStorage) { oldComments = JSON.parse(localStorage.getItem(post)); // Determine new comments and mark them visually var newComments = comments.diff(oldComments); $.each(newComments, function(index, cid) { comheads[cid].css('background-color', C_NEW_COMMENT); }); /* Update top of page to reflect number of new comments. */ var nspot = $("center > table > tbody > tr > td > br:first"); var ntext = '
' + pluralizeText("new comment", newComments.length) + '
' + pluralizeText("new post", newPosts) + '. ' + pluralizeText("updated post", updatedPosts) + '.' + '