Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name stackexchange reputation display
- // @namespace stackexchange
- // @version 0.1.6
- // @description obtain user's rating
- // @author omegastripes
- // @include http://stackoverflow.com/*
- // @include http://meta.stackoverflow.com/*
- // @include http://superuser.com/*
- // @include http://meta.superuser.com/*
- // @include http://stackexchange.com/*
- // @include http://meta.stackexchange.com/*
- // @include http://*.stackexchange.com/*
- // @include http://ux.stackexchange.com/
- // @run-at document-end
- // @grant none
- // ==/UserScript==
- if (!String.prototype.includes) {
- String.prototype.includes = function (arg) {
- return !!~this.indexOf(arg);
- };
- }
- //console.log(String.prototype.includes);
- if (!Math.log10) {
- Math.log10 = function (arg) {
- return Math.log(arg) / Math.LN10;
- };
- }
- //console.log(Math.log10);
- var ancNodes = document.getElementsByTagName('a');
- //var l = ancNodes.length;
- //var h = ancNodes[0].href;
- //var p = ancNodes[0].pathname;
- var usrNodes = [];
- var reg = /users\/\d+\/.+/i;
- for (var i = 0; i < ancNodes.length; i++) {
- if (reg.test(ancNodes[i].pathname) && ancNodes[i].search === '') {
- usrNodes.push(ancNodes[i]);
- //console.log('url added: ' + ancNodes[i].href);
- }
- }
- var myUrl = getMyAccUrl();
- //console.log('my link : ' + myUrl);
- for (var i in usrNodes) {
- var usrNode = usrNodes[i];
- var usrHref = usrNode.href;
- var usrUrl;
- //console.log('pelt: ' + usrNode.pathname + ' | ' + usrNode.parentNode.tagName + ' (' + usrNode.parentNode.className + ') | ' + usrNode.parentNode.parentNode.tagName + ' (' + usrNode.parentNode.parentNode.className + ')');
- if (usrNode.parentNode.id == 'tabs') continue;
- //console.log(usrNode);
- if (usrNode.parentNode.className.includes('avatar')) continue;
- if (usrNode.className.includes('topbar')) continue;
- if (usrNode.className.includes('icon-help')) continue;
- if (usrHref == myUrl) {
- usrUrl = usrHref + '?tab=profile';
- } else {
- usrUrl = usrHref;
- }
- //console.log('get rep by url: ' + usrUrl);
- putRepToNode(usrNode, usrUrl, makeup);
- }
- function putRepToNode(node, url, show) {
- var xhr = new XMLHttpRequest();
- //console.log(xhr);
- xhr.onreadystatechange = function() {
- if (xhr.readyState==4 && xhr.status==200) {
- //console.log('xhr ready');
- var res = xhr.responseText;
- var tmp = res.split('<div class="reputation" title="reputation">', 2);
- var tmp2, tmp3, repValue;
- if (tmp.length == 2) {
- tmp2 = tmp[1].split('<', 1);
- repValue = tmp2[0].trim().replace(/\D/g, '');
- } else {
- tmp = res.split('<div class="reputation">', 2);
- tmp2 = tmp[1].split('?tab=reputation">', 2);
- tmp3 = tmp2[1].split('<', 1);
- repValue = tmp3[0].trim().replace(/\D/g, '');
- }
- //console.log('got rep val: ' + repValue);
- var repLog = Math.round(Math.log10(repValue) * 10);
- //console.log('computed rep = ' + repLog + ' for ' + url);
- show(node, repValue, repLog);
- }
- };
- xhr.open('GET', url, true);
- xhr.send();
- }
- function makeup(prtNode, repValue, repLog) {
- var hueColor = repLog;
- if (hueColor > 60) hueColor = 60;
- var rgbColor = HSVtoRgb(hueColor * 4 - 30, 25, 95);
- var spanNode = document.createElement('span');
- var textNode = document.createTextNode(repLog);
- spanNode.appendChild(textNode);
- prtNode.appendChild(spanNode);
- if (!prtNode.title) prtNode.title = '' + repValue + ' rep';
- spanNode.style.margin = '3px';
- spanNode.style.paddingLeft = '2px';
- spanNode.style.paddingRight = '2px';
- spanNode.style.borderRadius = '5px';
- spanNode.style.border = '1px solid grey';
- spanNode.style.backgroundColor = '#' + rgbColor;
- spanNode.style.color = '#000';
- }
- function getMyAccUrl() {
- // if logged in search for tag
- // <a href="/usrNodes/000000/abcdefghijk" class="profile-me js-gps-track" data-gps-track="profile_summary.click()">
- // retrieve the link to own acc
- // parsing of the own acc differs from others
- var ancNodes = document.getElementsByTagName('a');
- var reg = /<a href=".*?" class=".*?profile-me.*?".*?>/i;
- for (var i = 0; i < ancNodes.length; i++) {
- if (reg.test(ancNodes[i].outerHTML)) {
- return ancNodes[i].href;
- }
- }
- }
- function HSVtoRgb(H, S, V) {
- var f, p, q , t, lH, R, G, B;
- S = (S > 1 )? S / 100 : S;
- V = (V > 1)? V / 100 : V;
- lH = parseInt(H / 60);
- f = H / 60 - lH;
- p = V * (1 - S);
- q = V * (1 - S * f);
- t = (1 - (1 - f) * S);
- switch (lH) {
- case 0: R = V; G = t; B = p; break;
- case 1: R = q; G = V; B = p; break;
- case 2: R = p; G = V; B = t; break;
- case 3: R = p; G = q; B = V; break;
- case 4: R = t; G = p; B = V; break;
- case 5: R = V; G = p; B = q; break;
- }
- return parseInt(R * 16).toString(16) + parseInt(G * 16).toString(16) + parseInt(B * 16).toString(16);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement