-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdated-script-jsN.js
111 lines (98 loc) · 3.96 KB
/
updated-script-jsN.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
104
105
106
107
108
109
110
111
let currentChapter = 1;
let currentVerse = 1;
const maxChapter = 18;
const versesPerChapter = {
1: 47, 2: 72, 3: 43, 4: 42, 5: 29, 6: 47, 7: 30, 8: 28, 9: 34,
10: 42, 11: 55, 12: 20, 13: 35, 14: 27, 15: 20, 16: 24, 17: 28, 18: 78
};
function initializeApp() {
document.getElementById("generateBtn").addEventListener("click", () => fetchVerse(currentChapter, currentVerse));
document.getElementById("randomBtn").addEventListener("click", fetchRandomVerse);
document.getElementById("nextBtn").addEventListener("click", nextVerse);
document.getElementById("previousBtn").addEventListener("click", previousVerse);
document.getElementById("chapterSelect").addEventListener("change", updateChapter);
document.getElementById("mobileMenuButton").addEventListener("click", toggleMobileMenu);
populateChapterSelect();
fetchVerse(currentChapter, currentVerse);
}
function populateChapterSelect() {
const chapterSelect = document.getElementById("chapterSelect");
for (let i = 1; i <= maxChapter; i++) {
const option = document.createElement("option");
option.value = i;
option.textContent = `${i}. ${getChapterTitle(i)}`;
chapterSelect.appendChild(option);
}
}
async function fetchVerse(chapter, verse) {
// https://cors-anywhere.herokuapp.com/
const url = `https://cors-anywhere.herokuapp.com/https://www.holy-bhagavad-gita.org/chapter/${chapter}/verse/${verse}`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Network response was not ok');
const html = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const article = doc.querySelector('article');
if (article) {
displayVerse(article.innerHTML);
currentChapter = chapter;
currentVerse = verse;
updateChapterSelect();
} else {
throw new Error('Article not found in the fetched content');
}
} catch (error) {
console.error('Error fetching verse:', error);
displayVerse('Error fetching verse. Please try again.');
}
}
function displayVerse(content) {
document.getElementById("sloka").innerHTML = content;
}
function updateChapterSelect() {
document.getElementById("chapterSelect").value = currentChapter;
}
function updateChapter() {
currentChapter = parseInt(document.getElementById("chapterSelect").value);
currentVerse = 1;
fetchVerse(currentChapter, currentVerse);
}
function nextVerse() {
if (currentVerse < versesPerChapter[currentChapter]) {
currentVerse++;
} else if (currentChapter < maxChapter) {
currentChapter++;
currentVerse = 1;
}
fetchVerse(currentChapter, currentVerse);
}
function previousVerse() {
if (currentVerse > 1) {
currentVerse--;
} else if (currentChapter > 1) {
currentChapter--;
currentVerse = versesPerChapter[currentChapter];
}
fetchVerse(currentChapter, currentVerse);
}
function fetchRandomVerse() {
const randomChapter = Math.floor(Math.random() * maxChapter) + 1;
const randomVerse = Math.floor(Math.random() * versesPerChapter[randomChapter]) + 1;
fetchVerse(randomChapter, randomVerse);
}
function toggleMobileMenu() {
const mobileMenu = document.getElementById("mobileMenu");
mobileMenu.classList.toggle("hidden");
}
function getChapterTitle(chapterNumber) {
const titles = [
"Arjuna Vishada Yoga", "Sankhya Yoga", "Karma Yoga", "Gnana-Karma-Sanyasa Yoga",
"Karma-Sanyasa Yoga", "Atma-Samyama Yoga", "Gnana-Vignana Yoga", "Aksara-ParaBrahma Yoga",
"Raja-Vidya-Raja-Guhya Yoga", "Vibhuti Yoga", "Vishwarupa-Darsana Yoga", "Bhakti Yoga",
"Ksetra-Ksetrajna-Vibhaga Yoga", "Gunatraya-Vibhaga Yoga", "Purushottama Yoga",
"Daivasura-Sampad-Vibhaga Yoga", "Shraddhatraya-Vibhaga Yoga", "Moksha-Sanyasa Yoga"
];
return titles[chapterNumber - 1];
}
initializeApp();