Skip to content

Commit

Permalink
Fix bug: table update error while using file view
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
msvamp authored Jun 24, 2020
1 parent 99ef114 commit c998132
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,24 @@ const checkForRepoPage = async () => {
const ns = document.querySelector('ul.UnderlineNav-body')
const navElem = document.getElementById(NAV_ELEM_ID)
const tdElems = document.querySelector('span.github-repo-size-div')

// whether 'add file' button is present
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

if (ns && !navElem) {
getAPIData(repoObj.repo).then(summary => {
if (summary && summary.size) {
ns.insertAdjacentHTML('beforeend', getSizeHTML(summary.size * 1024))
const newLiElem = document.getElementById(NAV_ELEM_ID)
newLiElem.title = 'Click to load directory sizes'
newLiElem.style.cssText = 'cursor: pointer'
newLiElem.onclick = loadDirSizes
if(viewingDir) {
newLiElem.title = 'Click to load directory sizes'
newLiElem.onclick = loadDirSizes
}
}
})
}

if (tdElems) return
if (!viewingDir || tdElems) return

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

0 comments on commit c998132

Please sign in to comment.