-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (79 loc) · 3.13 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import slokas from "./slokas.js";
let currentSlokaIndex = -1;
let fetchedSlokas = [];
async function fetchAndDisplaySlokaFromArticle(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const htmlText = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(htmlText, "text/html");
const articleTag = doc.querySelector("article");
if (articleTag) {
const slokaElement = document.getElementById("sloka");
slokaElement.innerHTML = articleTag.innerHTML;
fetchedSlokas = Array.from(articleTag.querySelectorAll("some-selector"))
.map((element) => element.innerText);
displaySloka();
} else {
console.error("No article tag found in the fetched document.");
displaySlokaFallback();
}
} catch (error) {
console.error("Failed to fetch and display the article:", error);
displaySlokaFallback();
}
}
function displaySloka() {
const slokaElement = document.getElementById("sloka");
if (fetchedSlokas.length > 0) {
currentSlokaIndex = (currentSlokaIndex + 1) % fetchedSlokas.length;
slokaElement.innerText = fetchedSlokas[currentSlokaIndex];
} else {
slokaElement.innerText = "No slokas available.";
}
}
function displaySlokaFallback() {
generateSloka();
}
function generateSloka() {
const chapterSelect = document.getElementById("chapterSelect");
const selectedChapter = parseInt(chapterSelect.value);
let filteredSlokas = slokas;
if (selectedChapter !== 0) {
filteredSlokas = slokas.filter((sloka) =>
sloka.startsWith(`BG ${selectedChapter}.`)
);
}
const slokaElement = document.getElementById("sloka");
if (filteredSlokas.length === 0) {
slokaElement.innerText = "No slokas found for the selected chapter.";
} else {
currentSlokaIndex = Math.floor(Math.random() * filteredSlokas.length);
slokaElement.innerText = filteredSlokas[currentSlokaIndex];
}
}
document.getElementById("generateBtn").addEventListener("click", function () {
const chapter = document.getElementById("chapterInput").value;
const verse = document.getElementById("verseInput").value;
const externalUrl = `https://cors-anywhere.herokuapp.com/https://www.holy-bhagavad-gita.org/chapter/${chapter}/verse/${verse}`;
fetchAndDisplaySlokaFromArticle(externalUrl);
});
document.getElementById("nextBtn").addEventListener("click", nextSloka);
document.getElementById("previousBtn").addEventListener("click", previousSloka);
document.getElementById("searchBtn").addEventListener("click", searchSloka);
function nextSloka() {
if (fetchedSlokas.length === 0) return;
currentSlokaIndex = (currentSlokaIndex + 1) % fetchedSlokas.length;
document.getElementById("sloka").innerText = fetchedSlokas[currentSlokaIndex];
}
function previousSloka() {
if (fetchedSlokas.length === 0) return;
currentSlokaIndex = (currentSlokaIndex - 1 + fetchedSlokas.length) % fetchedSlokas.length;
document.getElementById("sloka").innerText = fetchedSlokas[currentSlokaIndex];
}
function searchSloka() {
console.log("Search functionality to be implemented.");
}