Skip to content

Instantly share code, notes, and snippets.

@balthazar
Created August 3, 2014 11:11
Show Gist options
  • Save balthazar/ab1f0e5a883549137af1 to your computer and use it in GitHub Desktop.
Save balthazar/ab1f0e5a883549137af1 to your computer and use it in GitHub Desktop.
Appends the accepted answer rate on each answer in StackExchange network
// ==UserScript==
// @name Answer Accept Rate
// @version 0.1
// @description Get the answer accept rate of a user on StackExchange
// @namespace apercu.io
// @author Balthazar Gronon
// @license MIT
// @include http://stackoverflow.com/*
// @include http://ux.stackexchange.com/*
// @include http://security.stackexchange.com/*
// @include http://softwarerecs.stackexchange.com/*
// @include http://discuss.area51.stackexchange.com/*
// @include http://stackapps.com/*
// @include http://serverfault.com/*
// @include http://superuser.com/*
// @include http://meta.stackoverflow.com/*
// @include http://meta.serverfault.com/*
// @include http://meta.superuser.com/*
// @include http://.stackexchange.com/
// @include http://askubuntu.com/*
// @include http://meta.askubuntu.com/*
// @include http://mathoverflow.net/*
// @include http://meta.mathoverflow.net/*
// @exclude http://chat./
// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js
function with_jquery(f) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '(' + f.toString() + ')(jQuery)';
document.body.appendChild(script);
};
with_jquery(function($) {
function appendRate (rate, container) {
if (rate > 80) {
userclass = 'accept-answer-link';
} else if (rate > 35) {
userclass = 'cool';
} else if (rate > 20) {
userclass = 'warm';
} else if (rate > 5) {
userclass = 'hot';
} else {
userclass = 'supernova';
}
percent = (rate >= 0) ? '%' : '';
accept_rate_text = '<br class="cbt">\n<div class="accept-rate ' + userclass + '">' + rate + percent + ' accept rate</div>\n';
container.append(accept_rate_text);
$('.user-info').css('height','auto');
}
function recursiveAnswers (total, accepts, url, page, container) {
$.get(url + page, function(data) {
data.items.map(function (e) {
total++;
if (e.is_accepted) {
accepts++;
}
});
if (!data.has_more) {
var res = accepts * 100 / total;
return appendRate(res.toFixed(2), container);
}
page++;
recursiveAnswers(total, accepts, url, page, container);
});
}
function emulateAcceptedRate (userid, container) {
var answersurl = location.protocol + '//api.stackexchange.com/2.2/users/' + userid + '/answers?pagesize=100&site=' + location.host + '&page=';
recursiveAnswers(0, 0, answersurl, 1, container);
}
post = location.pathname.match(/questions\/(\d+)\D/i);
if (!post) return;
$('#answers .answer .post-signature').each(function () {
var id = $(this).find('a')[0].href.match(/\/users\/(\d+)(?:\/|$)/)[1];
var container = $(this).children();
emulateAcceptedRate (id, container);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment