Skip to content

Instantly share code, notes, and snippets.

@zacharee
Created June 14, 2016 16:53
Show Gist options
  • Save zacharee/4634e464af33b7247cbce75231b90ab6 to your computer and use it in GitHub Desktop.
Save zacharee/4634e464af33b7247cbce75231b90ab6 to your computer and use it in GitHub Desktop.
AU Election Script Thingy
// ==UserScript==
// @id stackexchange-election-primary-counter@poke
// @name StackExchange Election: Primary counter
// @namespace poke
// @version 1.4.4
// @author Patrick Westerhoff
// @include *://askubuntu.com/election/*
// @include *://askubuntu.com/election*
// @updateURL https://gist.githubusercontent.com/poke/9058895/raw/stackexchange-election-primary-counter.user.js
// @run-at document-end
// ==/UserScript==
(function () {
var primaryLink = document.querySelector('#tabs > a[href$="primary"]');
if (!primaryLink || primaryLink.className != 'youarehere')
return;
function addRow (table, cols, isHeader) {
var row = document.createElement('tr');
cols.forEach(function (cell) {
if (typeof cell !== 'object') {
cell = document.createTextNode(cell);
}
row.appendChild(document.createElement(isHeader ? 'th' : 'td')).appendChild(cell);
});
return table.appendChild(row);
}
// styles
var style = document.createElement('style');
style.textContent = '#_primary-counter { border: 1px solid silver; margin-bottom: 1em;}' +
'#_primary-counter th { border-bottom: 1px solid silver; }' +
'#_primary-counter td, #_primary-counter th { padding: 1px .5em; }' +
'#_primary-counter tr :nth-child(n+3) { text-align: right; }' +
'#_primary-counter tr:nth-child(11) { border-bottom: 1px solid silver; }' +
'#_primary-counter tr.withdrawn { color: #999; }';
document.head.appendChild(style);
// query candidate data
var users = Array.prototype.map.call(document.querySelectorAll('#mainbar > table > tbody > tr'), function (post) {
var scores = post.querySelector('.candidate-score-breakdown');
return {
name: post.querySelector('td.owner .user-info .user-details > a').textContent,
id: post.id.substr(5),
href: '#' + post.id,
votes: parseInt(post.querySelector('.votecell .vote-count-post').textContent, 10),
score: scores.querySelector('span').textContent.match(/(\d+\/\d+)/)[1],
rep: scores.querySelector('ul > li:nth-child(1)').textContent.trim().match(/reputation (.+)/)[1],
modBadges: scores.querySelector('ul > li:nth-child(2)').textContent.match(/moderation badges: (\d)\/\d/)[1],
editBadges: scores.querySelector('ul > li:nth-child(3)').textContent.match(/editing badges: (\d)\/\d/)[1],
participationBadges: scores.querySelector('ul > li:nth-child(4)').textContent.match(/participation badges: (\d)\/\d/)[1],
memberFor: post.querySelector('.user-details').lastChild.textContent.trim().match(/member for (.+)/)[1],
withdrawn: !!post.querySelector('.withdraw-date')
};
});
users.sort(function (a, b) { return b.votes - a.votes; });
// vote count querying
var mayQueryVoteCounts = document.body.innerHTML.indexOf('StackExchange.vote.bindFetchVoteCount') !== -1;
function queryVoteCounts (evt) {
evt.preventDefault();
var xhr = new XMLHttpRequest();
xhr.open('get', '/posts/' + this.id + '/vote-counts?_=' + (+new Date()), true);
xhr.onload = function () {
if (this.status == 200) {
var data = JSON.parse(this.response);
evt.target.parentNode.innerHTML = '+' + (+Math.abs(data.up)) + ' / ' + (-Math.abs(data.down));
}
};
xhr.send(null);
}
// display data
var table = document.createElement('table');
table.id = '_primary-counter';
addRow(table, ['#', 'User', 'Votes', 'Score', 'Reputation', 'Badges (M/E/P)', 'Member for'], true);
users.forEach(function (user, index) {
var link = document.createElement('a')
link.href = user.href;
link.appendChild(document.createTextNode(user.name));
var votes;
if (mayQueryVoteCounts) {
votes = document.createElement('a');
votes.addEventListener('click', queryVoteCounts.bind(user));
}
else {
votes = document.createElement('span');
}
votes.appendChild(document.createTextNode(user.votes));
var badges = [user.modBadges, user.editBadges, user.participationBadges].join(' / ');
var row = addRow(table, [index + 1, link, votes, user.score, user.rep, badges, user.memberFor]);
row.className = user.withdrawn ? 'withdrawn' : '';
});
document.querySelector('.question-status').appendChild(table);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment