GitHub Repo Size

Calculate repo size and inject into GitHub page

  1. // ==UserScript==
  2. // @name GitHub Repo Size
  3. // @namespace https://gist.github.com/miraclx/aad03f43fe8ac85682b0243f4f242f0d
  4. // @description Calculate repo size and inject into GitHub page
  5. // @icon https://github.githubassets.com/favicons/favicon.png
  6. // @version 0.2.0
  7. // @author miraclx
  8. // @license Apache-2.0
  9. // @supportURL https://gist.github.com/miraclx/aad03f43fe8ac85682b0243f4f242f0d
  10. // @grant GM_xmlhttpRequest
  11. // @match https://github.com/*
  12. // @require https://code.jquery.com/jquery-3.5.1.min.js
  13. // @connect tokei.rs
  14. // ==/UserScript==
  15. /* global $ */
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. const DATA_URL = `https://tokei.rs/b1/github/${window.location.pathname.split('/').slice(1, 3).join('/')}`;
  21. const CLOC_ID = "tokei-cloc";
  22. const CLOC_ELM = document.getElementById(CLOC_ID);
  23. const REPO_SUMMARY = $("#repo-content-pjax-container div.Layout div.file-navigation div.flex-items-center");
  24. if (!CLOC_ELM) {
  25. function handleLoad(response, code) {
  26. if (response.response && typeof (code = response.response.code) === "number") {
  27. if (REPO_SUMMARY.length !== 0) {
  28. const bloq = $(`
  29. <a href="${DATA_URL}?category=code" title="Click to view badge in new tab" id="${CLOC_ID}" target="_blank" class="ml-3 Link--primary no-underline">
  30. <svg text="gray" class="octicon octicon-file-code" width="16" height="16" viewBox="0 0 16 16" version="1.1" aria-hidden="true">
  31. <path fill-rule="evenodd" d="M8.5 1H1c-.55 0-1 .45-1 1v12c0 .55.45 1 1 1h10c.55 0 1-.45 1-1V4.5L8.5 1zM11 14H1V2h7l3 3v9zM5 6.98L3.5 8.5 5 10l-.5 1L2 8.5 4.5 6l.5.98zM7.5 6L10 8.5 7.5 11l-.5-.98L8.5 8.5 7 7l.5-1z">
  32. </path>
  33. </svg>
  34. <strong>
  35. ${code.toString().replace(/(\d)(?=(\d{3})+$)/g, "$1,")}
  36. </strong>
  37. <span class="text-gray-light">lines of code</span>
  38. </a>
  39. `);
  40. REPO_SUMMARY.append(bloq);
  41. }
  42. }
  43. }
  44.  
  45. GM_xmlhttpRequest({
  46. method: "GET",
  47. url: DATA_URL,
  48. responseType: "json",
  49. headers: { "Accept": "application/json" },
  50. onload: handleLoad,
  51. onerror: console.error,
  52. onabort: console.error,
  53. ontimeout: console.error,
  54. });
  55. }
  56. })();