Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Last active August 29, 2015 14:02
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 1995eaton/1e789042e8d21e06bfb8 to your computer and use it in GitHub Desktop.
Save 1995eaton/1e789042e8d21e06bfb8 to your computer and use it in GitHub Desktop.
A userscript that adds upvotes and downvotes to reddit comments (similar to RedditEnhancementSuite)
function formatComments() {
var commments = document.querySelectorAll("div[data-ups]");
for (var i = 0, l = commments.length; i < l; ++i) {
var data = commments[i];
var likes = data.getAttribute("data-ups");
var dislikes = data.getAttribute("data-downs");
if (likes !== undefined && dislikes !== undefined) {
var post = data.querySelector(".entry.unvoted");
if (post && !post.querySelector(".wooo")) {
var uad = post.querySelectorAll(".noncollapsed>.tagline>span[class$='likes']");
if (!uad || !uad.length) {
continue;
}
var p = uad[0].parentNode;
var cc = document.createElement("span");
cc.innerHTML = "(<span class='wooo' style='color:#7e6ff3'>" + dislikes + "</span> | <span style='color:orangered'>" + likes + "</span>)&nbsp;";
cc.style.display = "inline";
var q = p.children;
p.insertBefore(cc, q[q.length-2]);
if (cc.previousSibling && cc.previousSibling.style) {
cc.previousSibling.style.display = "none";
}
}
}
}
}
var ca = document.querySelector(".commentarea");
if (ca) {
formatComments();
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes && mutation.addedNodes.length && mutation.addedNodes[0].className) {
formatComments();
}
});
});
window.setTimeout(function() {
formatComments();
observer.observe(ca, {
subtree: true,
childList: true
});
}, 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment