GitHub Repo Size

Calculate repo size and inject into GitHub page

当前为 2021-02-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Repo Size
  3. // @namespace http://github.com/
  4. // @description Calculate repo size and inject into GitHub page
  5. // @icon https://github.githubassets.com/favicons/favicon.png
  6. // @version 0.1
  7. // @author Miraculous Owonubi
  8. // @grant GM_xmlhttpRequest
  9. // @match https://github.com/*
  10. // @require https://code.jquery.com/jquery-3.5.1.min.js
  11. // @connect tokei.rs
  12. // ==/UserScript==
  13. /* global $ */
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const CLOC_ID = "tokei-cloc";
  19. const DATA_URL = `https://tokei.rs/b1/github/${window.location.pathname.split('/').slice(1, 3).join('/')}`;
  20. const REPO_SUMMARY = $("#repo-content-pjax-container div.gutter-condensed.gutter-lg div.file-navigation div.flex-self-center");
  21. const CLOC_ELM = document.getElementById(CLOC_ID);
  22.  
  23. if (REPO_SUMMARY.length !== 0 && !CLOC_ELM) {
  24. function handleLoad(response, code) {
  25. if (response.response && typeof (code = response.response.code) === "number") {
  26. const bloq = $(`
  27. <a href="${DATA_URL}?category=lines" id="${CLOC_ID}" class="ml-3 link-gray-dark no-underline">
  28. <svg text="gray" class="octicon octicon-file-code" width="16" height="16" viewBox="0 0 16 16" version="1.1" aria-hidden="true">
  29. <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">
  30. </path>
  31. </svg>
  32. <strong>
  33. ${code.toString().replace(/(\d)(?=(\d{3})+$)/g, "$1,")}
  34. </strong>
  35. <span class="text-gray-light">lines of code</span>
  36. </a>
  37. `);
  38. REPO_SUMMARY.append(bloq);
  39. }
  40. }
  41.  
  42. GM_xmlhttpRequest({
  43. method: "GET",
  44. url: DATA_URL,
  45. responseType: "json",
  46. headers: { "Accept": "application/json"},
  47. onload: handleLoad,
  48. onerror: console.error,
  49. onabort: console.error,
  50. ontimeout: console.error,
  51. });
  52. }
  53. })();