Skip to content

Instantly share code, notes, and snippets.

@noscript
Last active April 14, 2016 00:20
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 noscript/b0420686256ab961e4e3f668bf9f1f5b to your computer and use it in GitHub Desktop.
Save noscript/b0420686256ab961e4e3f668bf9f1f5b to your computer and use it in GitHub Desktop.
Hacker News: New Comment Marker
// ==UserScript==
// @name Hacker News: New Comment Marker
// @description Make "new" comments since your last visit especially visible
// @downloadURL https://gist.github.com/noscript/b0420686256ab961e4e3f668bf9f1f5b/raw/hacker-news-new-comment-marker.user.js
// @namespace https://gist.github.com/noscript
// @version 2
// @include https://news.ycombinator.com/item?id=*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
// This is a 2016 adaptaion of the original version:
// https://arantius.com/misc/greasemonkey/old-versions/hacker-news-new-comment-marker.user.js
function idForLink(link) {
return parseInt(link.href.replace(/.*\?id=/, ''), 10);
}
function rowFor(el) {
el=el.parentNode;
while (el && el.tagName && 'TR'!=el.tagName) el=el.parentNode;
return el;
}
// Find all the post item links, and their IDs, and the latest ID.
var postLinks=$(".comhead .age a");
var postIds = postLinks.map(function() { return idForLink(this); });
var lastId=Math.max.apply(Math, postIds);
var lastRead=false;
var m=document.cookie.match(/last_read=([0-9]+)/);
if (m && m[1]) {
lastRead=parseInt(m[1], 10);
}
// Mark posts newer than the last read post.
if (lastRead) {
for (var i=0, link=null; link=postLinks[i]; i++) {
if (idForLink(link)>lastRead) {
try {
var row=rowFor(link);
row=rowFor(row);
row.style.backgroundColor='#FBFB78';
} catch (e) {
console.error(e);
}
}
}
}
// Store the last-read post id.
document.cookie=
'last_read='+lastId
+'; path='+document.location.pathname+document.location.search
+'; expire='+( new Date( new Date().valueOf() + 1209600000 ).toGMTString() );
@noscript
Copy link
Author

Screenshot:
screenshot

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