index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Gets all the videos uploaded to SkibidiHub
  2. async function getAllVideos() {
  3. return await axios.get("/api/getAllVideos").then(response => {
  4. return response.data;
  5. }).catch(error => {
  6. throw new Error(`getAllVideos() error: ${error}`);
  7. });
  8. }
  9. async function sunset() {
  10. return await axios.get("/api/sunset").then(response => {
  11. return response.data;
  12. }).catch(error => {
  13. throw new Error(`sunset(): error: ${error}`);
  14. })
  15. }
  16. function checkSunset(timestamp) {
  17. const countDownTime = timestamp
  18. const currentTime = new Date().getTime();
  19. return (countDownTime - currentTime) < 1
  20. }
  21. function updateTime(timestamp) {
  22. const countDownTime = timestamp
  23. const currentTime = new Date().getTime();
  24. var delta = Math.abs(countDownTime - currentTime) / 1000;
  25. var days = Math.floor(delta / 86400);
  26. delta -= days * 86400;
  27. var hours = Math.floor(delta / 3600) % 24;
  28. delta -= hours * 3600;
  29. var minutes = Math.floor(delta / 60) % 60;
  30. delta -= minutes * 60;
  31. var seconds = Math.floor(delta % 60); // in theory the modulus is not required
  32. document.getElementById("sunset-countdown").innerText = `${days} days, ${hours} hours, ${minutes} minutes, ${seconds} seconds`;
  33. }
  34. // Gets all the videos uploaded to SkibidiHub and sorts them randomly. (This is our algorithm)
  35. async function getRandomVideos(limit) {
  36. let videos = await getAllVideos();
  37. let newVideos = [];
  38. for (let i = 0; i < limit; i++) {
  39. if (videos.length < 1) continue; // If there are no more videos then do not run the code below
  40. let index = getRandomInt(videos.length);
  41. let video = videos[index];
  42. newVideos.push(video);
  43. videos.splice(index, 1); // Remove video from the list of videos
  44. }
  45. return newVideos;
  46. }
  47. // Returns a random int up to a set limit.
  48. function getRandomInt(max) {
  49. return Math.floor(Math.random() * max);
  50. }
  51. function badong() {
  52. document.getElementById("badong").play();
  53. setTimeout(badong, 4000);
  54. }
  55. document.addEventListener("DOMContentLoaded", async () => {
  56. const countdown = await sunset();
  57. if (countdown.sunset) { // Doom
  58. updateTime(countdown.timestamp)
  59. document.getElementById("sunset").style.display = "block";
  60. setInterval(() => {updateTime(countdown.timestamp)}, 1000)
  61. const container = document.getElementById("sunset-container");
  62. container.classList.remove("disabled")
  63. container.addEventListener("click", () => {
  64. if(checkSunset()) {
  65. } else {
  66. container.classList.add("disabled");
  67. window.scrollTo(0, 0)
  68. badong()
  69. }
  70. })
  71. }
  72. document.getElementById("account-button-anchor").href = `/user/${encodeURIComponent(Cookies.get('user'))}`
  73. if (Cookies.get("user") != null) {
  74. document.getElementById("login-button").classList.add("disabled");
  75. document.getElementById("logout-button").classList.remove("disabled");
  76. document.getElementById("upload-button").classList.remove("disabled");
  77. document.getElementById("account-button").classList.remove("disabled");
  78. document.getElementById("sign-in-msg").classList.add("disabled");
  79. }
  80. // john pork code
  81. if(johnPork()) {
  82. const container = document.getElementById("john-pork-container");
  83. container.classList.remove("disabled");
  84. container.addEventListener("click", handleJohnPorkClick);
  85. document.getElementById("john-pork-accept").addEventListener("click", () => {
  86. document.getElementById("john-pork").remove();
  87. document.getElementById("john-pork-ringtone").remove();
  88. document.getElementById("john-pork-call").classList.remove("disabled");
  89. document.getElementById("john-pork-accept").play();
  90. setTimeout(() => {
  91. container.remove();
  92. }, 3000);
  93. })
  94. document.getElementById("john-pork-decline").addEventListener("click", () => {
  95. container.remove();
  96. setTimeout(() => {
  97. alert("NEW MESSAGE (1) FROM John Pork:\nWe need to talk.")
  98. setTimeout(() => {
  99. alert("NEW MESSAGE (2) FROM John Pork:\nGet your ass downstairs now!")
  100. }, 3000)
  101. }, 3000)
  102. })
  103. }
  104. const videos = await getRandomVideos(30);
  105. videos.forEach(video => {
  106. makeVideo(video.id, video)
  107. })
  108. });
  109. function handleJohnPorkClick() {
  110. document.getElementById("john-pork").classList.remove("disabled");
  111. document.getElementById("john-pork-ringtone").play();
  112. document.getElementById("john-pork-container").removeEventListener("click", handleJohnPorkClick)
  113. }
  114. async function makeVideo(id, info) {
  115. const video = document.createElement("a");
  116. video.href = `/video/${id}`
  117. video.classList.add("video");
  118. const img = document.createElement("img");
  119. img.classList.add("thumbnail");
  120. img.setAttribute("src", `/api/thumbnail/${id}`);
  121. img.onclick = function (event) {
  122. window.location.pathname = `/video/${id}`;
  123. };
  124. video.appendChild(img);
  125. const videoInfoContainer = document.createElement("div");
  126. videoInfoContainer.classList.add("video-info-container");
  127. video.appendChild(videoInfoContainer);
  128. const videoTitle = document.createElement("h3");
  129. videoTitle.classList.add("video-title");
  130. const title = info.title
  131. if(info.title.length > 25) {
  132. videoTitle.innerText = title.slice(0, -(info.title.length - 22)) + "...";
  133. } else {
  134. videoTitle.innerText = title
  135. }
  136. videoTitle.onclick = function (event) {
  137. window.location.pathname = `/video/${id}`;
  138. };
  139. videoInfoContainer.appendChild(videoTitle);
  140. const videoUploader = document.createElement("a");
  141. videoUploader.href = `/user/${encodeURIComponent(info.uploader)}`
  142. videoUploader.classList.add("video-uploader");
  143. videoUploader.innerText = `published by: ${info.uploader}`;
  144. videoInfoContainer.appendChild(videoUploader);
  145. document.getElementById("videos").appendChild(video);
  146. }
  147. function isMobileOS() {
  148. const userAgent = window.navigator.userAgent;
  149. const platform = window.navigator?.userAgentData?.platform || window.navigator.platform;
  150. const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
  151. if (iosPlatforms.indexOf(platform) !== -1) {
  152. return true;
  153. } else if (/Android/.test(userAgent)) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. function johnPork() {
  159. if(isMobileOS()) return getRandomInt(100) < 12;
  160. return getRandomInt(100) < 5;
  161. }