From a27e60401581c0f658006471f283e4c534e7c9e2 Mon Sep 17 00:00:00 2001 From: Ralf Stockmann Date: Thu, 18 May 2023 09:48:01 +0200 Subject: [PATCH] Update script.js - Custom server field, fallback to mastodon.social --- script.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 5dc67d3..27becaf 100644 --- a/script.js +++ b/script.js @@ -56,12 +56,16 @@ let hashtags = getUrlParameter('hashtags'); 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(`https://chaos.social/api/v1/timelines/tag/${hashtag}`); + return $.get(`${server}/api/v1/timelines/tag/${hashtag}`); } + // Function to fetch and display posts const fetchAndDisplayPosts = function() { @@ -124,6 +128,23 @@ $(document).ready(function() { percentPosition: true }); + // Event listener for clicking on the hashtags + $('#hashtag-display').on('click', function() { + // Hide the main app content + $('#app-content').addClass('d-none'); + + // Show the form screen + $('#zero-state').removeClass('d-none'); + + // Get the current hashtags + let currentHashtags = $(this).text().split(' '); + + // Pre-fill the form fields with the current hashtags + for (let i = 0; i < currentHashtags.length; i++) { + $(`#hashtag${i+1}`).val(currentHashtags[i].substring(1)); // Remove the leading '#' + } + }); + // Check if hashtags are provided if (hashtagsArray[0] !== '') { // Fetch posts for each hashtag on page load @@ -164,6 +185,34 @@ $(document).ready(function() { window.location.href = newUrl; }); + // 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 !== ''; + }); + + // Get the entered server URL + let serverUrl = $('#serverUrl').val(); + + // Create a new URL with the entered hashtags and server URL + let newUrl = window.location.origin + window.location.pathname + `?hashtags=${hashtags.join(',')}&server=${serverUrl}`; + + // Reload the page with the new URL + window.location.href = newUrl; + }); + + // Update the times once when the page loads updateTimes();