Update script.js

This commit is contained in:
Voxel 2025-04-15 18:32:12 -04:00 committed by GitHub
parent 76d2618287
commit e454f28b8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,26 +1,25 @@
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
// Generate or retrieve the username from localStorage // Generate a random username in the format 'milkenjoyer[###]'
function generateRandomUsername() { function generateRandomUsername() {
const randomNum = Math.floor(Math.random() * 100000); const randomNum = Math.floor(Math.random() * 1000); // Random number between 0 and 999
return `milkenjoyer${randomNum.toString().padStart(3, '0')}`; return `milkenjoyer[${randomNum.toString().padStart(3, '0')}]`; // Ensure it has 3 digits
} }
// Check if username is already saved, else generate a new one // Check if username is already saved in localStorage
let username = localStorage.getItem('username'); let username = localStorage.getItem('username');
if (!username) { if (!username) {
username = generateRandomUsername(); username = generateRandomUsername(); // Temporary username until the first post
localStorage.setItem('username', username); // Save username to localStorage
} }
document.getElementById('username').innerText = username; document.getElementById('username').innerText = username;
// Retrieve stored posts from localStorage or initialize empty array // Retrieve stored posts from localStorage or initialize as an empty array
let posts = JSON.parse(localStorage.getItem('posts')) || []; let posts = JSON.parse(localStorage.getItem('posts')) || [];
// Function to render posts // Function to render posts
function renderPosts() { function renderPosts() {
postsSection.innerHTML = ''; // Clear current posts postsSection.innerHTML = ''; // Clear current posts
posts posts
.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)) // Sort posts by descending date .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)) // Sort by descending date
.forEach(post => { .forEach(post => {
const postDiv = document.createElement('div'); const postDiv = document.createElement('div');
postDiv.classList.add('post'); postDiv.classList.add('post');
@ -35,7 +34,7 @@ document.addEventListener('DOMContentLoaded', function() {
postContent.innerText = post.content; postContent.innerText = post.content;
postDiv.appendChild(postContent); postDiv.appendChild(postContent);
postsSection.prepend(postDiv); // Add post to the beginning postsSection.prepend(postDiv); // Add post at the beginning
}); });
} }
@ -53,15 +52,22 @@ document.addEventListener('DOMContentLoaded', function() {
const content = postContent.value.trim(); const content = postContent.value.trim();
if (!content) return; if (!content) return;
// Create a new post with a timestamp
const newPost = { const newPost = {
username: username, username: username,
content: content, content: content,
timestamp: new Date().toISOString() // Store the timestamp for sorting timestamp: new Date().toISOString() // Store timestamp for sorting
}; };
posts.push(newPost); posts.push(newPost);
localStorage.setItem('posts', JSON.stringify(posts)); // Save posts to localStorage localStorage.setItem('posts', JSON.stringify(posts)); // Save posts to localStorage
// After the first post, save the username permanently in localStorage
if (!localStorage.getItem('username')) {
localStorage.setItem('username', username);
}
renderPosts(); // Re-render posts renderPosts(); // Re-render posts
postContent.value = ''; // Clear post content postContent.value = ''; // Clear the post content field
}); });
}); });