Skip to content

Instantly share code, notes, and snippets.

@ingmar
Last active December 17, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ingmar/5564603 to your computer and use it in GitHub Desktop.
Save ingmar/5564603 to your computer and use it in GitHub Desktop.
Give HN some visual indication of points and comments
// ==UserScript==
// @name Hacker News improved
// @description Various improvements to Hacker News
// @version 4
// @namespace org.sdfjkl.greasemonkey.hn-improved
// @include http*://news.ycombinator.com/
// @include http*://news.ycombinator.com/news
// @include http*://news.ycombinator.com/newest
// @include http*://news.ycombinator.com/best
// @include http*://news.ycombinator.com/ask
// @include http*://news.ycombinator.com/item?*
// @require http://code.jquery.com/jquery-latest.js
// @grant GM_addStyle
// ==/UserScript==
var RE_POINTS = /(\d+) points/;
var RE_COMMENTS = /(\d+) comments/;
$(document).ready(function() {
var loc = window.location;
// CSS
GM_addStyle(
'.hni-bars { position: relative; opacity: 0.4; top: -2px; height: 0px; max-width: 1000px }' +
'.hni-spacer { height: 1px; }'
);
if (loc.pathname == '/item') {
// Votes
$('td.default > span.comhead').each(function(index) {
var m_votes = $(this).text().match(RE_POINTS);
var votes = 0;
if (m_votes) votes = m_votes[1];
//console.log(votes);
$(this).parent().prepend('<div class="hni-bars">' +
'<div style="width: ' + votes + 'px; height: 2px; background: green;"></div>' +
'</div>');
});
// My own posts
$('td > center > font').each(function(index) {
$(this).parent().css('background-color', '#00F1FF');
});
} else {
// inject bars that visually indicate the points/comments of an article
$('td.subtext > span').each(function(index) {
// get point and comment numbers
var m_points = $(this).text().match(RE_POINTS);
var m_comments = $(this).nextAll().eq(2).text().match(RE_COMMENTS);
// some articles (job ads) may not have points or comments
var points = 0;
var comments = 0;
if (m_points) points = m_points[1];
if (m_comments) comments = m_comments[1];
//console.log(points, comments);
$(this).parent().prepend('<div class="hni-bars">' +
'<div style="width: ' + points + 'px; height: 2px; background: green;"></div>' +
'<div style="width: ' + comments + 'px; height: 2px; background: orange;"></div>' +
'</div>');
// reclaim some vertical space so the page fits on my screen again
$(this).parent().parent().next().css('height', '4px');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment