let existingPosts = []; // Function to calculate relative time const timeAgo = function(date) { let seconds = Math.floor((new Date() - date) / 1000); let interval = seconds / 31536000; if (interval > 1) { return Math.floor(interval) + " years ago"; } interval = seconds / 2592000; if (interval > 1) { return Math.floor(interval) + " months ago"; } interval = seconds / 86400; if (interval > 1) { return Math.floor(interval) + " days ago"; } interval = seconds / 3600; if (interval > 1) { return Math.floor(interval) + " hours ago"; } interval = seconds / 60; if (interval > 1) { return Math.floor(interval) + " minutes ago"; } 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(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^]*)'); var results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); } // Get hashtags from URL parameters let hashtags = getUrlParameter('hashtags'); // Split the hashtags string into an array let hashtagsArray = hashtags.split(','); // Get server from URL parameters or use default let server = getUrlParameter('server') || 'https://mastodon.social'; // Function to fetch posts for a given hashtag const getPosts = function(hashtag) { return $.get(`${server}/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; // Check if there are multiple hashtags or just one if (hashtagsArray.length > 1) { // If there are multiple hashtags, `hashtagPosts` is an array of arrays // We use Array.prototype.flat() to combine them into one array allPosts = hashtagPosts.map(postData => postData[0]).flat(); } else { // If there's only one hashtag, `hashtagPosts` is a single array allPosts = hashtagPosts[0]; } // Sort the posts by date/time allPosts.sort((a, b) => new Date(a.created_at) - new Date(b.created_at)); // Loop through the sorted posts $.each(allPosts, function(i, post) { // Check if the post is not already displayed and is not a mention if (!existingPosts.includes(post.id) && post.in_reply_to_id === null) { // Add the post id to existingPosts existingPosts.push(post.id); let cardHTML = `
${post.content}
${post.media_attachments.length > 0 ? `` : ''}