Skip to content

Commit c998132

Browse files
authored
Fix bug: table update error while using file view
The extension tried to update file sizes even when a directory page was not being browsed, which led to error `TypeError: tree.forEach is not a function` on line 153 (line number before this commit) For example, visit page: https://github.com/harshjv/github-repo-size/blob/master/src/inject.js and check extension errors This is now fixed by detecting directory view with the presence of the 'Add file' button and updating table only if the button is present. Also, removed redundant CSS 'cursor: pointer' for newLiElem.
1 parent 99ef114 commit c998132

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/inject.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,24 @@ const checkForRepoPage = async () => {
132132
const ns = document.querySelector('ul.UnderlineNav-body')
133133
const navElem = document.getElementById(NAV_ELEM_ID)
134134
const tdElems = document.querySelector('span.github-repo-size-div')
135+
136+
// whether 'add file' button is present
137+
const viewingDir = document.querySelector('div.file-navigation.mb-3.d-flex.flex-items-start > div.d-flex > details > summary > span.btn.d-none.d-md-flex.flex-items-center') && true
135138

136139
if (ns && !navElem) {
137140
getAPIData(repoObj.repo).then(summary => {
138141
if (summary && summary.size) {
139142
ns.insertAdjacentHTML('beforeend', getSizeHTML(summary.size * 1024))
140143
const newLiElem = document.getElementById(NAV_ELEM_ID)
141-
newLiElem.title = 'Click to load directory sizes'
142-
newLiElem.style.cssText = 'cursor: pointer'
143-
newLiElem.onclick = loadDirSizes
144+
if(viewingDir) {
145+
newLiElem.title = 'Click to load directory sizes'
146+
newLiElem.onclick = loadDirSizes
147+
}
144148
}
145149
})
146150
}
147151

148-
if (tdElems) return
152+
if (!viewingDir || tdElems) return
149153

150154
const tree = await getAPIData(`${repoObj.repo}/contents/${repoObj.currentPath}?ref=${repoObj.ref}`)
151155
const sizeObj = { '..': '..' }

0 commit comments

Comments
 (0)