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

add file's measure select #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getRepoObject = () => {
}
}

const getHumanReadableSizeObject = (bytes) => {
const getHumanReadableSizeObject = (bytes, selectedMeasure = 'auto') => {
if (bytes === 0) {
return {
size: 0,
Expand All @@ -46,18 +46,27 @@ const getHumanReadableSizeObject = (bytes) => {

const K = 1024
const MEASURE = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(K))

let i = Math.floor(Math.log(bytes) / Math.log(K))

if (selectedMeasure !== 'auto') {
for (let size in MEASURE) {
if (MEASURE[size] === selectedMeasure) {
i = size
}
}
}

return {
size: parseFloat((bytes / Math.pow(K, i)).toFixed(2)),
measure: MEASURE[i]
}
}

const getHumanReadableSize = (size) => {
const getHumanReadableSize = (size, selectedSize = 'auto') => {
if (!size) return ''

const t = getHumanReadableSizeObject(size)
const t = getHumanReadableSizeObject(size, selectedSize)

return t.size + ' ' + t.measure
}
Expand All @@ -75,6 +84,24 @@ const getSizeHTML = (size) => {
].join('')
}

const getSelectHTML = (selectedMeasure = 'auto') => {

return [
`<select id="chooseMeasure">`,
`<option value="auto" ${selectedMeasure === 'auto' ? 'selected' : ''}>auto</option>`,
`<option value="B" ${selectedMeasure === 'B' ? 'selected' : ''}>B</option>`,
`<option value="KB" ${selectedMeasure === 'KB' ? 'selected' : ''}>KB</option>`,
`<option value="MB" ${selectedMeasure === 'MB' ? 'selected' : ''}>MB</option>`,
`<option value="GB" ${selectedMeasure === 'GB' ? 'selected' : ''}>GB</option>`,
`<option value="TB" ${selectedMeasure === 'TB' ? 'selected' : ''}>TB</option>`,
`<option value="PB" ${selectedMeasure === 'PB' ? 'selected' : ''}>PB</option>`,
`<option value="EB" ${selectedMeasure === 'EB' ? 'selected' : ''}>EB</option>`,
`<option value="ZB" ${selectedMeasure === 'ZB' ? 'selected' : ''}>ZB</option>`,
`<option value="YB" ${selectedMeasure === 'YB' ? 'selected' : ''}>YB</option>`,
`</select>`
].join('')
}

const checkStatus = (response) => {
if (response.status >= 200 && response.status < 300) {
return response
Expand Down Expand Up @@ -106,7 +133,7 @@ const getAPIData = (uri) => {

const getFileName = text => text.trim().split('/')[0]

const checkForRepoPage = async () => {
const checkForRepoPage = async (selectedMeasure = 'auto') => {
const repoObj = getRepoObject()
if (!repoObj) return

Expand All @@ -121,7 +148,7 @@ const checkForRepoPage = async () => {
const newLiElem = document.getElementById(LI_TAG_ID)
newLiElem.title = 'Click to load folder sizes'
newLiElem.style.cssText = 'cursor: pointer'
newLiElem.onclick = loadFolderSizes
newLiElem.onclick = () => loadFolderSizes(selectedMeasure)
}
})
}
Expand All @@ -139,6 +166,16 @@ const checkForRepoPage = async () => {
let items = document.querySelectorAll('table tbody tr.js-navigation-item td:nth-child(2) a')
let ageForReference = document.querySelectorAll('table tbody tr.js-navigation-item td:last-child')

const th = document.querySelector('th:last-child');
th.style.textAlign = 'right'
th.style.paddingRight = '10px'
th.style.fontWeight = 'normal'
th.innerHTML = getSelectHTML(selectedMeasure)
const select = document.querySelector('#chooseMeasure')
select.title = 'Choose measure'
select.style.cssText = 'cursor: pointer'
select.onchange = recountSizes

if (!list) {
await new Promise((resolve, reject) => {
setTimeout(function () {
Expand All @@ -164,11 +201,11 @@ const checkForRepoPage = async () => {
td.className += ' github-repo-size-folder'
td.title = 'Click to load folder size'
td.style.cssText = 'cursor: pointer'
td.onclick = loadFolderSizes
td.onclick = () => loadFolderSizes(selectedMeasure)
} else if (t === '..') {
label = ''
} else {
label = getHumanReadableSize(t)
label = getHumanReadableSize(t, selectedMeasure)
}

td.innerHTML = `<span class="css-truncate css-truncate-target github-repo-size-td">${label}</span>`
Expand All @@ -177,7 +214,14 @@ const checkForRepoPage = async () => {
}
}

const loadFolderSizes = async () => {
const recountSizes = (e) => {
const oldTdElems = document.querySelectorAll('span.github-repo-size-td')
oldTdElems.forEach(el => el.parentNode.remove())
document.querySelector('#' + LI_TAG_ID).remove()
checkForRepoPage(e.target.value)
}

const loadFolderSizes = async (selectedSize = auto) => {
const files = document.querySelectorAll('table tbody tr.js-navigation-item:not(.up-tree) td.content a')
const folderSizes = document.querySelectorAll('td.github-repo-size-folder > span')
const liElem = document.getElementById(LI_TAG_ID)
Expand Down Expand Up @@ -222,7 +266,7 @@ const loadFolderSizes = async () => {

for (const folderSize of folderSizes) {
const t = sizeObj[getFileName(files[i++].text)]
folderSize.textContent = getHumanReadableSize(t)
folderSize.textContent = getHumanReadableSize(t, selectedSize)
}
}

Expand Down