Skip to content

Commit 99ef114

Browse files
authored
Merge pull request #1 from harshjv/master
Merging with latest changes
2 parents 1bd607f + ffd4cf2 commit 99ef114

File tree

3 files changed

+85
-65
lines changed

3 files changed

+85
-65
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To enable viewing size of private repositories;
2121
3. Click on the Github Repo Size extension (this extension)'s icon aside the address bar.
2222
4. Paste your access token there in the prompt box.
2323

24-
### Temporarily override then token
24+
### Temporarily override the token
2525

2626
You can set `x-github-token` in `localStorage` to your access token, and the extension will use this value even if you've previously set token.
2727

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "GitHub Repository Size",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"manifest_version": 2,
55
"description": "Automatically adds repository size to GitHub's repository summary",
66
"homepage_url": "https://github.com/harshjv/github-repo-size",

src/inject.js

+83-63
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global fetch, Request, Headers, chrome, localStorage */
22

33
const API = 'https://api.github.com/repos/'
4-
const LI_TAG_ID = 'github-repo-size'
4+
const NAV_ELEM_ID = 'github-repo-size'
55
const GITHUB_TOKEN_KEY = 'x-github-token'
66

77
const storage = chrome.storage.sync || chrome.storage.local
@@ -65,14 +65,16 @@ const getHumanReadableSize = (size) => {
6565
const getSizeHTML = (size) => {
6666
const humanReadableSize = getHumanReadableSizeObject(size)
6767

68-
return [
69-
`<li id="${LI_TAG_ID}">`,
70-
'<svg class="octicon octicon-database" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">',
71-
'<path d="M6 15c-3.31 0-6-.9-6-2v-2c0-.17.09-.34.21-.5.67.86 3 1.5 5.79 1.5s5.12-.64 5.79-1.5c.13.16.21.33.21.5v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V7c0-.11.04-.21.09-.31.03-.06.07-.13.12-.19C.88 7.36 3.21 8 6 8s5.12-.64 5.79-1.5c.05.06.09.13.12.19.05.1.09.21.09.31v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V3c0-1.1 2.69-2 6-2s6 .9 6 2v2c0 1.1-2.69 2-6 2zm0-5c-2.21 0-4 .45-4 1s1.79 1 4 1 4-.45 4-1-1.79-1-4-1z"></path>',
72-
'</svg>',
73-
`<span class="num text-emphasized"> ${humanReadableSize.size}</span> ${humanReadableSize.measure}`,
74-
'</li>'
75-
].join('')
68+
return `
69+
<li class="d-flex" id="${NAV_ELEM_ID}">
70+
<a class="js-selected-navigation-item UnderlineNav-item hx_underlinenav-item no-wrap js-responsive-underlinenav-item" data-tab-item="settings-tab" style="cursor: pointer">
71+
<svg class="octicon octicon-gear UnderlineNav-octicon d-none d-sm-inline" display="none inline" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">
72+
<path d="M6 15c-3.31 0-6-.9-6-2v-2c0-.17.09-.34.21-.5.67.86 3 1.5 5.79 1.5s5.12-.64 5.79-1.5c.13.16.21.33.21.5v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V7c0-.11.04-.21.09-.31.03-.06.07-.13.12-.19C.88 7.36 3.21 8 6 8s5.12-.64 5.79-1.5c.05.06.09.13.12.19.05.1.09.21.09.31v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V3c0-1.1 2.69-2 6-2s6 .9 6 2v2c0 1.1-2.69 2-6 2zm0-5c-2.21 0-4 .45-4 1s1.79 1 4 1 4-.45 4-1-1.79-1-4-1z"></path>
73+
</svg>
74+
<span data-content="Settings">${humanReadableSize.size} ${humanReadableSize.measure}</span>
75+
</a>
76+
</li>
77+
`
7678
}
7779

7880
const checkStatus = (response) => {
@@ -81,6 +83,7 @@ const checkStatus = (response) => {
8183
}
8284

8385
console.error(response)
86+
8487
throw Error(`GitHub returned an invalid status: ${response.status}`)
8588
}
8689

@@ -92,7 +95,7 @@ const getAPIData = (uri) => {
9295
const token = localStorage.getItem(GITHUB_TOKEN_KEY) || githubToken
9396

9497
if (token) {
95-
headerObj['Authorization'] = 'token ' + token
98+
headerObj.Authorization = 'token ' + token
9699
}
97100

98101
const request = new Request(`${API}${uri}`, {
@@ -110,18 +113,34 @@ const checkForRepoPage = async () => {
110113
const repoObj = getRepoObject()
111114
if (!repoObj) return
112115

113-
const ns = document.querySelector('ul.numbers-summary')
114-
const liElem = document.getElementById(LI_TAG_ID)
115-
const tdElems = document.querySelector('span.github-repo-size-td')
116+
// wait for the table to load
117+
await new Promise((resolve, reject) => {
118+
function loading () {
119+
return !!document.querySelector('div[role="gridcell"] div.Skeleton')
120+
}
121+
122+
if (!loading()) return resolve()
123+
124+
const interval = setInterval(() => {
125+
if (!loading()) {
126+
clearInterval(interval)
127+
resolve()
128+
}
129+
}, 100)
130+
})
116131

117-
if (ns && !liElem) {
132+
const ns = document.querySelector('ul.UnderlineNav-body')
133+
const navElem = document.getElementById(NAV_ELEM_ID)
134+
const tdElems = document.querySelector('span.github-repo-size-div')
135+
136+
if (ns && !navElem) {
118137
getAPIData(repoObj.repo).then(summary => {
119138
if (summary && summary.size) {
120139
ns.insertAdjacentHTML('beforeend', getSizeHTML(summary.size * 1024))
121-
const newLiElem = document.getElementById(LI_TAG_ID)
122-
newLiElem.title = 'Click to load folder sizes'
140+
const newLiElem = document.getElementById(NAV_ELEM_ID)
141+
newLiElem.title = 'Click to load directory sizes'
123142
newLiElem.style.cssText = 'cursor: pointer'
124-
newLiElem.onclick = loadFolderSizes
143+
newLiElem.onclick = loadDirSizes
125144
}
126145
})
127146
}
@@ -131,80 +150,73 @@ const checkForRepoPage = async () => {
131150
const tree = await getAPIData(`${repoObj.repo}/contents/${repoObj.currentPath}?ref=${repoObj.ref}`)
132151
const sizeObj = { '..': '..' }
133152

134-
for (let item of tree) {
153+
tree.forEach(item => {
135154
sizeObj[item.name] = item.type !== 'dir' ? item.size : 'dir'
136-
}
155+
})
137156

138-
let list = document.querySelectorAll('table tbody tr.js-navigation-item')
139-
let items = document.querySelectorAll('table tbody tr.js-navigation-item td:nth-child(2) a')
140-
let ageForReference = document.querySelectorAll('table tbody tr.js-navigation-item td:last-child')
141-
142-
if (!list) {
143-
await new Promise((resolve, reject) => {
144-
setTimeout(function () {
145-
list = document.querySelectorAll('table tbody tr.js-navigation-item')
146-
items = document.querySelectorAll('table tbody tr.js-navigation-item td:nth-child(2) a')
147-
ageForReference = document.querySelectorAll('table tbody tr.js-navigation-item td:last-child')
148-
}, 1000)
149-
})
150-
}
157+
const list = [...document.querySelectorAll('div[role="row"].Box-row')]
158+
const items = [...document.querySelectorAll('div[role="row"].Box-row div[role="rowheader"] a')]
159+
const ageForReference = document.querySelectorAll('div[role="row"].Box-row div[role="gridcell"]:last-child')
151160

152-
let i = 0
161+
items.forEach((item, index) => {
162+
const filename = getFileName(item.text)
163+
const t = sizeObj[filename]
153164

154-
for (let item of items) {
155-
const t = sizeObj[getFileName(item.text)]
165+
const div = document.createElement('div')
166+
div.setAttribute('role', 'gridcell')
156167

157-
const td = document.createElement('td')
158-
td.className = 'age'
168+
div.style.cssText = 'width: 80px'
169+
div.className = 'text-gray-light text-right mr-3'
159170

160171
let label
161172

162173
if (t === 'dir') {
163174
label = '&middot;&middot;&middot;'
164-
td.className += ' github-repo-size-folder'
165-
td.title = 'Click to load folder size'
166-
td.style.cssText = 'cursor: pointer'
167-
td.onclick = loadFolderSizes
175+
div.className += ' github-repo-size-dir'
176+
div.title = 'Click to load directory size'
177+
div.style.cssText = 'cursor: pointer; width: 80px'
178+
div.onclick = loadDirSizes
179+
div.setAttribute('data-dirname', filename)
168180
} else if (t === '..') {
169181
label = ''
170182
} else {
171183
label = getHumanReadableSize(t)
172184
}
173185

174-
td.innerHTML = `<span class="css-truncate css-truncate-target github-repo-size-td">${label}</span>`
186+
div.innerHTML = `<span class="css-truncate css-truncate-target d-block width-fit github-repo-size-div">${label}</span>`
175187

176-
list[i].insertBefore(td, ageForReference[i++])
177-
}
188+
list[index].insertBefore(div, ageForReference[index])
189+
})
178190
}
179191

180-
const loadFolderSizes = async () => {
181-
const files = document.querySelectorAll('table tbody tr.js-navigation-item:not(.up-tree) td.content a')
182-
const folderSizes = document.querySelectorAll('td.github-repo-size-folder > span')
183-
const liElem = document.getElementById(LI_TAG_ID)
192+
const loadDirSizes = async () => {
193+
const files = [...document.querySelectorAll('div[role="row"].Box-row div[role="rowheader"] a')]
194+
const dirSizes = [...document.querySelectorAll('div.github-repo-size-dir span')]
195+
const navElem = document.getElementById(NAV_ELEM_ID)
184196

185-
if (liElem) {
186-
liElem.onclick = null
187-
liElem.title = null
197+
if (navElem) {
198+
navElem.onclick = null
199+
navElem.title = 'Loading directory sizes...'
188200
}
189201

190-
for (let folderSize of folderSizes) {
191-
folderSize.textContent = '...'
192-
folderSize.parentElement.onclick = null
193-
}
202+
dirSizes.forEach(dir => {
203+
dir.textContent = '...'
204+
dir.parentElement.onclick = null
205+
})
194206

195207
const repoObj = getRepoObject()
196208
if (!repoObj) return
197209

198210
const data = await getAPIData(`${repoObj.repo}/git/trees/${repoObj.ref}?recursive=1`)
199211

200212
if (data.truncated) {
201-
console.warn('github-repo-size: Data truncated. Folder size info may be incomplete.')
213+
console.warn('github-repo-size: Data truncated. Directory size info may be incomplete.')
202214
}
203215

204216
const sizeObj = {}
205217

206-
for (let item of data.tree) {
207-
if (!item.path.startsWith(repoObj.currentPath)) continue
218+
data.tree.forEach(item => {
219+
if (!item.path.startsWith(repoObj.currentPath)) return
208220

209221
const arr = item.path
210222
.replace(new RegExp(`^${repoObj.currentPath}`), '')
@@ -216,13 +228,21 @@ const loadFolderSizes = async () => {
216228
if (sizeObj[dir] === undefined) sizeObj[dir] = 0
217229
sizeObj[dir] += item.size
218230
}
219-
}
231+
})
232+
233+
files.forEach(file => {
234+
const dirname = getFileName(file.text)
235+
const t = sizeObj[dirname]
236+
237+
const dir = dirSizes.find(dir => dir.parentElement.getAttribute('data-dirname') === dirname)
220238

221-
let i = 0
239+
if (dir) {
240+
dir.textContent = getHumanReadableSize(t)
241+
}
242+
})
222243

223-
for (const folderSize of folderSizes) {
224-
const t = sizeObj[getFileName(files[i++].text)]
225-
folderSize.textContent = getHumanReadableSize(t)
244+
if (navElem) {
245+
navElem.title = 'Directory sizes loaded successfully'
226246
}
227247
}
228248

0 commit comments

Comments
 (0)