From 505314c50fafa76f2c465d3d012e81cc41c80f52 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 21 Mar 2026 17:01:35 +0200 Subject: fix(cgit.js): Execute most logic when document has loaded --- cgit.js | 122 +++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 62 insertions(+), 60 deletions(-) (limited to 'cgit.js') diff --git a/cgit.js b/cgit.js index 7dd14b9..0514186 100644 --- a/cgit.js +++ b/cgit.js @@ -10,77 +10,79 @@ viewport.name = "viewport"; viewport.content = "width=device-width initial-scale=1"; document.head.appendChild(viewport); -// Edit file tree -const list = document.getElementsByClassName('list')[0] +document.addEventListener("DOMContentLoaded", function(){ + // Edit file tree + const list = document.getElementsByClassName('list')[0] -if (list !== undefined && list.summary === "tree listing") { - const table = list.children[0] - const rows = table.children + if (list !== undefined && list.summary === "tree listing") { + const table = list.children[0] + const rows = table.children - // Reorder directories (and submodules) before files - var lastDir = 1 - for (let i = 1; i < rows.length; ++i) { - if (!rows[i].children[0].innerText.startsWith('-')) { - if (i > lastDir) { - table.insertBefore(rows[i], rows[lastDir]) + // Reorder directories (and submodules) before files + var lastDir = 1 + for (let i = 1; i < rows.length; ++i) { + if (!rows[i].children[0].innerText.startsWith('-')) { + if (i > lastDir) { + table.insertBefore(rows[i], rows[lastDir]) + } + ++lastDir } - ++lastDir } - } - // Human-readable size format - for (const row of rows) { - if (row.children[2].classList.contains('ls-size')) { - let size = row.children[2].innerText - if (size < 1024) { - size += ' B' - } - else if (size < 1048576) { - size = (size / 1024).toFixed(1) + ' KiB' - } - else if (size < 1073741824) { - size = (size / 1048576).toFixed(1) + ' MiB' + // Human-readable size format + for (const row of rows) { + if (row.children[2].classList.contains('ls-size')) { + let size = row.children[2].innerText + if (size < 1024) { + size += ' B' + } + else if (size < 1048576) { + size = (size / 1024).toFixed(1) + ' KiB' + } + else if (size < 1073741824) { + size = (size / 1048576).toFixed(1) + ' MiB' + } + else { + size = (size / 1073741824).toFixed(1) + ' GiB' + } + row.children[2].innerText = size } - else { - size = (size / 1073741824).toFixed(1) + ' GiB' - } - row.children[2].innerText = size } } -} -// Edit the tabs -const tabs = document.getElementsByClassName('tabs')[0].children[0].children[0].children[0] + // Edit the tabs + const tabs = document.getElementsByClassName('tabs')[0].children[0].children[0].children[0] -if (tabs !== undefined && tabs.children.length === 7) { - const about = tabs.children[0] - const summary = tabs.children[1] - const refs = tabs.children[2] - const log = tabs.children[3] - const tree = tabs.children[4] - const commit = tabs.children[5] - const diff = tabs.children[6] + if (tabs !== undefined && tabs.children.length === 7) { + const about = tabs.children[0] + const summary = tabs.children[1] + const refs = tabs.children[2] + const log = tabs.children[3] + const tree = tabs.children[4] + const commit = tabs.children[5] + const diff = tabs.children[6] - tabs.insertBefore(tree, refs) - tabs.insertBefore(log, refs) + tabs.insertBefore(tree, refs) + tabs.insertBefore(log, refs) - about.innerText = 'README' - summary.innerText = 'Summary' - tree.innerText = 'Files' - log.innerText = 'Commits' -} -else if (tabs !== undefined && tabs.children.length === 6) { - const summary = tabs.children[0] - const refs = tabs.children[1] - const log = tabs.children[2] - const tree = tabs.children[3] - const commit = tabs.children[4] - const diff = tabs.children[5] + about.innerText = 'README' + summary.innerText = 'Summary' + tree.innerText = 'Files' + log.innerText = 'Commits' + } + else if (tabs !== undefined && tabs.children.length === 6) { + const summary = tabs.children[0] + const refs = tabs.children[1] + const log = tabs.children[2] + const tree = tabs.children[3] + const commit = tabs.children[4] + const diff = tabs.children[5] - tabs.insertBefore(tree, refs) - tabs.insertBefore(log, refs) + tabs.insertBefore(tree, refs) + tabs.insertBefore(log, refs) - summary.innerText = 'Summary' - tree.innerText = 'Files' - log.innerText = 'Commits' -} + summary.innerText = 'Summary' + tree.innerText = 'Files' + log.innerText = 'Commits' + } +}); -- cgit v1.2.3