From 3e195ecd44b7b64ffbe76e78ab2c9dfa44812acd Mon Sep 17 00:00:00 2001 From: Ralf Stockmann Date: Thu, 18 May 2023 02:43:37 +0200 Subject: [PATCH] Update script.js - update relative Time every minute --- script.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/script.js b/script.js index b38d017..5dc67d3 100644 --- a/script.js +++ b/script.js @@ -26,6 +26,21 @@ const timeAgo = function(date) { return Math.floor(seconds) + " seconds ago"; } +// Function to update times +const updateTimes = function() { + // Find each timestamp element in the DOM + $('.card-text a').each(function() { + // Get the original date of the post + let date = new Date($(this).attr('data-time')); + + // Calculate the new relative time + let newTimeAgo = timeAgo(date); + + // Update the timestamp with the new relative time + $(this).text(newTimeAgo); + }); +}; + // Function to get a parameter by name from URL function getUrlParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); @@ -43,12 +58,13 @@ let hashtagsArray = hashtags.split(','); // Function to fetch posts for a given hashtag const getPosts = function(hashtag) { - return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`); + return $.get(`https://chaos.social/api/v1/timelines/tag/${hashtag}`); } // Function to fetch and display posts const fetchAndDisplayPosts = function() { + // Fetch posts for each hashtag $.when(...hashtagsArray.map(hashtag => getPosts(hashtag))).then(function(...hashtagPosts) { let allPosts; @@ -82,7 +98,7 @@ const fetchAndDisplayPosts = function() {

${post.content}

${post.media_attachments.length > 0 ? `Image` : ''} -

${timeAgo(new Date(post.created_at))}

+

${timeAgo(new Date(post.created_at))}

`; @@ -108,9 +124,49 @@ $(document).ready(function() { percentPosition: true }); - // Fetch posts for each hashtag on page load - fetchAndDisplayPosts(); + // Check if hashtags are provided + if (hashtagsArray[0] !== '') { + // Fetch posts for each hashtag on page load + fetchAndDisplayPosts(); - // Fetch posts for each hashtag every 10 seconds - setInterval(fetchAndDisplayPosts, 10000); + // Fetch posts for each hashtag every 10 seconds + setInterval(fetchAndDisplayPosts, 10000); + } else { + // Show the zero state and hide the app content + $('#zero-state').removeClass('d-none'); + $('#app-content').addClass('d-none'); + } + + // Update the navbar info with the provided hashtags + $('#hashtag-display').text(`${hashtagsArray.map(hashtag => `#${hashtag}`).join(' ')}`); + + // Handle the form submit event + $('#hashtag-form').on('submit', function(e) { + // Prevent the default form submission + e.preventDefault(); + + // Get the entered hashtags + let hashtags = [ + $('#hashtag1').val(), + $('#hashtag2').val(), + $('#hashtag3').val() + ]; + + // Filter out any empty strings + hashtags = hashtags.filter(function(hashtag) { + return hashtag !== ''; + }); + + // Create a new URL with the entered hashtags + let newUrl = window.location.origin + window.location.pathname + `?hashtags=${hashtags.join(',')}`; + + // Reload the page with the new URL + window.location.href = newUrl; + }); + + // Update the times once when the page loads + updateTimes(); + + // Then update every 60 seconds + setInterval(updateTimes, 60000); });