Skip to content

Commit

Permalink
Update readings.js
Browse files Browse the repository at this point in the history
update v1.0
  • Loading branch information
margregorioschurch authored Dec 21, 2024
1 parent c47a1a4 commit 2d9f02c
Showing 1 changed file with 88 additions and 16 deletions.
104 changes: 88 additions & 16 deletions js/readings.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,88 @@
async function load_readings(){let n=new Date,t=`https://orthocal.info/api/gregorian/${n.getFullYear()}/${n.getMonth()+1}/${n.getDate()}/`,a=await fetch(t);if(a.ok){let e=await a.json();abbr_readings=e.abbreviated_reading_indices.map(n=>e.readings[n]),document.querySelector("#readings").innerHTML=`
<h2>${n.toDateString()}</h2>
<h3>${e.summary_title}</h3>
<h2>Scripture Readings</h2>
${abbr_readings.map(n=>`
<h3>${n.display}</h3>
<!-- There is no markup in the verses, so we wrap the whole thing in <p> tags -->
<p>${n.passage.map(n=>n.content).join(" ")}</p>
`).join("")}
<h2>Commemorations</h2>
${e.stories.map(n=>`
<h3>${n.title}</h3>
<!-- Stories already have the html markup embedded, so we don't need <p> tags -->
${n.story}
`).join("")}
`}}document.addEventListener("DOMContentLoaded",load_readings),function(){$(document).ready(function(){n()}),$("button").click(function(){n()});var n=function(){$.ajax({url:"https://labs.bible.org/api/?passage=random&type=json&callback=myCallback",crossDomain:!0,dataType:"jsonp",success:function(n){$("#newQuote0").html("<strong>"+n[0].bookname+" "+n[0].chapter+"&nbsp;:&nbsp;"+n[0].verse+"</strong>&nbsp;-&nbsp;"+n[0].text)}})}}(),function(){$(document).ready(function(){n()}),$("button").click(function(){n()});var n=function(){$.ajax({url:"https://labs.bible.org/api/?passage=random&type=json&callback=myCallback",crossDomain:!0,dataType:"jsonp",success:function(n){$("#newQuote1").html("<strong>"+n[0].bookname+" "+n[0].chapter+"&nbsp;:&nbsp;"+n[0].verse+"</strong>&nbsp;-&nbsp;"+n[0].text)}})}}(),function(){$(document).ready(function(){n()}),$("button").click(function(){n()});var n=function(){$.ajax({url:"https://labs.bible.org/api/?passage=random&type=json&callback=myCallback",crossDomain:!0,dataType:"jsonp",success:function(n){$("#newQuote2").html("<strong>"+n[0].bookname+" "+n[0].chapter+"&nbsp;:&nbsp;"+n[0].verse+"</strong>&nbsp;-&nbsp;"+n[0].text)}})}}(),function(){$(document).ready(function(){n()}),$("button").click(function(){n()});var n=function(){$.ajax({url:"https://labs.bible.org/api/?passage=random&type=json&callback=myCallback",crossDomain:!0,dataType:"jsonp",success:function(n){$("#newQuote3").html("<strong>"+n[0].bookname+" "+n[0].chapter+"&nbsp;:&nbsp;"+n[0].verse+"</strong>&nbsp;-&nbsp;"+n[0].text)}})}}();
async function load_readings() {
let today = new Date();
let apiURL = `https://orthocal.info/api/gregorian/${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}/`;

try {
let response = await fetch(apiURL);
if (response.ok) {
let data = await response.json();
let abbr_readings = data.abbreviated_reading_indices.map(
(index) => data.readings[index]
);

document.querySelector("#readings").innerHTML = `
<h2>${today.toDateString()}</h2>
<h3>${data.summary_title}</h3>
<h2>Scripture Readings</h2>
${abbr_readings
.map(
(reading) => `
<h3>${reading.display}</h3>
<p>${reading.passage.map((p) => p.content).join(" ")}</p>
`
)
.join("")}
<h2>Commemorations</h2>
${data.stories
.map(
(story) => `
<h3>${story.title}</h3>
${story.story}
`
)
.join("")}
`;
} else {
console.error("Failed to fetch readings: ", response.status);
}
} catch (error) {
console.error("Error fetching readings: ", error);
}
}

document.addEventListener("DOMContentLoaded", load_readings);
// Function to fetch a random Bible quote and update the specified element
async function fetchRandomBibleQuote(quoteId) {
try {
const response = await fetch("https://labs.bible.org/api/?passage=random&type=json");
if (!response.ok) throw new Error(`Error fetching quote for ${quoteId}`);
const data = await response.json();

// Update the content of the specific element
document.getElementById(quoteId).innerHTML = `<strong>${data[0].bookname} ${data[0].chapter}:${data[0].verse}</strong> - ${data[0].text}`;
} catch (error) {
console.error(`Failed to load quote for ${quoteId}:`, error);
document.getElementById(quoteId).innerHTML = "Failed to load quote.";
}
}

// Function to load all Bible quotes with a delay
function loadAllQuotesWithDelay() {
// Set delay between each fetch (2 seconds)
setTimeout(() => {
fetchRandomBibleQuote("newQuote0");
}, 0); // No delay for the first quote

setTimeout(() => {
fetchRandomBibleQuote("newQuote1");
}, 2000); // Delay of 2 seconds for the second quote

setTimeout(() => {
fetchRandomBibleQuote("newQuote2");
}, 4000); // Delay of 4 seconds for the third quote

setTimeout(() => {
fetchRandomBibleQuote("newQuote3");
}, 6000); // Delay of 6 seconds for the fourth quote (if present)
}

// Event listener to load quotes with delay on DOMContentLoaded
document.addEventListener("DOMContentLoaded", () => {
loadAllQuotesWithDelay();

// Reload quotes on button click with delay
document.querySelectorAll("button").forEach(button => {
button.addEventListener("click", loadAllQuotesWithDelay);
});
});

0 comments on commit 2d9f02c

Please sign in to comment.