HN scores highlighter

Highlights scores with different colors

  1. // ==UserScript==
  2. // @name HN scores highlighter
  3. // @namespace http://elamperti.com/
  4. // @version 0.1
  5. // @description Highlights scores with different colors
  6. // @author Enrico Lamperti
  7. // @match https://news.ycombinator.com/
  8. // @match https://news.ycombinator.com/news*
  9. // @grant none
  10. // ==/UserScript==
  11. /* jshint -W097 */
  12. 'use strict';
  13.  
  14. var thresholds = [
  15. 20,
  16. 70,
  17. 200,
  18. 500,
  19. 800
  20. ];
  21. var defaultColor = '#CCCCCC';
  22. var colors = [
  23. '#666666', // 20
  24. '#BD9910', // 70
  25. '#EA7C07', // 200
  26. '#FF0000', // 500
  27. '#0000FF', // 800
  28. ];
  29.  
  30.  
  31. function parseScore(elem) {
  32. return parseInt(elem.innerHTML.split(" ")[0]);
  33. };
  34.  
  35. function getColorForScore(score) {
  36. var color = defaultColor;
  37. for (var i=0; i < thresholds.length; i++) {
  38. if (score >= thresholds[i]) {
  39. color = colors[i];
  40. }
  41. }
  42. return color;
  43. }
  44.  
  45. var items = document.querySelectorAll(".score");
  46. for (var i=0; i < items.length; i++) {
  47. items[i].style.fontWeight = 'bold';
  48. items[i].style.color= getColorForScore(parseScore(items[i]));
  49. }