40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
function addItem(url) {
|
|
getFileSize(url, insertRow);
|
|
}
|
|
|
|
async function getFileSize(url, myCallback) {
|
|
// needs proper cors config
|
|
var http = new XMLHttpRequest();
|
|
http.open('HEAD', url, true); // true = Asynchronous
|
|
http.onreadystatechange = function() {
|
|
if (this.readyState == this.DONE) {
|
|
if (this.status === 200) {
|
|
myCallback(url,parseInt(this.getResponseHeader('content-length')));
|
|
}
|
|
}
|
|
};
|
|
http.send();
|
|
}
|
|
|
|
function insertRow(url, size) {
|
|
let table = document.getElementById("table");
|
|
var newRow = table.insertRow(table.length);
|
|
|
|
let suffixes = ["B", "KB", "MB", "GB", "TB"];
|
|
var sindex = 0;
|
|
while(size > 1024) {
|
|
sindex+=1;
|
|
size/=1024;
|
|
}
|
|
|
|
newRow.insertCell(0).innerHTML = "<a href="+url+" download>"+url.split("/").pop()+"</a>";
|
|
newRow.insertCell(1).innerHTML = pRound(size) + " " + suffixes[sindex];
|
|
}
|
|
|
|
function pRound(num) {
|
|
// pretty round
|
|
mult = Math.ceil(Math.pow(10,3-String(num).split(".",1)[0].length));
|
|
return Math.round(num*mult)/mult;
|
|
}
|
|
|
|
// thoughts and prayers for everyone working in javascript
|