1
1
/* global fetch, Request, Headers, chrome, localStorage */
2
2
3
3
const API = 'https://api.github.com/repos/'
4
- const LI_TAG_ID = 'github-repo-size'
4
+ const NAV_ELEM_ID = 'github-repo-size'
5
5
const GITHUB_TOKEN_KEY = 'x-github-token'
6
6
7
7
const storage = chrome . storage . sync || chrome . storage . local
@@ -65,14 +65,16 @@ const getHumanReadableSize = (size) => {
65
65
const getSizeHTML = ( size ) => {
66
66
const humanReadableSize = getHumanReadableSizeObject ( size )
67
67
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
+ `
76
78
}
77
79
78
80
const checkStatus = ( response ) => {
@@ -81,6 +83,7 @@ const checkStatus = (response) => {
81
83
}
82
84
83
85
console . error ( response )
86
+
84
87
throw Error ( `GitHub returned an invalid status: ${ response . status } ` )
85
88
}
86
89
@@ -92,7 +95,7 @@ const getAPIData = (uri) => {
92
95
const token = localStorage . getItem ( GITHUB_TOKEN_KEY ) || githubToken
93
96
94
97
if ( token ) {
95
- headerObj [ ' Authorization' ] = 'token ' + token
98
+ headerObj . Authorization = 'token ' + token
96
99
}
97
100
98
101
const request = new Request ( `${ API } ${ uri } ` , {
@@ -110,18 +113,34 @@ const checkForRepoPage = async () => {
110
113
const repoObj = getRepoObject ( )
111
114
if ( ! repoObj ) return
112
115
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
+ } )
116
131
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 ) {
118
137
getAPIData ( repoObj . repo ) . then ( summary => {
119
138
if ( summary && summary . size ) {
120
139
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'
123
142
newLiElem . style . cssText = 'cursor: pointer'
124
- newLiElem . onclick = loadFolderSizes
143
+ newLiElem . onclick = loadDirSizes
125
144
}
126
145
} )
127
146
}
@@ -131,80 +150,73 @@ const checkForRepoPage = async () => {
131
150
const tree = await getAPIData ( `${ repoObj . repo } /contents/${ repoObj . currentPath } ?ref=${ repoObj . ref } ` )
132
151
const sizeObj = { '..' : '..' }
133
152
134
- for ( let item of tree ) {
153
+ tree . forEach ( item => {
135
154
sizeObj [ item . name ] = item . type !== 'dir' ? item . size : 'dir'
136
- }
155
+ } )
137
156
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' )
151
160
152
- let i = 0
161
+ items . forEach ( ( item , index ) => {
162
+ const filename = getFileName ( item . text )
163
+ const t = sizeObj [ filename ]
153
164
154
- for ( let item of items ) {
155
- const t = sizeObj [ getFileName ( item . text ) ]
165
+ const div = document . createElement ( 'div' )
166
+ div . setAttribute ( 'role' , 'gridcell' )
156
167
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 '
159
170
160
171
let label
161
172
162
173
if ( t === 'dir' ) {
163
174
label = '···'
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 )
168
180
} else if ( t === '..' ) {
169
181
label = ''
170
182
} else {
171
183
label = getHumanReadableSize ( t )
172
184
}
173
185
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>`
175
187
176
- list [ i ] . insertBefore ( td , ageForReference [ i ++ ] )
177
- }
188
+ list [ index ] . insertBefore ( div , ageForReference [ index ] )
189
+ } )
178
190
}
179
191
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 )
184
196
185
- if ( liElem ) {
186
- liElem . onclick = null
187
- liElem . title = null
197
+ if ( navElem ) {
198
+ navElem . onclick = null
199
+ navElem . title = 'Loading directory sizes...'
188
200
}
189
201
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
+ } )
194
206
195
207
const repoObj = getRepoObject ( )
196
208
if ( ! repoObj ) return
197
209
198
210
const data = await getAPIData ( `${ repoObj . repo } /git/trees/${ repoObj . ref } ?recursive=1` )
199
211
200
212
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.' )
202
214
}
203
215
204
216
const sizeObj = { }
205
217
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
208
220
209
221
const arr = item . path
210
222
. replace ( new RegExp ( `^${ repoObj . currentPath } ` ) , '' )
@@ -216,13 +228,21 @@ const loadFolderSizes = async () => {
216
228
if ( sizeObj [ dir ] === undefined ) sizeObj [ dir ] = 0
217
229
sizeObj [ dir ] += item . size
218
230
}
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 )
220
238
221
- let i = 0
239
+ if ( dir ) {
240
+ dir . textContent = getHumanReadableSize ( t )
241
+ }
242
+ } )
222
243
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'
226
246
}
227
247
}
228
248
0 commit comments