Skip to content

Instantly share code, notes, and snippets.

@shaungallagher
Created May 3, 2014 11:58
Show Gist options
  • Save shaungallagher/3d3a3ae8ffa2de7653ca to your computer and use it in GitHub Desktop.
Save shaungallagher/3d3a3ae8ffa2de7653ca to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Hacker News profile bar
// @description Adds a profile bar to each comment that includes data from the user's profile page (e.g. karma, days since creation, "about me" text)
// @author Shaun Gallagher, author of "Experimenting With Babies" (http://www.experimentingwithbabies.com) and "Correlated" (http://www.correlated.org)
// @namespace HNprofilebar
// @match https://news.ycombinator.com/*
// ==/UserScript==
// load jQuery
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// add a profile bar
function main() {
user_set = [];
users = {};
counter = 0;
max_gets = 50;
$('.comhead a[href^="user"]').each(function() {
var div = $(this).parent().parent();
var comhead = $(this).parent();
var user = $(this).text();
if ($.inArray(user, user_set) == -1 && counter <= max_gets) {
user_set.push(user);
var about = window.localStorage.getItem('hn_about_' + user);
var stats = window.localStorage.getItem('hn_stats_' + user);
function insertBar(user, stats, about) {
$('.comhead a[href="user?id=' + user + '"]').each(function() {
var div = $(this).parent().parent();
var comhead = $(this).parent();
$(comhead).append(stats);
if (about) {
html = $('<div style="margin-top: 5px; font-family: verdana; font-size: 8pt">' + about + '</div>');
$(div).append(html);
}
});
}
if (!stats) {
counter++;
$.get('https://news.ycombinator.com/user?id=' + user, function(data) {
var about = $(data).find('textarea[name="about"]').text();
if (!about) {
about = $(data).find('td:contains("about:")').next().html();
about = about.replace(/<p>/g,'<br>')
about = about.replace(/<\/p>/g,'<br>')
about = about.replace(/<br>/g, ' ')
}
if (about) {
if (about.length > 500) {
about = about.substr(0, 500) + '...';
}
window.localStorage.setItem('hn_about_' + user, about);
}
var karma = $(data).find('td:contains("karma:")').next().text();
var avg = $(data).find('td:contains("avg:")').next().text();
var created = $(data).find('td:contains("created:")').next().text();
var stats = ' | karma: ' + karma + ' | avg: ' + avg + ' | created: ' + created;
window.localStorage.setItem('hn_stats_' + user, stats);
insertBar(user, stats, about);
});
}
else {
insertBar(user, stats, about);
}
}
});
}
// load jQuery and execute the main function
addJQuery(main);
@shaungallagher
Copy link
Author

Screenshot shows the profile bars in action:

Learn how to install this user script

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