Last active
August 29, 2015 14:02
-
-
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)
This file contains 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
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>) "; | |
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