diff --git a/README.md b/README.md index 476c9b0..42815b9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ GetBible Tooltips is an intuitive and lightweight JavaScript solution for embedd ```html - + ``` 2. **Markup Your Scripture References:** diff --git a/dist/js/getBible.js b/dist/js/getBible.js index 0cc5dea..06815d3 100644 --- a/dist/js/getBible.js +++ b/dist/js/getBible.js @@ -1,4 +1,4 @@ -/*! getBible Loader v2.0.0 | https://getbible.net | (c) 2023 Llewellyn van der Merwe | MIT License */ +/*! getBible Loader v2.0.2 | https://getbible.net | (c) 2023 Llewellyn van der Merwe | MIT License */ class GetBibleTooltip { constructor() { @@ -6,7 +6,6 @@ class GetBibleTooltip { this.findAndFetchScriptureReferences(); } - // Find elements with the 'getBible' class and fetch their references individually findAndFetchScriptureReferences() { const elements = document.querySelectorAll('.getBible'); elements.forEach(element => { @@ -41,18 +40,16 @@ class GetBibleTooltip { }); } - // 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 key = this.generateStorageKey(reference, translation); // Corrected this line 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 + const oneMonthAgo = Date.now() - 30 * 24 * 60 * 60 * 1000; if (timestamp > oneMonthAgo) { return data; } @@ -60,9 +57,8 @@ class GetBibleTooltip { return null; } - // Function to store data in local storage storeInLocalStorage(reference, translation, data) { - const key = generateStorageKey(reference, translation); + const key = this.generateStorageKey(reference, translation); // Corrected this line const item = { data, timestamp: Date.now(), @@ -70,19 +66,16 @@ class GetBibleTooltip { localStorage.setItem(key, JSON.stringify(item)); } - // Modified fetchScripture function async fetchScripture(reference, translation) { try { - // Check local storage first - const localStorageData = await checkLocalStorage(reference, translation); + const localStorageData = await this.checkLocalStorage(reference, translation); // Corrected this line 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 + this.storeInLocalStorage(reference, translation, data); // Corrected this line return data; } else { const errorData = await response.json(); @@ -90,12 +83,9 @@ class GetBibleTooltip { throw new Error(errorMessage); } } catch (error) { - // If the response is not JSON or another error occurs, throw the default error message if (error instanceof SyntaxError) { - // This indicates a problem with JSON parsing, meaning the response was not JSON throw new Error('Failed to fetch scripture'); } else { - // Re-throw the error that we constructed from the JSON response throw error; } } diff --git a/dist/js/getBible.min.js b/dist/js/getBible.min.js index d2aa6c1..a91f5ec 100644 --- a/dist/js/getBible.min.js +++ b/dist/js/getBible.min.js @@ -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(){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}); +/*! getBible Loader v2.0.2 | 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,i=t.dataset.showTranslation?parseInt(t.dataset.showTranslation,10):0,n=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,i,n,o)}).catch(t=>console.error(t))})})})}generateStorageKey(t,e){return`getBible-${e}-${t}`}async checkLocalStorage(t,e){let a=this.generateStorageKey(t,e),r=localStorage.getItem(a);if(r){let{data:i,timestamp:n}=JSON.parse(r),o=Date.now()-2592e6;if(n>o)return i}return null}storeInLocalStorage(t,e,a){let r=this.generateStorageKey(t,e),i={data:a,timestamp:Date.now()};localStorage.setItem(r,JSON.stringify(i))}async fetchScripture(t,e){try{let a=await this.checkLocalStorage(t,e);if(null!==a)return a;let r=await fetch(`${this.apiEndpoint}${encodeURIComponent(e)}/${encodeURIComponent(t)}`);if(r.ok){let i=await r.json();return this.storeInLocalStorage(t,e,i),i}{let n=await r.json(),o=n.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,i){let n="",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)),i&&!h.has(c)&&(d.push(t[c].language),h.add(c)),d.length>0&&(n+="["+d.join(" - ")+"]\n");let p=t[c].verses.map(t=>`${t.verse}. ${t.text}`).join("\n");n+=p+"\n\n"}return n.trim()}addToolTip(t,e,a,r,i,n){let o=this.formatScriptureText(e,a,r,i,n),s=t.title;t.title=s?s+"\n"+o:o}}document.addEventListener("DOMContentLoaded",t=>{new GetBibleTooltip}); diff --git a/example/basic.html b/example/basic.html index 6917344..deaa584 100644 --- a/example/basic.html +++ b/example/basic.html @@ -3,10 +3,10 @@
-