Skip to content

Instantly share code, notes, and snippets.

@green-flash
Last active October 17, 2022 16:25
Show Gist options
  • Save green-flash/8c9ac15f48291ab0524b to your computer and use it in GitHub Desktop.
Save green-flash/8c9ac15f48291ab0524b to your computer and use it in GitHub Desktop.
updown - shows upvote/downvote counts for a reddit submission on click
// ==UserScript==
// @name updown
// @namespace green_flash
// @description shows up/downvotes for submissions on click
// @include *.reddit.com/r/*
// @version 1
// @grant none
// ==/UserScript==
addButtons();
function addButtons()
{
var linkListing = document.getElementsByClassName("linklisting")[0];
var linkDivs = linkListing.getElementsByClassName("link");
for(var i=0;i<linkDivs.length;i++)
{
var linkDiv = linkDivs[i];
var buttonsList = linkDiv.getElementsByClassName("buttons")[0];
var newButton = document.createElement("a");
newButton.appendChild(document.createTextNode("show updowns"));
newButton.setAttribute("linkIndex", i);
newButton.href="javascript://void(0)";
newButton.onclick = function() {handleClick(this);};
var listElement = document.createElement("li");
listElement.appendChild(newButton);
listElement.onclick = function () {
this.style.display='none';
}
buttonsList.appendChild(listElement);
}
}
function handleClick(button)
{
var linkIndex = button.getAttribute("linkIndex");
var linkListing = document.getElementsByClassName("linklisting")[0];
var linkDivs = linkListing.getElementsByClassName("link");
var linkDiv = linkDivs[linkIndex];
var commentsLink = linkDiv.getElementsByClassName("comments")[0];
var commentsPage = httpGet(commentsLink.href + "?limit=1&depth=1");
var scoreSectionRegex = /<div class=(\"|\')score(\"|\')[\s\S]*?<\/div>/;
var scoreSection = scoreSectionRegex.exec(commentsPage);
var scoreRegex = /<span class=(\"|\')number(\"|\')>([\d\,\.]*?)<\/span>/;
var score = scoreRegex.exec(scoreSection)[3].replace(',','').replace('.','');
var upvotesPercentageRegex = /\((\d+)\s*\%[^\)]*\)/;
var upvotesPercentage = upvotesPercentageRegex.exec(scoreSection)[1];
var upvotes = calcUpvotes(score, upvotesPercentage);
var downvotes = "--";
if (upvotes != "--") {
downvotes = upvotes - score;
}
var taglineParagraph = linkDiv.getElementsByClassName("tagline")[0];
existingUpvoteSpans = taglineParagraph.getElementsByClassName("res_post_ups");
existingDownvoteSpans = taglineParagraph.getElementsByClassName("res_post_downs");
if (existingUpvoteSpans.length > 0 && existingDownvoteSpans.length > 0) {
existingUpvoteSpans[0].textContent = "" + upvotes;
existingDownvoteSpans[0].textContent = "" + downvotes;
return;
}
var updownInfoSpan = document.createElement("span");
var upvoteSpan = document.createElement("span");
upvoteSpan.setAttribute("class", "res_post_ups");
upvoteSpan.setAttribute("style", "color: #FF8B24;");
upvoteSpan.appendChild(document.createTextNode(upvotes));
var downvoteSpan = document.createElement("span");
downvoteSpan.setAttribute("class", "res_post_downs");
downvoteSpan.setAttribute("style", "color: #9494FF;");
downvoteSpan.appendChild(document.createTextNode(downvotes));
updownInfoSpan.appendChild(document.createTextNode("("));
updownInfoSpan.appendChild(upvoteSpan);
updownInfoSpan.appendChild(document.createTextNode("|"));
updownInfoSpan.appendChild(downvoteSpan);
updownInfoSpan.appendChild(document.createTextNode(") "));
taglineParagraph.insertBefore(updownInfoSpan,taglineParagraph.firstChild);
}
function calcUpvotes(score, upvotesPercentage)
{
var upvotes;
if (score != 0) {
upvotes = Math.round(((upvotesPercentage / 100) * score) / (2 * (upvotesPercentage / 100) - 1));
} else {
upvotes = "--";
}
return upvotes;
}
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment