Github 仓库大小

在 github 搜索和存储库页面上的存储库名称旁边添加存储库大小

目前为 2024-08-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Github Repo Size+
  3. // @name:zh-CN Github 仓库大小
  4. // @namespace https://github.com/qinwuyuan-cn
  5. // @description Adds the repo size next to the repo name on github search and repo pages
  6. // @description:zh-CN 在 github 搜索和存储库页面上的存储库名称旁边添加存储库大小
  7. // @version 0.1.2.9
  8. // @author mshll & 人民的勤务员 <toniaiwanowskiskr47@gmail.com>
  9. // @match *://github.com/search*
  10. // @match *://github.com/*/*
  11. // @grant none
  12. // @icon https://github.githubassets.com/pinned-octocat.svg
  13. // @license MIT
  14. // @source https://github.com/qinwuyuan-cn/UserScripts
  15.  
  16. // ==/UserScript==
  17.  
  18.  
  19. "use strict"
  20. //! Generate a new public access token from https://github.com/settings/tokens and insert it here
  21. //*Note: to be able to see the size of your private repos, you need to select the `repo` scope when generating the token
  22. const TOKEN = ""
  23. const getPageType = () => {
  24. const { pathname, search } = window.location
  25. const params = new URLSearchParams(search)
  26. const [, username, repo] = pathname.split("/")
  27. const q = params.get("q")?.toLocaleLowerCase()
  28. const type = params.get("type")?.toLocaleLowerCase()
  29. if (username && repo) return "repo"
  30. if (q && type === "code") return "code_search"
  31. if (q) return "search"
  32. }
  33. const addSizeToRepos = () => {
  34. const pageType = getPageType()
  35. // Get the repo selector based on the page type
  36. let repoSelector
  37. switch (pageType) {
  38. case "repo":
  39. repoSelector = "#repository-container-header strong a"
  40. break
  41. case "search":
  42. repoSelector = 'div[data-testid="results-list"] .search-title a'
  43. break
  44. case "code_search":
  45. repoSelector = 'div[data-testid="results-list"] .search-title a'
  46. break
  47. default:
  48. return
  49. }
  50. function extractPath(input) {
  51. const thirdSlashIndex = input.indexOf('/', input.indexOf('/', input.indexOf('/') + 1) + 1)
  52. if (thirdSlashIndex !== -1) {
  53. return input.substring(0, thirdSlashIndex)
  54. }
  55. return input
  56. }
  57. // Get all the repo links
  58. document.querySelectorAll(repoSelector).forEach(async (elem) => {
  59. // Get json data from github api to extract the size
  60. const tkn = TOKEN
  61. var href = elem.getAttribute("href")
  62. href = extractPath(href)
  63. console.log(href)
  64. const headers = tkn ? { authorization: `token ${tkn}` } : {}
  65.  
  66. const jsn = await (
  67. await fetch(`https://api.github.com/repos${href}`, {
  68. headers: headers,
  69.  
  70. })
  71. ).json()
  72. // If JSON failed to load, skip
  73. if (jsn.message) return
  74. // Get parent element to append the size to
  75. let parent = elem.parentElement
  76. if (pageType === "repo") {
  77. parent = elem.parentElement.parentElement
  78. }
  79. // Create the size container
  80. let sizeContainer = parent.querySelector(`#mshll-repo-size`)
  81. if (sizeContainer === null) {
  82. sizeContainer = document.createElement("span")
  83. sizeContainer.id = "mshll-repo-size"
  84. sizeContainer.classList.add("Label", "Label--info", "v-align-middle", "ml-1")
  85. sizeContainer.setAttribute("title", "Repository size")
  86. sizeContainer.innerText = "-"
  87. // Create the size icon
  88. let sizeSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg")
  89. sizeSVG.setAttribute("aria-hidden", "true")
  90. sizeSVG.setAttribute("viewBox", "-4 -4 22 22")
  91. sizeSVG.setAttribute("width", "16")
  92. sizeSVG.setAttribute("height", "16")
  93. sizeSVG.setAttribute("fill", "currentColor")
  94. sizeSVG.setAttribute("data-view-component", "true")
  95. sizeSVG.classList.add("octicon", "octicon-file-directory", "mr-1")
  96. let sizeSVGPath = document.createElementNS("http://www.w3.org/2000/svg", "path")
  97. sizeSVGPath.setAttribute("fill-rule", "evenodd")
  98. sizeSVGPath.setAttribute("d", "M1 3.5c0-.626.292-1.165.7-1.59.406-.422.956-.767 1.579-1.041C4.525.32 6.195 0 8 0c1.805 0 3.475.32 4.722.869.622.274 1.172.62 1.578 1.04.408.426.7.965.7 1.591v9c0 .626-.292 1.165-.7 1.59-.406.422-.956.767-1.579 1.041C11.476 15.68 9.806 16 8 16c-1.805 0-3.475-.32-4.721-.869-.623-.274-1.173-.62-1.579-1.04-.408-.426-.7-.965-.7-1.591Zm1.5 0c0 .133.058.318.282.551.227.237.591.483 1.101.707C4.898 5.205 6.353 5.5 8 5.5c1.646 0 3.101-.295 4.118-.742.508-.224.873-.471 1.1-.708.224-.232.282-.417.282-.55 0-.133-.058-.318-.282-.551-.227-.237-.591-.483-1.101-.707C11.102 1.795 9.647 1.5 8 1.5c-1.646 0-3.101.295-4.118.742-.508.224-.873.471-1.1.708-.224.232-.282.417-.282.55Zm0 4.5c0 .133.058.318.282.551.227.237.591.483 1.101.707C4.898 9.705 6.353 10 8 10c1.646 0 3.101-.295 4.118-.742.508-.224.873-.471 1.1-.708.224-.232.282-.417.282-.55V5.724c-.241.15-.503.286-.778.407C11.475 6.68 9.805 7 8 7c-1.805 0-3.475-.32-4.721-.869a6.15 6.15 0 0 1-.779-.407Zm0 2.225V12.5c0 .133.058.318.282.55.227.237.592.484 1.1.708 1.016.447 2.471.742 4.118.742 1.647 0 3.102-.295 4.117-.742.51-.224.874-.47 1.101-.707.224-.233.282-.418.282-.551v-2.275c-.241.15-.503.285-.778.406-1.247.549-2.917.869-4.722.869-1.805 0-3.475-.32-4.721-.869a6.327 6.327 0 0 1-.779-.406Z")
  99. sizeSVG.appendChild(sizeSVGPath)
  100. // Convert the size to human readable
  101. const sizes = ["B", "KB", "MB", "GB", "TB"]
  102. const size = jsn.size * 1024 // Github API returns size in KB so convert to bytes
  103. let i = parseInt(Math.floor(Math.log(size) / Math.log(1024)))
  104. const humanReadableSize = (size / Math.pow(1024, i)).toFixed(1) + " " + sizes[i]
  105. // Insert the size into the size container
  106. sizeContainer.innerHTML = `${humanReadableSize}`
  107. sizeContainer.prepend(sizeSVG)
  108. // Insert the size container into the DOM
  109. if (pageType === "code_search") {
  110. parent.style.direction = 'ltr'
  111. }
  112. parent.appendChild(sizeContainer)
  113. }
  114. })
  115. }
  116. window.addSizeToRepos = addSizeToRepos
  117. // Add the size to the repos on the page
  118. //addSizeToRepos()
  119. window.onload = function () {
  120. addSizeToRepos()
  121. }
  122. // Watch for URL changes
  123. let lastUrl = location.href
  124. new MutationObserver(() => {
  125. const url = location.href
  126. if (url !== lastUrl) {
  127. lastUrl = url
  128. setTimeout(function () {
  129. //NOTE - 此处增加延时了,就这样得了
  130. addSizeToRepos()
  131. }, 1500)
  132. }
  133.  
  134. }).observe(document, { subtree: true, childList: true })