f0ckv2/public/s/js/item.js
2020-04-05 18:47:09 +02:00

56 lines
1.6 KiB
JavaScript

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 { interval, epoch } = getDuration(~~((new Date() - new Date(date)) / 1000));
return `${interval} ${epoch}${interval === 1 ? "" : "s"} ago`;
};
const clickOnElementBinding = selector => () => (elem = document.querySelector(selector))?elem.click():null;
const keybindings = {
"ArrowLeft": clickOnElementBinding("#next"),
"ArrowRight": clickOnElementBinding("#prev"),
"r": clickOnElementBinding("#random")
};
(() => {
if(video = document.querySelector(".video-js")) {
const vid1 = videojs(video);
vid1.persistvolume({
namespace: "f0ck"
});
if(vid1.autoplay() && !vid1.paused() && vid1.hasClass("vjs-paused")) {
vid1.pause();
vid1.play();
}
}
document.querySelectorAll("time.timeago").forEach(e => e.innerHTML = timeAgo(e.title));
document.addEventListener("keydown", e => {
if(e.key in keybindings) {
e.preventDefault();
keybindings[e.key]();
}
});
if(f0ckimage = document.querySelector("#f0ck-image"))
f0ckimage.addEventListener("click", e => {
e.preventDefault();
f0ckimage.hasAttribute("style")?f0ckimage.removeAttribute("style"):f0ckimage.setAttribute("style", "max-height: unset;");
});
})();