GitHub RepoSize

show repo's size

  1. // ==UserScript==
  2. // @name GitHub RepoSize
  3. // @namespace http://js.zombie110year.top/
  4. // @version 0.2.0
  5. // @description show repo's size
  6. // @author zombie110year@outlook.com
  7. // @match https://github.com/*/*
  8. // @grant none
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. const ICON_DATABASE = `<svg t="1566195284663" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1131" width="16" height="16"><path d="M894.030769 177.230769c0-74.830769-171.323077-135.876923-382.030769-135.876923S129.969231 102.4 129.969231 177.230769v47.261539c0 74.830769 171.323077 135.876923 382.030769 135.876923s382.030769-61.046154 382.030769-135.876923V177.230769zM129.969231 334.769231c0 59.076923 171.323077 106.338462 382.030769 106.338461S894.030769 393.846154 894.030769 334.769231v96.492307c0 74.830769-171.323077 135.876923-382.030769 135.876924S129.969231 506.092308 129.969231 431.261538V334.769231z m0 0c0 59.076923 171.323077 106.338462 382.030769 106.338461S894.030769 393.846154 894.030769 334.769231v96.492307c0 74.830769-171.323077 135.876923-382.030769 135.876924S129.969231 506.092308 129.969231 431.261538V334.769231z m0 206.769231c0 59.076923 171.323077 106.338462 382.030769 106.338461s382.030769-47.261538 382.030769-106.338461v96.492307c0 74.830769-171.323077 135.876923-382.030769 135.876923s-382.030769-59.076923-382.030769-133.907692v-98.461538z m0 208.738461c0 59.076923 171.323077 106.338462 382.030769 106.338462s382.030769-47.261538 382.030769-106.338462V846.769231c0 74.830769-171.323077 135.876923-382.030769 135.876923S129.969231 921.6 129.969231 846.769231v-96.492308z" p-id="1132" fill="#515151"></path></svg>`;
  14. const SESSION_STORAGE_KEY = "api_fetched";
  15. parseURL()
  16. .then(obj => {
  17. return `https://api.github.com/repos/${obj.owner}/${obj.repo}`;
  18. })
  19. .then(async (url) => {
  20. // cache repo info
  21. if (sessionStorage.getItem(SESSION_STORAGE_KEY) === null) {
  22. let data = await fetch(url).then(resp => resp.json());
  23. sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(data));
  24. }
  25. })
  26. .then(main);
  27.  
  28. /**
  29. * main processing, **assume api info has been cached in sessionStorage**.
  30. *
  31. * 1. reposize
  32. */
  33. async function main() {
  34. let status = JSON.parse(sessionStorage.getItem(SESSION_STORAGE_KEY));
  35. showRepoSize(status["size"]);
  36. }
  37.  
  38. /**
  39. * add repo's size infomation into summary list
  40. *
  41. * @param {number} size kilobytes
  42. */
  43. async function showRepoSize(size) {
  44. let unit = "KB";
  45. let display_size = -1;
  46. if (size < 1024) {
  47. display_size = size;
  48. unit = "KB";
  49. } else if (size < 1048576) {
  50. display_size = (size / 1024).toFixed(3);
  51. unit = "MB";
  52. } else {
  53. display_size = (size / 1048576).toFixed(3);
  54. unit = "GB";
  55. }
  56. let ul = document.querySelector(".numbers-summary");
  57. let li = document.createElement("li");
  58. li.innerHTML = `<a href="${location.pathname}">${ICON_DATABASE} <span class="num text-emphasized">${display_size}</span> ${unit}</a>`;
  59. ul.appendChild(li);
  60. }
  61.  
  62. /**
  63. * get repo's owner and name to fetch GitHub's api
  64. */
  65. async function parseURL() {
  66. let pattern = RegExp("/([^/]+)/([^/]+)/?");
  67. let paths = pattern.exec(location.pathname);
  68. let obj = {
  69. owner: paths[1],
  70. repo: paths[2]
  71. }
  72. return obj;
  73. }
  74. })();

QingJ © 2025

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