Github Contributions Heatmap

Adds a heatmap to the Github contribution graph.

  1. // ==UserScript==
  2. // @name Github Contributions Heatmap
  3. // @description Adds a heatmap to the Github contribution graph.
  4. // @author Dorian MARCHAL
  5. // @namespace http://dorian-marchal.com
  6. // @include https://github.com/*
  7. // @version 2
  8. // @license GNU GPL v2
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. "use strict";
  13. /* jshint unused: false */
  14. /* jshint multistr: true */
  15. /* jshint newcap: false */
  16.  
  17.  
  18. var GithubHeatmap = {
  19.  
  20. COLDEST_HUE: 240,
  21. HOTTEST_HUE: 0,
  22. MAX_CONTRIBUTION_COUNT: 100,
  23.  
  24. css: "\
  25. .toggle-wrapper {\
  26. float: right;\
  27. position: relative;\
  28. top: -3px;\
  29. font-weight: normal;\
  30. margin-right: 8px;\
  31. }\
  32. .slide-toggle {\
  33. display: inline-block;\
  34. vertical-align: middle;\
  35. box-sizing: content-box;\
  36. margin: 2px 0;\
  37. padding: 0;\
  38. border: none;\
  39. height: 20px;\
  40. width: 34px;\
  41. cursor: pointer;\
  42. }\
  43. .slide-toggle input {\
  44. display: none;\
  45. }\
  46. .slide-toggle input + .slide-toggle-style {\
  47. position: relative;\
  48. width: 100%;\
  49. height: 100%;\
  50. border-radius: 50px;\
  51. background-color: #A3A3A3;\
  52. box-shadow: 0 0 2px 0px #555 inset;\
  53. transition-duration: 300ms;\
  54. }\
  55. .slide-toggle input + .slide-toggle-style:after {\
  56. content: \"\";\
  57. display: inline-block;\
  58. position: absolute;\
  59. left: 2px;\
  60. top: 2px;\
  61. height: 16px;\
  62. width: 16px;\
  63. border-radius: 50%;\
  64. background-color: #FFF;\
  65. box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);\
  66. transition-duration: 300ms;\
  67. }\
  68. .slide-toggle :checked + .slide-toggle-style {\
  69. background-color: #72CD52;\
  70. box-shadow: 0 0 2px 0px #498931 inset;\
  71. }\
  72. .slide-toggle :checked + .slide-toggle-style:after {\
  73. left: 16px;\
  74. }\
  75. ",
  76.  
  77. getCssColor: function(hue, saturation, luminosity) {
  78. return "hsl(" + hue + ", " + saturation + "%, " + luminosity + "%)";
  79. },
  80.  
  81. adjustFunction: function(x) {
  82. return Math.pow((x - 1), 3) + 1;
  83. },
  84.  
  85. adjustValue: function(x, max) {
  86.  
  87. if(x < 0 || max === 0) {
  88. return;
  89. }
  90.  
  91. //On repasse la valeur sur [0, 1] avant d'appliquer la fonction
  92. var value = x / max;
  93.  
  94. return Math.floor(GithubHeatmap.adjustFunction(value) * max);
  95. },
  96.  
  97. getFillColor: function(contributionCount) {
  98.  
  99. if(contributionCount === 0) {
  100. return null;
  101. }
  102.  
  103. contributionCount = Math.min(contributionCount, GithubHeatmap.MAX_CONTRIBUTION_COUNT);
  104.  
  105. var hue = Math.floor(((GithubHeatmap.adjustValue(contributionCount, GithubHeatmap.MAX_CONTRIBUTION_COUNT) /
  106. (GithubHeatmap.adjustValue(GithubHeatmap.MAX_CONTRIBUTION_COUNT, GithubHeatmap.MAX_CONTRIBUTION_COUNT))) *
  107. (GithubHeatmap.HOTTEST_HUE - GithubHeatmap.COLDEST_HUE)) + GithubHeatmap.COLDEST_HUE);
  108.  
  109. return GithubHeatmap.getCssColor(hue, 70, 50);
  110. },
  111.  
  112. addHeatmap: function() {
  113.  
  114. var $calendar = $("#contributions-calendar");
  115.  
  116. var $calDays = $(".js-calendar-graph-svg .day");
  117.  
  118. if($calDays.length > 0 && $calendar.attr("data-heatmap") !== "heatmap") {
  119.  
  120. $calendar.attr("data-heatmap", "heatmap");
  121.  
  122. var $toggle = $("<div class=\"toggle-wrapper\">Heatmap: <label class=\"slide-toggle\"><input checked=\"checked\" type=\"checkbox\"><div class=\"slide-toggle-style\"></div></label></div>", {
  123. });
  124.  
  125. $toggle.find("input").on("change", function() {
  126.  
  127. GithubHeatmap.toggleHeatmap();
  128. });
  129.  
  130. $("#contributions-calendar").siblings("h3").append($toggle);
  131.  
  132. $calDays.each(function() {
  133.  
  134. var contributionCount = parseInt(this.getAttribute("data-count"));
  135.  
  136. if(contributionCount > 0) {
  137.  
  138. console.log(this);
  139. this.setAttribute("data-fill", this.style.fill);
  140. this.style.fill = GithubHeatmap.getFillColor(contributionCount);
  141. }
  142. });
  143.  
  144. var contributionCount = 1;
  145.  
  146. $(".contrib-legend li").each(function() {
  147.  
  148. this.setAttribute("data-fill", $(this).css("background-color"));
  149. $(this).css("background-color", GithubHeatmap.getFillColor(contributionCount));
  150. contributionCount += 15;
  151. });
  152. }
  153. },
  154.  
  155. toggleHeatmap: function() {
  156.  
  157. $(".js-calendar-graph-svg .day").each(function() {
  158. var fill = this.getAttribute("data-fill");
  159. this.setAttribute("data-fill", this.style.fill);
  160. this.style.fill = fill;
  161. });
  162.  
  163. $(".contrib-legend li").each(function() {
  164. var fill = this.getAttribute("data-fill");
  165. this.setAttribute("data-fill", $(this).css("background-color"));
  166. $(this).css("background-color", fill);
  167. });
  168. },
  169.  
  170. init: function() {
  171.  
  172. $("head").append("<style type='text/css' >" + GithubHeatmap.css + "</style>");
  173. $("body").append();
  174. GithubHeatmap.addHeatmap();
  175.  
  176. var target = document.querySelector(".site");
  177.  
  178. var observer = new MutationObserver(function(mutations) {
  179. mutations.forEach(function(mutation) {
  180. GithubHeatmap.addHeatmap();
  181. });
  182. });
  183.  
  184. observer.observe(target, { subtree: true, childList: true });
  185. }
  186.  
  187. };
  188.  
  189. GithubHeatmap.init();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址