54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
// ==UserScript==
|
|
// @name nginx: Better directory index
|
|
// @include http://localhost:8888/torrent*/download/*
|
|
// @include https://ci.sbruder.de/nix-store/*
|
|
// ==/UserScript==
|
|
|
|
// https://stackoverflow.com/a/14919494
|
|
function humanFileSize(bytes) {
|
|
const thresh = 1024
|
|
if(Math.abs(bytes) < thresh) {
|
|
return bytes + ' B'
|
|
}
|
|
const units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
|
|
var u = -1;
|
|
do {
|
|
bytes /= thresh;
|
|
++u;
|
|
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
|
|
return bytes.toFixed(1)+' '+units[u]
|
|
}
|
|
|
|
|
|
function textToA(line) {
|
|
let outerElement = document.createElement('div')
|
|
outerElement.innerHTML = line
|
|
return outerElement.getElementsByTagName('a')[0]
|
|
}
|
|
|
|
function parseLine(line) {
|
|
const href = textToA(line).href
|
|
const filename = href.substr(-1) === '/' ? decodeURIComponent(href.split('/').slice(-2, -1)[0]) : decodeURIComponent(href.split('/').pop())
|
|
const size = line.split(' ').pop()
|
|
return {
|
|
href: href,
|
|
filename: filename,
|
|
size: size
|
|
}
|
|
}
|
|
|
|
function processLine(line) {
|
|
meta = parseLine(line)
|
|
return `<tr><td><a href="${meta.href}">${meta.filename}</a></td><td>${meta.size === '-' ? '-' : humanFileSize(meta.size)}</td></tr>`
|
|
}
|
|
|
|
const collator = new Intl.Collator('kn', {numeric: true})
|
|
|
|
document.querySelector('pre').outerHTML = '<table style="font-family: monospace;"><tr><td><a href="../">../</a></td><td>-</td></tr>' + document.querySelector('pre').innerHTML
|
|
.split('\n')
|
|
.filter(line => line !== '')
|
|
.filter(line => line !== '<a href="../">../</a>')
|
|
.map(processLine)
|
|
.sort(collator.compare)
|
|
.join('\n') + '</table>'
|