Created
December 10, 2021 16:00
-
-
Save MohamedElashri/facabe53ab60b432b474f271d8984ca0 to your computer and use it in GitHub Desktop.
HN Enhancments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Hacker News | |
// @namespace melashri.net | |
// @version 0.6 (by Mohamed Elashri) | |
// @description Enhanced HN, unread comments highlighter. Open links in new page | |
// @author Prahlad Yeri, Mohamed Elashri | |
// @match https://news.ycombinator.com/* | |
// @grant GM_addStyle | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js | |
// ==/UserScript== | |
console.log('==== HN USERSCRIPT ===='); | |
console.log('css customization'); | |
$("body,a,span,p").css('font-family', 'verdana'); | |
$("tr.spacer").css('height','9px'); | |
$("div.comment").css('font-size','10pt'); | |
$("table.itemlist tbody").css('background-color', '#fff'); | |
$("table.itemlist").parent().css('background-color', '#fff'); | |
$("table.fatitem").parent().css('background-color', '#fff'); | |
$("span a, span.subtext").css('font-size','9pt'); | |
$("td.subtext, td.subtext a").css('font-size','9pt'); | |
$("#hnmain").css('background-color','#fff'); | |
$("#hnmain td[bgcolor='#ff6600'] a").css('font-family','verdana'); | |
$("#hnmain td[bgcolor='#ff6600'] a").css('font-size','11pt'); | |
$("#hnmain td[bgcolor='#ff6600'] a").css('color','white'); | |
$("#hnmain td[bgcolor='#ff6600'] span").css('color','white'); | |
document.querySelectorAll("#hnmain td[bgcolor='#ff6600']")[0].style.backgroundColor='#000'; | |
console.log('done'); | |
if (window.location.pathname.indexOf('/item') == -1) return; | |
/***** Process Comments ****/ | |
console.log('Now processing comments'); | |
var ss = window.location.search; | |
var postID = ss.split("id=")[1]; | |
//console.log(GM_getValue('foo')); | |
//var redditBag = GM_getValue('redditBag'); | |
var hnbag = localStorage.getItem('hnbag'); | |
console.log("postID is ", postID); | |
if (hnbag==null) { | |
console.log('bag not found, creating one'); | |
hnbag = { | |
posts: {} | |
}; | |
} | |
else { | |
console.log('bag found'); | |
hnbag = JSON.parse(hnbag); | |
} | |
var dt=new Date(); | |
var newPost = false; | |
if (hnbag.posts[postID] != undefined) { | |
console.log('post found in history'); | |
} | |
else { | |
newPost = true; | |
console.log('post not found in history'); | |
hnbag.posts[postID] = {}; | |
hnbag.posts[postID].readComments = []; //@todo: add housekeeping mechanism | |
} | |
hnbag.posts[postID].lastVisit = dt.toUTCString(); //hnbag.posts[postID].lastVisit; | |
console.log('lastVisit set as: ', dt.toUTCString()); | |
var cmts = $('.athing.comtr'); | |
console.log(cmts.length, ' comments found on page!'); | |
cmts.each(function() { | |
var cmid = $(this).attr('id'); | |
if (cmid!=undefined) { | |
//console.log('checking ' + cmid + ' as read!'); | |
$(this).each(function(){ | |
if (newPost) { | |
console.log('first visit, all are unread, so dont highlight anything'); | |
hnbag.posts[postID].readComments.push(cmid); | |
} | |
else | |
{ | |
if (hnbag.posts[postID].readComments.indexOf(cmid) == -1) { | |
console.log('now comment found!'); | |
$('#' + cmid).addClass('nr_unread'); | |
hnbag.posts[postID].readComments.push(cmid); | |
} | |
} | |
}); | |
} | |
else { | |
console.log('id attr not found!'); | |
} | |
}); | |
$('.nr_unread').css('background-color','lightblue'); | |
hnbag = JSON.stringify(hnbag); | |
localStorage.setItem('hnbag', hnbag); | |
//Makes all links to posts and users open in a new tab | |
const classes = ["storylink", "hnuser"]; | |
const ids = ["me"]; | |
classes.forEach(c => { | |
let elements = document.getElementsByClassName(c); | |
for (var i = 0; i < elements.length; i++) {elements[i].setAttribute("target", "_blank");} | |
}); | |
ids.forEach(i => { | |
let element = document.getElementById(i); | |
if (typeof element !== "undefined" && element) element.setAttribute("target", "_blank"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment