loader/dist/js/getBible.min.js

2 lines
1.7 KiB
JavaScript

/*! 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});