Advertisement
omegastripes

Tampermonkey stackexchange reputation display

May 10th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         stackexchange reputation display
  3. // @namespace    stackexchange
  4. // @version      0.1.6
  5. // @description  obtain user's rating
  6. // @author       omegastripes
  7. // @include      http://stackoverflow.com/*
  8. // @include      http://meta.stackoverflow.com/*
  9. // @include      http://superuser.com/*
  10. // @include      http://meta.superuser.com/*
  11. // @include      http://stackexchange.com/*
  12. // @include      http://meta.stackexchange.com/*
  13. // @include      http://*.stackexchange.com/*
  14. // @include      http://ux.stackexchange.com/
  15. // @run-at       document-end
  16. // @grant        none
  17. // ==/UserScript==
  18.  
  19. if (!String.prototype.includes) {
  20.     String.prototype.includes = function (arg) {
  21.         return !!~this.indexOf(arg);
  22.     };
  23. }
  24. //console.log(String.prototype.includes);
  25.  
  26. if (!Math.log10) {
  27.     Math.log10 = function (arg) {
  28.         return Math.log(arg) / Math.LN10;
  29.     };
  30. }
  31. //console.log(Math.log10);
  32.  
  33. var ancNodes = document.getElementsByTagName('a');
  34. //var l = ancNodes.length;
  35. //var h = ancNodes[0].href;
  36. //var p = ancNodes[0].pathname;
  37.  
  38. var usrNodes = [];
  39. var reg = /users\/\d+\/.+/i;
  40. for (var i = 0; i < ancNodes.length; i++) {
  41.     if (reg.test(ancNodes[i].pathname) && ancNodes[i].search === '') {
  42.         usrNodes.push(ancNodes[i]);
  43.         //console.log('url added: ' + ancNodes[i].href);
  44.     }
  45. }
  46.  
  47. var myUrl = getMyAccUrl();
  48. //console.log('my link : ' + myUrl);
  49.  
  50. for (var i in usrNodes) {
  51.     var usrNode = usrNodes[i];
  52.     var usrHref = usrNode.href;
  53.     var usrUrl;
  54.     //console.log('pelt: ' + usrNode.pathname + ' | ' + usrNode.parentNode.tagName + ' (' + usrNode.parentNode.className + ') | ' + usrNode.parentNode.parentNode.tagName + ' (' + usrNode.parentNode.parentNode.className + ')');
  55.     if (usrNode.parentNode.id == 'tabs') continue;
  56.     //console.log(usrNode);
  57.     if (usrNode.parentNode.className.includes('avatar')) continue;
  58.     if (usrNode.className.includes('topbar')) continue;
  59.     if (usrNode.className.includes('icon-help')) continue;
  60.     if (usrHref == myUrl) {
  61.         usrUrl = usrHref + '?tab=profile';
  62.     } else {
  63.         usrUrl = usrHref;
  64.     }
  65.     //console.log('get rep by url: ' + usrUrl);
  66.     putRepToNode(usrNode, usrUrl, makeup);
  67. }
  68.  
  69. function putRepToNode(node, url, show) {
  70.     var xhr = new XMLHttpRequest();
  71.     //console.log(xhr);
  72.     xhr.onreadystatechange = function() {
  73.         if (xhr.readyState==4 && xhr.status==200) {
  74.             //console.log('xhr ready');
  75.             var res = xhr.responseText;
  76.             var tmp = res.split('<div class="reputation" title="reputation">', 2);
  77.             var tmp2, tmp3, repValue;
  78.             if (tmp.length == 2) {
  79.                 tmp2 = tmp[1].split('<', 1);
  80.                 repValue = tmp2[0].trim().replace(/\D/g, '');
  81.             } else {
  82.                 tmp = res.split('<div class="reputation">', 2);
  83.                 tmp2 = tmp[1].split('?tab=reputation">', 2);
  84.                 tmp3 = tmp2[1].split('<', 1);
  85.                 repValue = tmp3[0].trim().replace(/\D/g, '');
  86.             }
  87.             //console.log('got rep val: ' + repValue);
  88.             var repLog = Math.round(Math.log10(repValue) * 10);
  89.             //console.log('computed rep = ' + repLog + ' for ' + url);
  90.             show(node, repValue, repLog);
  91.         }
  92.     };
  93.     xhr.open('GET', url, true);
  94.     xhr.send();
  95. }
  96.  
  97. function makeup(prtNode, repValue, repLog) {
  98.     var hueColor = repLog;
  99.     if (hueColor > 60) hueColor = 60;
  100.     var rgbColor = HSVtoRgb(hueColor * 4 - 30, 25, 95);
  101.     var spanNode = document.createElement('span');
  102.     var textNode = document.createTextNode(repLog);
  103.     spanNode.appendChild(textNode);
  104.     prtNode.appendChild(spanNode);
  105.     if (!prtNode.title) prtNode.title = '' + repValue + ' rep';
  106.     spanNode.style.margin = '3px';
  107.     spanNode.style.paddingLeft = '2px';
  108.     spanNode.style.paddingRight = '2px';
  109.     spanNode.style.borderRadius = '5px';
  110.     spanNode.style.border = '1px solid grey';
  111.     spanNode.style.backgroundColor = '#' + rgbColor;
  112.     spanNode.style.color = '#000';
  113. }
  114.  
  115. function getMyAccUrl() {
  116.     // if logged in search for tag
  117.     // <a href="/usrNodes/000000/abcdefghijk" class="profile-me js-gps-track" data-gps-track="profile_summary.click()">
  118.     // retrieve the link to own acc
  119.     // parsing of the own acc differs from others
  120.     var ancNodes = document.getElementsByTagName('a');
  121.     var reg = /<a href=".*?" class=".*?profile-me.*?".*?>/i;
  122.     for (var i = 0; i < ancNodes.length; i++) {
  123.         if (reg.test(ancNodes[i].outerHTML)) {
  124.             return ancNodes[i].href;
  125.         }
  126.     }
  127. }
  128.  
  129. function HSVtoRgb(H, S, V) {
  130.     var f, p, q , t, lH, R, G, B;
  131.     S = (S > 1 )? S / 100 : S;
  132.     V = (V > 1)? V / 100 : V;
  133.     lH = parseInt(H / 60);
  134.     f = H / 60 - lH;
  135.     p = V * (1 - S);
  136.     q = V * (1 - S * f);
  137.     t = (1 - (1 - f) * S);
  138.     switch (lH) {
  139.         case 0: R = V; G = t; B = p; break;
  140.         case 1: R = q; G = V; B = p; break;
  141.         case 2: R = p; G = V; B = t; break;
  142.         case 3: R = p; G = q; B = V; break;
  143.         case 4: R = t; G = p; B = V; break;
  144.         case 5: R = V; G = p; B = q; break;
  145.     }
  146.     return parseInt(R * 16).toString(16) + parseInt(G * 16).toString(16) + parseInt(B * 16).toString(16);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement