Fixed syntax errors in class.

This commit is contained in:
Llewellyn van der Merwe 2023-11-12 19:29:33 +02:00
parent c98e5ada9c
commit 6faf139293
Signed by: Llewellyn
GPG Key ID: A9201372263741E7
4 changed files with 10 additions and 20 deletions

View File

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

22
dist/js/getBible.js vendored
View File

@ -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 { class GetBibleTooltip {
constructor() { constructor() {
@ -6,7 +6,6 @@ class GetBibleTooltip {
this.findAndFetchScriptureReferences(); this.findAndFetchScriptureReferences();
} }
// Find elements with the 'getBible' class and fetch their references individually
findAndFetchScriptureReferences() { findAndFetchScriptureReferences() {
const elements = document.querySelectorAll('.getBible'); const elements = document.querySelectorAll('.getBible');
elements.forEach(element => { elements.forEach(element => {
@ -41,18 +40,16 @@ class GetBibleTooltip {
}); });
} }
// Function to generate a unique key for each scripture
generateStorageKey(reference, translation) { generateStorageKey(reference, translation) {
return `getBible-${translation}-${reference}`; return `getBible-${translation}-${reference}`;
} }
// Function to check local storage
async checkLocalStorage(reference, translation) { async checkLocalStorage(reference, translation) {
const key = generateStorageKey(reference, translation); const key = this.generateStorageKey(reference, translation); // Corrected this line
const storedItem = localStorage.getItem(key); const storedItem = localStorage.getItem(key);
if (storedItem) { if (storedItem) {
const { data, timestamp } = JSON.parse(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) { if (timestamp > oneMonthAgo) {
return data; return data;
} }
@ -60,9 +57,8 @@ class GetBibleTooltip {
return null; return null;
} }
// Function to store data in local storage
storeInLocalStorage(reference, translation, data) { storeInLocalStorage(reference, translation, data) {
const key = generateStorageKey(reference, translation); const key = this.generateStorageKey(reference, translation); // Corrected this line
const item = { const item = {
data, data,
timestamp: Date.now(), timestamp: Date.now(),
@ -70,19 +66,16 @@ class GetBibleTooltip {
localStorage.setItem(key, JSON.stringify(item)); localStorage.setItem(key, JSON.stringify(item));
} }
// Modified fetchScripture function
async fetchScripture(reference, translation) { async fetchScripture(reference, translation) {
try { try {
// Check local storage first const localStorageData = await this.checkLocalStorage(reference, translation); // Corrected this line
const localStorageData = await checkLocalStorage(reference, translation);
if (localStorageData !== null) { if (localStorageData !== null) {
return localStorageData; return localStorageData;
} }
// Fetch from API if not in local storage
const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`); const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`);
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
storeInLocalStorage(reference, translation, data); // Store in local storage this.storeInLocalStorage(reference, translation, data); // Corrected this line
return data; return data;
} else { } else {
const errorData = await response.json(); const errorData = await response.json();
@ -90,12 +83,9 @@ class GetBibleTooltip {
throw new Error(errorMessage); throw new Error(errorMessage);
} }
} catch (error) { } catch (error) {
// If the response is not JSON or another error occurs, throw the default error message
if (error instanceof SyntaxError) { if (error instanceof SyntaxError) {
// This indicates a problem with JSON parsing, meaning the response was not JSON
throw new Error('Failed to fetch scripture'); throw new Error('Failed to fetch scripture');
} else { } else {
// Re-throw the error that we constructed from the JSON response
throw error; throw error;
} }
} }

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(){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});

View File

@ -3,10 +3,10 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GetBible Tooltips Example</title> <title>GetBible Tooltips (2.0.2) Example</title>
<!-- getBible JS --> <!-- getBible JS -->
<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.2/dist/js/getBible.min.js"></script>
</head> </head>
<body> <body>