Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function(){
var separateThousands = function(n) {
var s = String(n);
s = s.replace(/[1-9][0-9]*/, function(match) {
var r = "";
for (var i = 0; i < match.length; i++) {
r = match[match.length - 1 - i] + r;
if (i % 3 == 2 && i < match.length - 1) {
r = "," + r;
}
}
return r;
});
return s;
};
var rpad = function(s, len) {
while (s.length < len) {
s += " ";
}
return s;
}
var lpad = function(s, len) {
while (s.length < len) {
s = " " + s;
}
return s;
}
var candidates = $(".vote-count-post").map(function() {
"use strict";
var score, name, reputation, helpfulFlagEls, helpfulFlags, postEl, postHeight, repEl, withdrawn, vote;
if ($(".vote-count-separator", this).length == 0) {
score = Math.max(+$(this).text(), 1);
} else {
// vote counts are split/open
score = Math.max(
+($("div[style$=green]", this).text())
- +($("div[style$=maroon]", this).text())
, 1);
}
if ($(this).prev().hasClass("vote-up-on")) {
vote = "+1";
} else if ($(this).next().hasClass("vote-down-on")) {
vote = "-1";
} else {
vote = "0";
}
postEl = $(this).closest("tr");
postHeight = postEl.find(".post-text").height();
name = postEl.find(".user-details a").text();
repEl = postEl.find(".user-details .reputation-score");
reputation = +(repEl.attr("title").replace(/[^0-9]/g, "") || repEl.text().replace(/[^0-9]/g, ""));
helpfulFlagEls = postEl.find(".user-details").filter(function() {
return this.childNodes[0].textContent === "helpful flags: ";
});
if (helpfulFlagEls.length) {
helpfulFlags = +helpfulFlagEls[0].childNodes[1].textContent;
withdrawn = false;
} else {
helpfulFlags = "???";
withdrawn = true;
}
return {
score: score,
vote: vote,
name: name,
reputation: reputation,
helpfulFlags: helpfulFlags,
postHeight: postHeight,
withdrawn: withdrawn
};
}).toArray();
if (!candidates.length) {
console.warn("No candidates found. Are you on the primaries page where primary scores are visible?");
}
candidates.sort(function(a, b) {
"use strict";
// Sort withdrawn candidates to the bottom
if (a.withdrawn && !b.withdrawn) return +1;
if (b.withdrawn && !a.widthdrawn) return -1;
return b.score - a.score;
});
var anyWithdrawn = false;
console.log($(candidates).map(function(i, c) {
"use strict";
// put thing above first withdrawn to indicate
if (!c.withdrawn) {
return rpad(lpad("" + (i + 1), 3) + ". " + rpad(c.name, 20) + " [" + lpad("" + c.score, 5) + "]", 30) + " (" + lpad(""+(0|(c.reputation/1000)), 3) + "k rep," + lpad("" + c.helpfulFlags, 5) + " helpful flags)," + lpad("" + c.vote, 3);
} else {
if (!anyWithdrawn) {
var prefix = (" -- withdrawn --") + "\n";
anyWithdrawn = true;
} else {
var prefix = "";
}
return prefix + rpad(rpad(" " + c.name, 25) + " [" + lpad("" + c.score, 5) + "]", 30) + " (" + lpad(""+(0|(c.reputation/1000)), 3) + "k rep)," + lpad("" + c.vote, 3);
}
}).toArray().join("\n"));
jQuery.getScript("https://www.google.com/jsapi").then(function() {
"use strict";
google.load("visualization", "1", {
packages: ["corechart"],
callback: function() {
graph("Height of Nomination Post (pixels)", "Current Primary Score", "postHeight", "score");
graph("log(Reputation × Helpful Flags)", "log(Current Primary Score)", ["helpfulFlags", "reputation"], "score", true, true);
graph("Helpful Flags", "Current Primary Score", "helpfulFlags", "score");
graph("Reputation", "Current Primary Score", "reputation", "score");
function graph(xLabel, yLabel, xKey, yKey, logX, logY) {
var data = new google.visualization.DataTable();
data.addColumn("number", xLabel);
data.addColumn("number", yLabel);
data.addColumn({
type: "string",
role: "tooltip"
});
for (var i = 0, l = candidates.length; i < l; ++i) {
var candidate = candidates[i],
xValue;
if (typeof xKey === "string") {
xValue = candidate[xKey];
} else {
xValue = 1;
for (var j in xKey) {
xValue = xValue * candidate[xKey[j]];
}
}
if (xValue == xValue && typeof xValue === "number" && typeof candidate[yKey] === "number") {
data.addRows([
[xValue, candidate[yKey], candidate.name]
]);
}
}
var target = jQuery("<div style=\"width: 650px; height: 400px; display: inline-block;\"></div>").insertAfter($(".post-text").first())[0];
var chart = new google.visualization.ScatterChart(target);
chart.draw(data, {
title: yLabel + " / " + xLabel,
chartArea: {
top: "15%",
width: "80%",
left: "15%",
height: "70%"
},
legend: "none",
pointSize: 4,
colors: ["#123"],
hAxis: {
title: xLabel,
gridlines: {
count: 10
},
logScale: !! logY
},
vAxis: {
title: yLabel,
gridlines: {
count: 10
},
logScale: !! logX
}
});
}
}
});
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.