Adds local memory to store the verses for one month.

This commit is contained in:
Llewellyn van der Merwe 2023-11-12 19:16:19 +02:00
parent 4762d8226c
commit c98e5ada9c
Signed by: Llewellyn
GPG Key ID: A9201372263741E7
3 changed files with 39 additions and 4 deletions

View File

@ -12,7 +12,7 @@ GetBible Tooltips is an intuitive and lightweight JavaScript solution for embedd
```html
<!-- Include the GetBible tooltips script from jsDelivr CDN -->
<script src="https://cdn.jsdelivr.net/gh/getbible/loader@2.0.0/dist/js/getBible.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/getbible/loader@2.0.1/dist/js/getBible.min.js"></script>
```
2. **Markup Your Scripture References:**

39
dist/js/getBible.js vendored
View File

@ -41,15 +41,50 @@ class GetBibleTooltip {
});
}
// Fetch scripture from the API endpoint using the reference
// Function to generate a unique key for each scripture
generateStorageKey(reference, translation) {
return `getBible-${translation}-${reference}`;
}
// Function to check local storage
async checkLocalStorage(reference, translation) {
const key = generateStorageKey(reference, translation);
const storedItem = localStorage.getItem(key);
if (storedItem) {
const { data, timestamp } = JSON.parse(storedItem);
const oneMonthAgo = Date.now() - 30 * 24 * 60 * 60 * 1000; // One month in milliseconds
if (timestamp > oneMonthAgo) {
return data;
}
}
return null;
}
// Function to store data in local storage
storeInLocalStorage(reference, translation, data) {
const key = generateStorageKey(reference, translation);
const item = {
data,
timestamp: Date.now(),
};
localStorage.setItem(key, JSON.stringify(item));
}
// Modified fetchScripture function
async fetchScripture(reference, translation) {
try {
// Check local storage first
const localStorageData = await checkLocalStorage(reference, translation);
if (localStorageData !== null) {
return localStorageData;
}
// Fetch from API if not in local storage
const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`);
if (response.ok) {
const data = await response.json();
storeInLocalStorage(reference, translation, data); // Store in local storage
return data;
} else {
// Attempt to read the JSON response to get the error message
const errorData = await response.json();
const errorMessage = errorData.error || 'Failed to fetch scripture';
throw new Error(errorMessage);

View File

@ -1 +1 @@
/*! getBible Loader v2.0.0 | https://getbible.net | (c) 2023 Llewellyn van der Merwe | MIT License */ class GetBibleTooltip{constructor(){this.apiEndpoint="https://query.getbible.net/v2/",this.findAndFetchScriptureReferences()}findAndFetchScriptureReferences(){document.querySelectorAll(".getBible").forEach(t=>{const e=t.innerHTML.split(";"),a=(t.dataset.translation||"kjv").toLowerCase().split(";"),n=t.dataset.showBookName?parseInt(t.dataset.showBookName,10):1,o=t.dataset.showTranslation?parseInt(t.dataset.showTranslation,10):0,r=t.dataset.showAbbreviation?parseInt(t.dataset.showAbbreviation,10):0,s=t.dataset.showLanguage?parseInt(t.dataset.showLanguage,10):0;e&&e.forEach(e=>{a.forEach(a=>{this.fetchScripture(e.trim(),a.trim()).then(e=>{e&&this.addToolTip(t,e,n,o,r,s)}).catch(t=>console.error(t))})})})}async fetchScripture(t,e){try{const a=await fetch(`${this.apiEndpoint}${encodeURIComponent(e)}/${encodeURIComponent(t)}`);if(a.ok){return await a.json()}{const t=(await a.json()).error||"Failed to fetch scripture";throw new Error(t)}}catch(t){throw t instanceof SyntaxError?new Error("Failed to fetch scripture"):t}}formatScriptureText(t,e,a,n,o){let r="",s=new Set,i=new Set,c=new Set,h=new Set;for(const d in t){let p=[];a&&!i.has(d)&&(p.push(t[d].translation),i.add(d)),n&&!c.has(d)&&(p.push(t[d].abbreviation),c.add(d)),e&&!s.has(d)&&(p.push(t[d].name),s.add(d)),o&&!h.has(d)&&(p.push(t[d].language),h.add(d)),p.length>0&&(r+="["+p.join(" - ")+"]\n"),r+=t[d].verses.map(t=>`${t.verse}. ${t.text}`).join("\n")+"\n\n"}return r.trim()}addToolTip(t,e,a,n,o,r){const s=this.formatScriptureText(e,a,n,o,r),i=t.title;t.title=i?i+"\n"+s:s}}document.addEventListener("DOMContentLoaded",t=>{new GetBibleTooltip});
/*! getBible Loader v2.0.0 | https://getbible.net | (c) 2023 Llewellyn van der Merwe | MIT License */ class GetBibleTooltip{constructor(){this.apiEndpoint="https://query.getbible.net/v2/",this.findAndFetchScriptureReferences()}findAndFetchScriptureReferences(){let t=document.querySelectorAll(".getBible");t.forEach(t=>{let e=t.innerHTML.split(";"),a=(t.dataset.translation||"kjv").toLowerCase().split(";"),r=t.dataset.showBookName?parseInt(t.dataset.showBookName,10):1,n=t.dataset.showTranslation?parseInt(t.dataset.showTranslation,10):0,i=t.dataset.showAbbreviation?parseInt(t.dataset.showAbbreviation,10):0,o=t.dataset.showLanguage?parseInt(t.dataset.showLanguage,10):0;e&&e.forEach(e=>{a.forEach(a=>{this.fetchScripture(e.trim(),a.trim()).then(e=>{e&&this.addToolTip(t,e,r,n,i,o)}).catch(t=>console.error(t))})})})}generateStorageKey(t,e){return`getBible-${e}-${t}`}async checkLocalStorage(t,e){let a=generateStorageKey(t,e),r=localStorage.getItem(a);if(r){let{data:n,timestamp:i}=JSON.parse(r),o=Date.now()-2592e6;if(i>o)return n}return null}storeInLocalStorage(t,e,a){let r=generateStorageKey(t,e),n={data:a,timestamp:Date.now()};localStorage.setItem(r,JSON.stringify(n))}async fetchScripture(t,e){try{let a=await checkLocalStorage(t,e);if(null!==a)return a;let r=await fetch(`${this.apiEndpoint}${encodeURIComponent(e)}/${encodeURIComponent(t)}`);if(r.ok){let n=await r.json();return storeInLocalStorage(t,e,n),n}{let i=await r.json(),o=i.error||"Failed to fetch scripture";throw Error(o)}}catch(s){if(s instanceof SyntaxError)throw Error("Failed to fetch scripture");throw s}}formatScriptureText(t,e,a,r,n){let i="",o=new Set,s=new Set,l=new Set,h=new Set;for(let c in t){let d=[];a&&!s.has(c)&&(d.push(t[c].translation),s.add(c)),r&&!l.has(c)&&(d.push(t[c].abbreviation),l.add(c)),e&&!o.has(c)&&(d.push(t[c].name),o.add(c)),n&&!h.has(c)&&(d.push(t[c].language),h.add(c)),d.length>0&&(i+="["+d.join(" - ")+"]\n");let p=t[c].verses.map(t=>`${t.verse}. ${t.text}`).join("\n");i+=p+"\n\n"}return i.trim()}addToolTip(t,e,a,r,n,i){let o=this.formatScriptureText(e,a,r,n,i),s=t.title;t.title=s?s+"\n"+o:o}}document.addEventListener("DOMContentLoaded",t=>{new GetBibleTooltip});