-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone.js
74 lines (61 loc) · 2.24 KB
/
one.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Load and manage the stories
let stories = []; // Global variable to hold all stories
// Ensure the page refreshes when the PWA is launched
/*
if (window.matchMedia('(display-mode: standalone)').matches) {
window.addEventListener('pageshow', (event) => {
if (event.persisted) {
window.location.reload();
}
});
}
*/
// Fetch the stories from the JSON file
function getDayOfYear(date = new Date()) {
// Start of the year for the given date
const startOfYear = new Date(date.getFullYear(), 0, 0);
// Calculate the difference in milliseconds and convert to days
const diff = date - startOfYear;
const oneDay = 1000 * 60 * 60 * 24;
// Return the day of the year
return Math.floor(diff / oneDay);
}
fetch('stories.json')
.then(response => response.json())
.then(data => {
stories = data; // Save stories to the global variable
displayStory(getTodayStory()); // Display today's story on load
});
if (window.matchMedia('(display-mode: standalone)').matches) {
window.addEventListener('DOMContentLoaded', () => {
displayStory(getTodayStory()); // Replace with your story-fetching logic
});
}
// Function to get today's story based on the date
function getTodayStory() {
const today = getDayOfYear();
return stories[today % stories.length];
}
// Function to display a story
function displayStory(story) {
const titleDiv = document.getElementById('story-title');
const textDiv = document.getElementById('story-text');
titleDiv.textContent = story.title;
textDiv.innerHTML = formatStory(story.story);
}
// Function to format story text (convert \n to <br> and \n\n to <p>)
function formatStory(text) {
return text
.split('\n\n') // Split paragraphs
.map(paragraph => `<p>${paragraph.replace(/\n/g, '<br>')}</p>`) // Convert line breaks
.join('');
}
// Event listener for the "New Story" button
document.getElementById('new-story-btn').addEventListener('click', () => {
if (stories.length === 0) return; // Ensure stories are loaded
// Randomly select a story
const randomIndex = Math.floor(Math.random() * stories.length);
const randomStory = stories[randomIndex];
// Display the random story
displayStory(randomStory);
});