Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Check stat bar before inserting li node. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
harshjv committed Oct 13, 2016
1 parent d9bac95 commit c8f1415
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/inject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global fetch, Request, Headers */

const API = 'https://api.github.com/repos/'
const LI_TAG_ID = 'github-repo-size'

function isTree (uri) {
var repoURI = uri.split('/')
Expand Down Expand Up @@ -50,7 +51,7 @@ function getHumanReadableSize (size) {

function getSizeHTML (size) {
const humanReadableSize = getHumanReadableSizeObject(size)
return '<li>' +
return '<li id="' + LI_TAG_ID + '">' +
'<a>' +
'<svg class="octicon octicon-database" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">' +
'<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>' +
Expand Down Expand Up @@ -102,43 +103,47 @@ function checkForRepoPage () {

if (isTree(repoURI)) {
var ns = document.querySelector('ul.numbers-summary')
var liElem = document.getElementById(LI_TAG_ID)
var tdElems = document.querySelector('span.github-repo-size-td')

if (ns) {
if (ns && !liElem) {
getAPIData(getRepoInfoURI(repoURI), function (data) {
if (data && data.size) {
ns.insertAdjacentHTML('beforeend', getSizeHTML(data.size * 1024))
}
})
}

getAPIData(getRepoContentURI(repoURI), function (data) {
var sizeArray = {}
if (!tdElems) {
getAPIData(getRepoContentURI(repoURI), function (data) {
var sizeArray = {}

var upTree = document.querySelector('div.file-wrap > table > tbody > tr.up-tree > td > a.js-navigation-open')
var upTree = document.querySelector('div.file-wrap > table > tbody > tr.up-tree > td > a.js-navigation-open')

if (upTree) {
upTree.parentNode.parentNode.appendChild(document.createElement('td'))
}
if (upTree) {
upTree.parentNode.parentNode.appendChild(document.createElement('td'))
}

for (var item of data) {
sizeArray[item.name] = item.type !== 'dir' ? item.size : null
}
for (var item of data) {
sizeArray[item.name] = item.type !== 'dir' ? item.size : null
}

var contents = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.content > span > a')
var ageForReference = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.age')
var contents = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.content > span > a')
var ageForReference = document.querySelectorAll('div.file-wrap > table > tbody:last-child tr > td.age')

var i = 0
var i = 0

for (var o of contents) {
var t = sizeArray[getFileName(o.text)]
for (var o of contents) {
var t = sizeArray[getFileName(o.text)]

var td = document.createElement('td')
td.className = 'age'
td.innerHTML = '<span class="css-truncate css-truncate-target">' + getHumanReadableSize(t) + '</span>'
var td = document.createElement('td')
td.className = 'age'
td.innerHTML = '<span class="css-truncate css-truncate-target github-repo-size-td">' + getHumanReadableSize(t) + '</span>'

o.parentNode.parentNode.parentNode.insertBefore(td, ageForReference[i++])
}
})
o.parentNode.parentNode.parentNode.insertBefore(td, ageForReference[i++])
}
})
}
}
}

Expand Down

0 comments on commit c8f1415

Please sign in to comment.