fpaste/public/js/main.js
2018-02-20 09:40:37 +01:00

46 lines
1.4 KiB
JavaScript

window.onload = () => {
const menu = document.querySelector("menu");
switch(window.location.pathname.split(/\//)[1]) {
case "v": menu.querySelector("li:nth-child(2)").classList.add("active"); break;
case "u":
menu.querySelector("li:nth-child(4)").classList.add("active");
menu.querySelector("li:nth-child(4) > ul").classList.remove("hidden");
break;
case "a":
menu.querySelector("li:nth-child(6)").classList.add("active");
menu.querySelector("li:nth-child(6) > ul").classList.remove("hidden");
break;
default:
menu.querySelector("li:nth-child(1)").classList.add("active");
break;
}
// timeago
const epochs = [
["year", 31536000],
["month", 2592000],
["day", 86400],
["hour", 3600],
["minute", 60],
["second", 1]
];
const getDuration = timeAgoInSeconds => {
for (let [name, seconds] of epochs) {
const interval = ~~(timeAgoInSeconds / seconds);
if (interval >= 1) {
return {
interval: interval,
epoch: name
};
}
}
};
const timeAgo = date => {
const timeAgoInSeconds = ~~((new Date() - new Date(date)) / 1000);
const {interval, epoch} = getDuration(timeAgoInSeconds);
const suffix = interval === 1 ? "" : "s";
return `${interval} ${epoch}${suffix} ago`;
};
document.querySelectorAll("time").forEach(e => e.innerHTML = timeAgo(e.title));
};