Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.

Commit 961717e

Browse files
committed
🎉 Display file size
1 parent e64bc1e commit 961717e

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed

src/inject.js

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global fetch */
1+
/* global fetch, Request, Headers */
22

33
const API = 'https://api.github.com/repos/'
44

@@ -12,19 +12,15 @@ function getRepoInfoURI (uri) {
1212
return repoURI[0] + '/' + repoURI[1]
1313
}
1414

15-
function getSizeHTML (size) {
16-
const humanReadableSize = getHumanReadableSizeObject(size)
17-
return '<li>' +
18-
'<a>' +
19-
'<svg class="octicon octicon-database" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">' +
20-
'<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>' +
21-
'</svg>' +
22-
'<span class="num text-emphasized"> ' +
23-
humanReadableSize.size +
24-
'</span> ' +
25-
humanReadableSize.measure +
26-
'</a>' +
27-
'</li>'
15+
function getRepoContentURI (uri) {
16+
var repoURI = uri.split('/')
17+
var treeBranch = repoURI.splice(2, 2, 'contents')
18+
19+
if (treeBranch && treeBranch[1]) {
20+
repoURI.push('?ref=' + treeBranch[1])
21+
}
22+
23+
return repoURI.join('/')
2824
}
2925

3026
function getHumanReadableSizeObject (bytes) {
@@ -45,7 +41,30 @@ function getHumanReadableSizeObject (bytes) {
4541
}
4642
}
4743

44+
function getHumanReadableSize (size) {
45+
if (!size) return ''
46+
47+
var t = getHumanReadableSizeObject(size)
48+
return t.size + ' ' + t.measure
49+
}
50+
51+
function getSizeHTML (size) {
52+
const humanReadableSize = getHumanReadableSizeObject(size)
53+
return '<li>' +
54+
'<a>' +
55+
'<svg class="octicon octicon-database" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">' +
56+
'<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>' +
57+
'</svg>' +
58+
'<span class="num text-emphasized"> ' +
59+
humanReadableSize.size +
60+
'</span> ' +
61+
humanReadableSize.measure +
62+
'</a>' +
63+
'</li>'
64+
}
65+
4866
function checkStatus (response) {
67+
console.log(response)
4968
if (response.status >= 200 && response.status < 300) {
5069
return response
5170
}
@@ -62,25 +81,63 @@ function parseJSON (response) {
6281
}
6382

6483
function getAPIData (uri, callback) {
65-
fetch(API + uri, {
66-
headers: {
84+
var request = new Request(API + uri, {
85+
headers: new Headers({
6786
'User-Agent': 'harshjv/github-repo-size'
68-
}
87+
})
6988
})
89+
90+
fetch(request)
7091
.then(checkStatus)
7192
.then(parseJSON)
72-
.then(data => callback(data && data.size))
93+
.then(callback)
7394
.catch(e => console.error(e))
7495
}
7596

97+
function getFileName (text) {
98+
return text.trim().split('/')[0]
99+
}
100+
76101
function checkForRepoPage () {
77102
var repoURI = window.location.pathname.substring(1)
78103

79104
if (isTree(repoURI)) {
80-
getAPIData(getRepoInfoURI(repoURI), function (size) {
81-
if (size) {
82-
var ns = document.querySelector('ul.numbers-summary')
83-
ns.insertAdjacentHTML('beforeend', getSizeHTML(size))
105+
var ns = document.querySelector('ul.numbers-summary')
106+
107+
if (ns) {
108+
getAPIData(getRepoInfoURI(repoURI), function (data) {
109+
if (data && data.size) {
110+
ns.insertAdjacentHTML('beforeend', getSizeHTML(data.size))
111+
}
112+
})
113+
}
114+
115+
getAPIData(getRepoContentURI(repoURI), function (data) {
116+
var sizeArray = {}
117+
118+
var upTree = document.querySelector('div.file-wrap > table > tbody > tr.up-tree > td > a.js-navigation-open')
119+
120+
if (upTree) {
121+
upTree.parentNode.parentNode.appendChild(document.createElement('td'))
122+
}
123+
124+
for (var item of data) {
125+
sizeArray[item.name] = item.type !== 'dir' ? item.size : null
126+
}
127+
128+
var contents = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.content > span > a')
129+
var ageForReference = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.age')
130+
131+
var i = 0
132+
133+
for (var o of contents) {
134+
var t = sizeArray[getFileName(o.text)]
135+
136+
var td = document.createElement('td')
137+
td.className = 'age'
138+
td.innerHTML = '<span class="css-truncate css-truncate-target">' + getHumanReadableSize(t) + '</span>'
139+
140+
o.parentNode.parentNode.parentNode.insertBefore(td, ageForReference[i++])
84141
}
85142
})
86143
}

0 commit comments

Comments
 (0)