stats.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (()=>{
  2. let tmp = new Date();
  3. const NOW = tmp.getTime();
  4. tmp.setHours(0);
  5. tmp.setMinutes(0);
  6. tmp.setSeconds(0);
  7. let today = tmp.getTime();
  8. if (tmp.getDay() === 0)
  9. tmp.setDate(tmp.getDate() -7);
  10. if (tmp.getDay() !== 1)
  11. tmp.setDate(tmp.getDate()-tmp.getDay() +1);
  12. const ONE_WEEK = tmp.getTime();
  13. tmp.setTime(today);
  14. tmp.setDate(1);
  15. const ONE_MONTH = tmp.getTime();
  16. const relSource = document.getElementById("relSource");
  17. /* INIT DROPDOWN */ (() => {
  18. let frag = document.createDocumentFragment();
  19. let origins = ACCESS.reduce((acc, i) => { acc[i.publicId] = true; return acc; }, {});
  20. for (let i of ORIGINS) origins[i] = true;
  21. for (let i of (["All"].concat(Object.keys(origins).sort())).map(i => {
  22. let node = document.createElement("option");
  23. node.textContent = i;
  24. return node;
  25. }))
  26. frag.appendChild(i);
  27. relSource.appendChild(frag);
  28. })();
  29. function formatDate(d) {
  30. if (!d || isNaN(d.getTime()))
  31. return "";
  32. return d.toLocaleDateString() + " " + d.toLocaleTimeString();
  33. }
  34. function prependDomain(xmlElement) {
  35. xmlElement.textContent = document.location.origin + xmlElement.textContent;
  36. }
  37. function generateQrCode(size, rel) {
  38. if (rel)
  39. rel = "&rel="+encodeURIComponent(rel);
  40. return "/" +PUBLIC_ID +"/qrcode.png?origin="+encodeURIComponent(document.location.origin)+'&s='+size +(rel || "");
  41. }
  42. let HASH_CHANGING = false;
  43. function selectSource(source) {
  44. let _access = source === "All" ? ACCESS : ACCESS.filter(i => i.publicId === source);
  45. let lastAccess = _access.reduce((prev, i) => (prev && prev.accessTime > i.accessTime) ? prev : i, null);
  46. document.getElementById('totalAccessCount').textContent = _access.length;
  47. document.getElementById('weekAccess').textContent = _access.reduce((acc, i) => acc + (i.accessTime >= ONE_WEEK ? 1 : 0), 0);
  48. document.getElementById('monthAccess').textContent = _access.reduce((acc, i) => acc + (i.accessTime >= ONE_MONTH ? 1 : 0), 0);
  49. document.getElementById('lastAccess').textContent = formatDate(new Date(lastAccess?.accessTime));
  50. document.getElementById("publicLinkQr").innerHTML = "<img src='" +generateQrCode(150, !source || source === "All" ? null : source) +"'/>";
  51. document.getElementById("publicLinkQrBig").href = generateQrCode(1024, !source || source === "All" ? null : source);
  52. const linkEnd = PUBLIC_ID + (!source || source === "All" ? "" : ("?r=" +source));
  53. document.getElementById("publicLink").textContent = document.location.origin + "/x/" + linkEnd;
  54. document.getElementById("embedLink").textContent = document.location.origin + "/x/raw/" + linkEnd;
  55. document.getElementById("publicLinkA").href = "/x/" +linkEnd;
  56. document.getElementById("embedLinkA").href = "/x/raw/" +linkEnd;
  57. HASH_CHANGING = true;
  58. document.location.hash = encodeURIComponent(source);
  59. HASH_CHANGING = false;
  60. }
  61. function updateSource() {
  62. const selected = relSource.selectedOptions?.[0]?.textContent;
  63. selectSource(selected || selected === '' ? selected : "All");
  64. }
  65. function hashChanged() {
  66. if (HASH_CHANGING) return;
  67. if (document.location.hash) {
  68. const hash = (decodeURIComponent(document.location.hash.substring(1)));
  69. selectSource(hash);
  70. for (let i =0; i < relSource.children.length; ++i) {
  71. if (relSource.children[i].textContent === hash) {
  72. relSource.selectedIndex = i;
  73. break;
  74. }
  75. }
  76. } else {
  77. updateSource();
  78. }
  79. }
  80. hashChanged();
  81. addEventListener("hashchange", hashChanged);
  82. relSource.addEventListener("change", () => updateSource());
  83. document.getElementById("size").textContent = FILE_SIZE +'o';
  84. document.getElementById("creationTime").textContent = formatDate(new Date(CREATION_TIME));
  85. document.getElementById("expiralTime").textContent = formatDate(new Date(EXPIRAL_TIME));
  86. })();