Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2013 14:33
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 anonymous/5607831 to your computer and use it in GitHub Desktop.
Save anonymous/5607831 to your computer and use it in GitHub Desktop.
Tampermonkey script that allows you to hover over a user's name to view his karma while viewing comments on hackernews.
// ==UserScript==
// @name Hover Over And Grab Karma
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description While viewing comments, allows you to hover over a username to view his karma
// @match https://news.ycombinator.com/item?*
// @copyright 2012+, You
// ==/UserScript==
var allLinks = document.querySelectorAll("a");
allLinks = Array.prototype.slice.call(allLinks); // convert nodeList to array
var userLinks = allLinks.filter(function(link) {
return link.href.indexOf("user?id") !== -1;
});
userLinks.forEach(function(link) {
link.onmouseover = function() { addKarmaInfo(link) };
});
function addKarmaInfo(link) {
var onSuccess = function(response) {
var karma = response.match(/karma:.{9}(\d+)/)[1];
addUserInfo(link, " (karma: " + karma + ") ");
};
grabKarma(link.href, onSuccess);
}
function grabKarma(url, onSuccess) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
onSuccess(xmlhttp.response);
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
// Adds or updates text node after the username link
function addUserInfo(afterLink, textToAdd) {
var nextSibling = afterLink.nextSibling;
// span doesn't exist yet, create it
if (!nextSibling || nextSibling.nodeName !== 'SPAN') {
repSpan = document.createElement('span');
repSpan.setAttribute("class", "reputation");
afterLink.parentNode.insertBefore(repSpan, nextSibling);
}
// now update it
repSpan = afterLink.nextSibling;
repSpan.innerHTML = textToAdd;
console.log(afterLink.nextSibling);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment