Update script.js

- Custom server field, fallback to mastodon.social
This commit is contained in:
Ralf Stockmann 2023-05-18 09:48:01 +02:00 committed by GitHub
parent 2ce66a27a6
commit a27e604015
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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();