Github 仓库大小

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

当前为 2024-08-30 提交的版本,查看 最新版本

  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.38
  8. // @author mshll & 人民的勤务员 <toniaiwanowskiskr47@gmail.com>
  9. // @match *://github.com/search*
  10. // @match *://github.com/*/*
  11. // @match *://github.com/*?tab=repositories*
  12. // @grant none
  13. // @grant GM_getValue
  14. // @grant GM_setValue
  15. // @grant GM_addStyle
  16. // @grant GM_registerMenuCommand
  17. // @grant none
  18. // @icon https://github.githubassets.com/pinned-octocat.svg
  19. // @license MIT
  20. // @source https://github.com/qinwuyuan-cn/UserScripts
  21. // @supportURL https://github.com/ChinaGodMan/UserScripts/issues
  22. // @homepageURL https://github.com/ChinaGodMan/UserScripts
  23. // ==/UserScript==
  24. "use strict"
  25. //! Generate a new public access token from https://github.com/settings/tokens and insert it here
  26. //*Note: to be able to see the size of your private repos, you need to select the `repo` scope when generating the token
  27. let TOKEN = GM_getValue('githubToken', "")
  28. GM_addStyle(`
  29. .modal-overlay{position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.5);display:flex;justify-content:center;align-items:center;z-index:1000;}
  30. .modal-content{background:white;padding:20px;border-radius:8px;width:400px;box-shadow:0 4px 15px rgba(0,0,0,0.2);position:relative;}
  31. .modal-title{margin:0 0 10px 0;font-size:20px;}
  32. .modal-description{margin-bottom:20px;font-size:14px;color:#666;}
  33. .modal-description a{color:#007bff;text-decoration:underline;}
  34. #github-token-input{width:100%;padding:8px;border:1px solid #ccc;border-radius:4px;margin-bottom:20px;font-size:14px;}
  35. #save-token{background-color:#28a745;color:white;border:none;padding:10px 20px;cursor:pointer;border-radius:4px;margin-right:10px;}
  36. #cancel-token{background-color:#dc3545;color:white;border:none;padding:10px 20px;cursor:pointer;border-radius:4px;}
  37. `)
  38. function createModal() {
  39. const modalHTML = `
  40. <div class="modal-overlay">
  41. <div class="modal-content">
  42. <h2 class="modal-title">Set GitHub Token</h2>
  43. <p class="modal-description">
  44. Enter your GitHub personal access token with "repo" scope.
  45. <a href="https://github.com/settings/tokens/new?description=GitHub%20Repo%20Size%20UserScript&scopes=repo" target="_blank" rel="noopener noreferrer">
  46. Click here to create a new token
  47. </a>
  48. </p>
  49. <input type="text" id="github-token-input" placeholder="Enter your GitHub personal access token">
  50. <button id="save-token">Save</button>
  51. <button id="cancel-token" class="cancel">Cancel</button>
  52. </div>
  53. </div>
  54. `
  55. const modalContainer = document.createElement('div')
  56. modalContainer.innerHTML = modalHTML
  57. document.body.appendChild(modalContainer)
  58. const input = document.getElementById('github-token-input')
  59. input.value = GM_getValue('githubToken', '')
  60. document.getElementById('save-token').addEventListener('click', () => {
  61. const token = input.value.trim()
  62. if (token) {
  63. GM_setValue('githubToken', token)
  64. modalContainer.remove()
  65. TOKEN = token
  66. }
  67. })
  68. document.getElementById('cancel-token').addEventListener('click', () => modalContainer.remove())
  69. }
  70. GM_registerMenuCommand('Set GitHub Token', function () {
  71. createModal()
  72. })
  73. const getPageType = () => {
  74. const { pathname, search } = window.location
  75. const params = new URLSearchParams(search)
  76. const [, username, repo] = pathname.split("/")
  77. const q = params.get("q")?.toLocaleLowerCase()
  78. const type = params.get("type")?.toLocaleLowerCase()
  79. if (window.location.pathname.split('/').pop() === "repositories") return "list-view-container"
  80. if (window.location.href.includes("?tab=repositories")) return "user-repositories"
  81. if (username && repo) return "repo"
  82. if (q && type === "code") return "code_search"
  83. if (q) return "search"
  84. }
  85. const addSizeToRepos = () => {
  86. const pageType = getPageType()
  87. // Get the repo selector based on the page type
  88. let repoSelector
  89. switch (pageType) {
  90. case "repo"://仓库详情界面
  91. repoSelector = "#repository-container-header strong a"
  92. break
  93. case "list-view-container"://ORG下的仓库列表
  94. repoSelector = 'div[data-testid="list-view-item-title-container"] h4 a'
  95. break
  96. case "user-repositories"://用户资料页面的仓库TAB
  97. repoSelector = '#user-repositories-list h3 a'
  98. break
  99. case "search"://搜索
  100. repoSelector = 'div[data-testid="results-list"] .search-title a'
  101. break
  102. case "code_search"://代码搜索
  103. repoSelector = 'div[data-testid="results-list"] .search-title a'
  104. break
  105. default:
  106. return
  107. }
  108. function extractPath(input) {
  109. const thirdSlashIndex = input.indexOf('/', input.indexOf('/', input.indexOf('/') + 1) + 1)
  110. if (thirdSlashIndex !== -1) {
  111. return input.substring(0, thirdSlashIndex)
  112. }
  113. return input
  114. }
  115. // Get all the repo links
  116. document.querySelectorAll(repoSelector).forEach(async (elem) => {
  117. // Get json data from github api to extract the size
  118. const tkn = TOKEN
  119. var href = elem.getAttribute("href")
  120. href = extractPath(href)
  121. console.log(href)
  122. const headers = tkn ? { authorization: `token ${tkn}` } : {}
  123. const jsn = await (
  124. await fetch(`https://api.github.com/repos${href}`, {
  125. headers: headers,
  126. })
  127. ).json()
  128. // If JSON failed to load, skip
  129. if (jsn.message) return
  130. // Get parent element to append the size to
  131. let parent = elem.parentElement
  132. if (pageType === "repo") {
  133. parent = elem.parentElement.parentElement
  134. }
  135. // Create the size container
  136. let sizeContainer = parent.querySelector(`#mshll-repo-size`)
  137. if (sizeContainer === null) {
  138. sizeContainer = document.createElement("span")
  139. sizeContainer.id = "mshll-repo-size"
  140. sizeContainer.classList.add("Label", "Label--info", "v-align-middle", "ml-1")
  141. sizeContainer.setAttribute("title", "Repository size")
  142. sizeContainer.innerText = "-"
  143. // Create the size icon
  144. let sizeSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg")
  145. sizeSVG.setAttribute("aria-hidden", "true")
  146. sizeSVG.setAttribute("viewBox", "-4 -4 22 22")
  147. sizeSVG.setAttribute("width", "16")
  148. sizeSVG.setAttribute("height", "16")
  149. sizeSVG.setAttribute("fill", "currentColor")
  150. sizeSVG.setAttribute("data-view-component", "true")
  151. sizeSVG.classList.add("octicon", "octicon-file-directory", "mr-1")
  152. let sizeSVGPath = document.createElementNS("http://www.w3.org/2000/svg", "path")
  153. sizeSVGPath.setAttribute("fill-rule", "evenodd")
  154. 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")
  155. sizeSVG.appendChild(sizeSVGPath)
  156. // Convert the size to human readable
  157. const sizes = ["B", "KB", "MB", "GB", "TB"]
  158. const size = jsn.size * 1024 // Github API returns size in KB so convert to bytes
  159. let i = parseInt(Math.floor(Math.log(size) / Math.log(1024)))
  160. const humanReadableSize = (size / Math.pow(1024, i)).toFixed(1) + " " + sizes[i]
  161. // Insert the size into the size container
  162. sizeContainer.innerHTML = `${humanReadableSize}`
  163. sizeContainer.prepend(sizeSVG)
  164. // Insert the size container into the DOM
  165. if (pageType === "code_search") {
  166. parent.style.direction = 'ltr'
  167. }
  168. parent.appendChild(sizeContainer)
  169. }
  170. })
  171. }
  172. window.addSizeToRepos = addSizeToRepos
  173. // Add the size to the repos on the page
  174. //addSizeToRepos()
  175. window.onload = function () {
  176. addSizeToRepos()
  177. }
  178. // Watch for URL changes
  179. let lastUrl = location.href
  180. new MutationObserver(() => {
  181. const url = location.href
  182. if (url !== lastUrl) {
  183. lastUrl = url
  184. setTimeout(function () {
  185. //NOTE - 此处增加延时了,就这样得了
  186. addSizeToRepos()
  187. }, 1500)
  188. }
  189. }).observe(document, { subtree: true, childList: true })