1
- /* global fetch */
1
+ /* global fetch, Request, Headers */
2
2
3
3
const API = 'https://api.github.com/repos/'
4
4
@@ -12,19 +12,15 @@ function getRepoInfoURI (uri) {
12
12
return repoURI [ 0 ] + '/' + repoURI [ 1 ]
13
13
}
14
14
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 ( '/' )
28
24
}
29
25
30
26
function getHumanReadableSizeObject ( bytes ) {
@@ -45,7 +41,30 @@ function getHumanReadableSizeObject (bytes) {
45
41
}
46
42
}
47
43
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
+
48
66
function checkStatus ( response ) {
67
+ console . log ( response )
49
68
if ( response . status >= 200 && response . status < 300 ) {
50
69
return response
51
70
}
@@ -62,25 +81,63 @@ function parseJSON (response) {
62
81
}
63
82
64
83
function getAPIData ( uri , callback ) {
65
- fetch ( API + uri , {
66
- headers : {
84
+ var request = new Request ( API + uri , {
85
+ headers : new Headers ( {
67
86
'User-Agent' : 'harshjv/github-repo-size'
68
- }
87
+ } )
69
88
} )
89
+
90
+ fetch ( request )
70
91
. then ( checkStatus )
71
92
. then ( parseJSON )
72
- . then ( data => callback ( data && data . size ) )
93
+ . then ( callback )
73
94
. catch ( e => console . error ( e ) )
74
95
}
75
96
97
+ function getFileName ( text ) {
98
+ return text . trim ( ) . split ( '/' ) [ 0 ]
99
+ }
100
+
76
101
function checkForRepoPage ( ) {
77
102
var repoURI = window . location . pathname . substring ( 1 )
78
103
79
104
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 ++ ] )
84
141
}
85
142
} )
86
143
}
0 commit comments