first commit

This commit is contained in:
Llewellyn van der Merwe 2023-11-08 17:36:52 +02:00
commit 52252bc50c
Signed by: Llewellyn
GPG Key ID: A9201372263741E7
5 changed files with 228 additions and 0 deletions

7
LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2023 Llewellyn van der Merwe, getbible.net
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

75
README.md Normal file
View File

@ -0,0 +1,75 @@
# GetBible Tooltips Integration Guide
## Overview
GetBible Tooltips is an intuitive and lightweight JavaScript solution for embedding Bible scripture tooltips into your website. By simply adding the `getBible` class to any element on your webpage, you can enable users to see the full scripture text in a tooltip when they hover over a reference.
## How to Add GetBible Tooltips to Your Website
1. **Include the GetBible JavaScript File:**
First, include the GetBible script in the `<head>` section of your HTML document:
```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>
```
2. **Markup Your Scripture References:**
In the body of your HTML document, apply the `getBible` class to any element that should display a scripture tooltip. Here is an example using a `ul` list:
```html
<ul>
<li class="getBible">John 3:16</li>
<li class="getBible">1 John 3:16-19,22</li>
<!-- Add more elements as needed -->
</ul>
```
For a detailed example, check out the [basic usage example](https://git.vdm.dev/getBible/loader/src/branch/master/example/basic.html) in the `getbible/loader` repository.
## Utilizing Data Attributes
Data attributes allow you to customize the behavior and display of the scripture tooltips.
- `data-translation`: Specify the Bible translations to use, separated by semicolons (e.g., `kjv;aov`). The tooltip will fetch the scripture from each translation listed.
- `data-show-translation`: Set to `1` to display the translation name in the tooltip.
- `data-show-abbreviation`: Set to `1` to display the abbreviation of the translation in the tooltip.
- `data-show-language`: Set to `1` to display the language of the translation in the tooltip.
Here's how you might use these attributes:
```html
<span class="getBible" data-translation="kjv;aov" data-show-translation="1" data-show-language="1">John 3:16,19</span>
```
In the example above, the tooltip for this list item will show text from both the King James Version (KJV) and another version abbreviated as AOV. It will also display the translation name and language for each scripture reference.
## Scripture Reference Formatting
For the scripture references to be recognized correctly by the GetBible Tooltips, they must adhere to the specific book names used in the translation book list. Here are the updated guidelines for formatting your references considering the provided information:
- **Book Names**: Use the exact book names as found in the translation book list. Each translation might have its own list, and to ensure accuracy, refer to the respective list for the correct book names.
- You can view each translation's book list using the API endpoints provided:
- To list all available translations: `https://api.getbible.net/v2/translations.json`
- To list all books for a specific translation: `https://api.getbible.net/v2/[translation_abbreviation]/books.json`
- As a default, all book names from the [King James Version (KJV)](https://api.getbible.net/v2/kjv/books.json) are valid for all translations.
- **No Abbreviations**: Currently, the use of book name abbreviations is not supported. Always use the full book name.
- **Chapter and Verse**: Follow the book name with the chapter number, a colon, and the verse number(s) (e.g., "John 3:16").
- **Multiple Verses**: Separate multiple verses with commas (e.g., "John 3:16,17").
- **Verse Ranges**: Indicate a range of verses using a hyphen (e.g., "John 3:16-19").
- **Multiple References**: Separate different scripture references with semicolons (e.g., "John 3:16-17; 1 John 3:16-19").
Ensure that your references are formatted according to these guidelines for the GetBible Tooltips to function as intended. This attention to detail will provide users with the correct scripture texts and improve their overall experience on your website.
## Copyright and License
Copyright [Llewellyn van der Merwe](https://getBible.net) under the [MIT license](LICENSE.md).

118
dist/js/getBible.js vendored Normal file
View File

@ -0,0 +1,118 @@
/*! 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();
}
// Find elements with the 'getBible' class and fetch their references individually
findAndFetchScriptureReferences() {
const elements = document.querySelectorAll('.getBible');
elements.forEach(element => {
const references = element.innerHTML.split(';');
const translations = (element.dataset.translation || 'kjv').toLowerCase().split(';');
const showBookName = element.dataset.showBookName ?
parseInt(element.dataset.showBookName, 10) : 1;
const showTranslation = element.dataset.showTranslation ?
parseInt(element.dataset.showTranslation, 10) : 0;
const showAbbreviation = element.dataset.showAbbreviation ?
parseInt(element.dataset.showAbbreviation, 10) : 0;
const showLanguage = element.dataset.showLanguage ?
parseInt(element.dataset.showLanguage, 10) : 0;
if (references) {
references.forEach(reference => {
translations.forEach(translation => {
this.fetchScripture(reference.trim(), translation.trim()).then(scripture => {
if (scripture) {
this.addToolTip(
element,
scripture,
showBookName,
showTranslation,
showAbbreviation,
showLanguage
);
}
}).catch(error => console.error(error));
});
});
}
});
}
// Fetch scripture from the API endpoint using the reference
async fetchScripture(reference, translation) {
try {
const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`);
if (response.ok) {
const data = await response.json();
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);
}
} 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;
}
}
}
// Format the verses for tooltip display
formatScriptureText(data, showBook, showTrans, showAbbr, showLang) {
let formattedText = '';
let setBookName = new Set();
let setTranslation = new Set();
let setAbbreviation = new Set();
let setLanguage = new Set();
for (const key in data) {
let headerParts = [];
if (showTrans && !setTranslation.has(key)) {
headerParts.push(data[key].translation);
setTranslation.add(key);
}
if (showAbbr && !setAbbreviation.has(key)) {
headerParts.push(data[key].abbreviation);
setAbbreviation.add(key);
}
if (showBook && !setBookName.has(key)) {
headerParts.push(data[key].name);
setBookName.add(key);
}
if (showLang && !setLanguage.has(key)) {
headerParts.push(data[key].language);
setLanguage.add(key);
}
// Construct the header if there are any parts to include
if (headerParts.length > 0) {
formattedText += '[' + headerParts.join(' - ') + "]\n";
}
// Add verses
const verses = data[key].verses.map(verse => `${verse.verse}. ${verse.text}`).join("\n");
formattedText += verses + "\n\n"; // Add extra newline for separation between entries
}
return formattedText.trim();
}
// Add or append tooltip to the element
addToolTip(element, data, showBook, showTrans, showAbbr, showLang) {
const scriptureText = this.formatScriptureText(data, showBook, showTrans, showAbbr, showLang);
const existingToolTip = element.title;
element.title = existingToolTip ? existingToolTip + "\n" + scriptureText : scriptureText;
}
}
document.addEventListener('DOMContentLoaded', (event) => {
new GetBibleTooltip();
});

1
dist/js/getBible.min.js vendored Normal file
View File

@ -0,0 +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});

27
example/basic.html Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GetBible Tooltips Example</title>
<!-- getBible JS -->
<script src="https://cdn.jsdelivr.net/gh/getbible/loader@2.0.0/dist/js/getBible.min.js"></script>
</head>
<body>
<h1>Scripture References</h1>
<p>Hover over the references to see the scripture text:</p>
<ul>
<li class="getBible" data-translation="kjv;aov" data-show-translation="1" data-show-language="1">John 3:16,19</li>
<li class="getBible" data-show-abbreviation="1">John 3:16-17; 1 John 3:16-19,22</li>
<li class="getBible" data-translation="kjv;codex" data-show-language="1">Genesis 1:1</li>
<li class="getBible" data-translation="kjv;codex" data-show-language="1">Psalms 23:1-4</li>
<li class="getBible">Romans 8:28,31-39</li>
</ul>
</body>
</html>