Skip to content

Instantly share code, notes, and snippets.

@falconkirtaran
Forked from anonymous/acceptrate.user.js
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falconkirtaran/2cb459739024c7eb3c14 to your computer and use it in GitHub Desktop.
Save falconkirtaran/2cb459739024c7eb3c14 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Home Site
// @version 0.1
// @description Display a user's "home" Stack Exchange site (that which they have the most reputation on) and their accept rate
// @namespace mkl9FF3w
// @author Falcon Darkstar Momot
// @license GNU GPL v3 or later (http://www.gnu.org/copyleft/gpl.html)
// @grant none
// @include http://stackoverflow.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://stackapps.com/*
// @include http://.stackexchange.com/
// @include http://askubuntu.com/*
// @include http://meta.askubuntu.com/*
// @include http://answers.onstartups.com/*
// @include http://meta.answers.onstartups.com/*
// @include http://mathoverflow.net/*
// @include http://meta.mathoverflow.net/*
// @include http://discuss.area51.stackexchange.com/*
// @exclude http://chat./
// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js
// @require //ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==
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($) {
console.debug("In home site / accept rate userscript");
// Ensure we are looking at a question
post = location.pathname.match(/questions\/(\d+)\D/i);
if (!post) return;
console.debug("Found question");
// Find the user ID who wrote the question
userid = $(".post-signature.owner > div > div.user-details > a")[0].href.match(/\/users\/(\d+)(?:\/|$)/)[1];
// Many SE users have negative IDs; none of them are interesting to us
if (userid < 1) return;
console.debug("Found local user");
// Get user's info from the SE API
apiurl = location.protocol + "//api.stackexchange.com/2.2/users/" + userid + "?site=" + location.host;
console.debug("Querying API [" + apiurl + "]");
$.get(apiurl, function(o) {
console.debug("Got local user stats");
var user = o.items[0];
if (!user) return;
if ('account_id' in user) {
console.debug("Got network user ID");
ident_apiurl = location.protocol + "//api.stackexchange.com/2.2/users/" + user['account_id'] + "/associated?types=main_site";
ident_rep = 0;
ident_name = "";
ident_icon = "";
console.debug("Querying API [" + ident_apiurl + "]");
$.get(ident_apiurl, function(o) {
console.debug("Queried API");
o['items'].map(function(u) {
console.debug("Parsing network user's local account on [" + u['site_name'] + "]");
if (('reputation' in u) && ('site_url' in u) && ('site_name' in u) && (u['reputation'] > ident_rep)) {
if (("http://" + location.host == u['site_url']) || ("https://" + location.host == u['site_url'])) {
console.debug("Ignoring current site");
return;
}
console.debug("Highest so far: [" + u['reputation'] + "] on [" + u['site_name'] + "]");
ident_rep = u['reputation'];
ident_name = u['site_name'];
ident_icon = u['site_url'] + "/favicon.ico";
}
return;
});
console.debug("Finished processing network accounts")
console.debug("Highest was [" + ident_rep + "] on [" + ident_name + "] with icon at [" + ident_icon + "]");
if (ident_icon != "") {
ident_icon_text = '<br class="cbt">\n<div class="reputation-score" dir="ltr" title="' + ident_name + '"><b>' + ident_rep + '</b>' + '<img src="' + ident_icon + '" alt="' + ident_name + '" /></div>\n';
$(".post-signature.owner .user-details").append(ident_icon_text);
$('.user-info').css('height','auto');
}
return;
});
}
if ('accept_rate' in user) {
// TODO: These percentages might not be exactly as
// they used to be. Try to verify them later.
if (user['accept_rate'] > 80) {
userclass = "accept-answer-link";
} else if (user['accept_rate'] > 35) {
userclass = "cool";
} else if (user['accept_rate'] > 20) {
userclass = "warm";
} else if (user['accept_rate'] > 5) {
userclass = "hot";
} else {
userclass = "supernova";
}
accept_rate_text = '<br class="cbt">\n<div class="accept-rate ' + userclass + '">' + user['accept_rate'] + '% accept rate</div>\n';
console.log(accept_rate_text)
$(".post-signature.owner > div").append(accept_rate_text);
$('.user-info').css('height','auto');
}
return;
});
return;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment