-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
347 lines (307 loc) · 12.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// https://quran.com/1/1
const site1 = "https://quran.com/";
// https://quranwbw.com/2#5
const site2 = "https://quranwbw.com/";
// https://tanzil.net/#trans/en.hilali/2:6
// https://tanzil.net/#2:6
const site3 = "https://tanzil.net/";
// copy variables
let fullc, arc, enc, tafsirCopy;
// keyboard shortcuts
document.addEventListener("keydown", function (event) {
if (event.key === "s") {
document.getElementById("urlForm").scrollIntoView();
} else if (event.key === "q") {
document.getElementById("quran").scrollIntoView();
} else if (event.key === "t") {
document.getElementById("tafsir-print").scrollIntoView();
} else if (event.key === "u") {
document.getElementById("result").scrollIntoView();
} else if (event.key === "c") {
document.getElementById("copy-status").scrollIntoView();
}
});
// table of contents
document.addEventListener("DOMContentLoaded", function () {
var tocIcon = document.getElementById("tocIcon");
var toc = document.getElementById("toc");
var overlay = document.createElement("div");
overlay.className = "overlay";
document.body.appendChild(overlay);
tocIcon.addEventListener("click", function () {
toc.classList.toggle("open");
overlay.classList.toggle("open");
});
overlay.addEventListener("click", function () {
toc.classList.remove("open");
overlay.classList.remove("open");
});
// Add event listeners to each TOC link
var tocLinks = toc.querySelectorAll("a");
tocLinks.forEach(function (link) {
link.addEventListener("click", function () {
toc.classList.remove("open");
overlay.classList.remove("open");
});
});
});
// copy To Clipboard
function copyToClipboard(textToCopy) {
navigator.clipboard
.writeText(textToCopy)
.then(() => {
// print ready
const printCopy = textToCopy.replace(/\n/g, "<br>");
document.getElementById(
"copy-status"
).innerHTML = `✔️📋 Copied to Clipboard:<hr class="black-hr">${printCopy}<hr class="black-hr">`;
// notify
const notification = document.getElementById("notification");
notification.style.display = "block";
notification.innerHTML = `✔️📋 Copied`;
setTimeout(() => {
notification.style.display = "none";
}, 2000); // Hide after 3 seconds
})
.catch((err) => {
console.error("Error copying text: ", err);
});
}
async function getAyah(surah, ayah, lang) {
// lang fix for ayah
switch (lang) {
case "english":
lang = "en.hilali";
break;
case "urdu":
lang = "ur.junagarhi";
break;
case "arabic":
lang = "en.hilali";
break;
}
try {
// console.log( `https://api.alquran.cloud/v1/ayah/${surah}:${ayah}/${lang}`);
// Fetch English translation
const enResponse = await fetch(
`https://api.alquran.cloud/v1/ayah/${surah}:${ayah}/${lang}`
);
if (!enResponse.ok) {
throw new Error("Failed to fetch English Ayah");
}
const enData = await enResponse.json();
// Fetch Arabic text
const arResponse = await fetch(
`https://api.alquran.cloud/v1/ayah/${surah}:${ayah}/ar`
);
if (!arResponse.ok) {
throw new Error("Failed to fetch Arabic Ayah");
}
const arData = await arResponse.json();
// return values
if (enData.code === 200 && arData.code === 200) {
return {
en: enData.data.text,
enName: enData.data.surah.englishName,
ar: arData.data.text,
arName: arData.data.surah.name,
};
} else {
throw new Error("Failed to fetch Ayah");
}
} catch (err) {
console.error(err);
throw err;
}
}
// https://cdn.jsdelivr.net/gh/spa5k/tafsir_api@main/tafsir/editions.json
// https://cdn.jsdelivr.net/gh/spa5k/tafsir_api@main/tafsir/en-tafisr-ibn-kathir/1/1.json
// getTafsir(1, 2, ar-tafseer-al-saddi)
// getTafsir(1, 2, entafisr-ibn-kathir)
// Parameters: (1-114), (1-286), (en, ar, ur), (tafisr-ibn-kathir, tafseer-al-saddi)
async function getTafsir(surah, ayah, tafsirName) {
// Name fix for tafsir
var trueName;
switch (tafsirName) {
case "ar-tafseer-al-saddi":
trueName = "Tafsir As-Sa'di";
break;
case "ar-tafsir-ibn-kathir":
case "en-tafisr-ibn-kathir":
case "ur-tafseer-ibn-e-kaseer":
trueName = "Tafsir Ibn Kathir";
break;
default:
trueName = tafsirName;
}
try {
// Fetch translation
//console.log( `https://cdn.jsdelivr.net/gh/spa5k/tafsir_api@main/tafsir/${tafsirName}/${surah}/${ayah}.json`);
const tafsirResponse = await fetch(
`https://cdn.jsdelivr.net/gh/spa5k/tafsir_api@main/tafsir/${tafsirName}/${surah}/${ayah}.json`
);
if (!tafsirResponse.ok) {
throw new Error("Failed to fetch Tafsir");
}
const tafsirData = await tafsirResponse.json();
// return values
if (tafsirData.text == "") {
throw new Error("No Tafsir for this Ayah");
} else {
return {
text: tafsirData.text,
name: trueName,
};
}
} catch (err) {
console.error(err);
throw err;
}
}
function qurancomFix(surah, ayahNum) {
if (ayahNum === "" || ayahNum === "0") {
ayahNum = "";
} else {
ayahNum = `/${ayahNum}`;
}
surah = `${site1}${surah}${ayahNum}`;
return surah;
}
function quranwbwFix(surah, ayahNum) {
if (ayahNum === "" || ayahNum === "0") {
ayahNum = "";
} else {
ayahNum = `#${ayahNum}`;
}
surah = `${site2}${surah}${ayahNum}`;
return surah;
}
function tanzilFix(surah, ayahNum) {
if (ayahNum === "" || ayahNum === "0") {
ayahNum = "";
} else {
ayahNum = `:${ayahNum}`;
}
surah = `${site3}#${surah}${ayahNum}`;
return surah;
}
function tanzilEnFix(surah, ayahNum) {
if (ayahNum === "" || ayahNum === "0") {
ayahNum = "";
} else {
ayahNum = `:${ayahNum}`;
}
surah = `${site3}#trans/en.hilali/${surah}${ayahNum}`;
return surah;
}
document.getElementById("urlForm").addEventListener("submit", function (e) {
e.preventDefault();
const surahName = document.getElementById("surah").value;
const lang = document.getElementById("lang").value;
const surahNum = document.getElementById("Surah-num").value;
const ayahNum = document.getElementById("ayah-num").value;
const tafsirHTML = document.getElementById("tafsir").value;
var surah = "";
if (surahName !== "") {
surah = surahName;
}
if (surahNum !== "") {
surah = surahNum;
}
if (surah === "") {
alert("Please provide Surah Name or number");
document.getElementById("result").textContent =
"Please provide Surah Name or number";
} else if (surah) {
const url1 = qurancomFix(surah, ayahNum);
const url2 = quranwbwFix(surah, ayahNum);
const url3 = tanzilFix(surah, ayahNum);
const url4 = tanzilEnFix(surah, ayahNum);
// add buttons to increment/decrement ayah
document.getElementById(
"buttons"
).innerHTML = `<br><button type="submit" onclick="document.getElementById('ayah-num').value = parseInt(document.getElementById('ayah-num').value) - 1;" >⬅️ Prev </button> - <button type="submit" onclick="document.getElementById('ayah-num').value = parseInt(document.getElementById('ayah-num').value) + 1;" > Next ➡️</button>`;
// URLs
document.getElementById("result").innerHTML =
"URLs<hr class='black-hr'>";
surahAyah = `Quran ${surah}:${ayahNum}`;
// QuranCom
document.getElementById(
"result"
).innerHTML += `<li>Quran.com: <a href="${url1}" target="_blank">${surahAyah}</a></li>`;
// QuranWBW
document.getElementById(
"result"
).innerHTML += `<li>QuranWBW: <a href="${url2}" target="_blank">${surahAyah}</a></li>`;
// Tanzil
document.getElementById(
"result"
).innerHTML += `<li>Tanzil: <a href="${url3}" target="_blank">${surahAyah}</a> (Translation: <a href="${url4}" target="_blank">Hilali</a>)</li>`;
// Append and copy ayah
getAyah(surah, ayahNum, lang)
.then((result) => {
// Variables
enSurahDetails = `Surah ${result.enName}, ${surah}:${ayahNum}`;
arSurahDetails = `${result.arName} ، ${surah}:${ayahNum}`;
fullc = `{${result.ar}} [${arSurahDetails}]\n\n{${result.en}} [${enSurahDetails}]`;
arc = `{${result.ar}} [${arSurahDetails}]`;
enc = `{${result.en}} [${enSurahDetails}]`;
// copy by default
copyToClipboard(fullc);
// Ayah Print
document.getElementById(
"quran"
).innerHTML = `${enSurahDetails}<hr class="black-hr"><p class="arabic">${result.ar}</p>${result.en}<br><hr class="black-hr">`;
// buttons to copy
// full copy
document.getElementById(
"quran"
).innerHTML += `<button onclick='copyToClipboard(fullc)'>Copy Full</button>`;
// Arabic copy
document.getElementById(
"quran"
).innerHTML += ` - <button onclick='copyToClipboard(arc)'>Copy Arabic</button>`;
// English copy
document.getElementById(
"quran"
).innerHTML += ` - <button onclick='copyToClipboard(enc)'>Copy Translation</button>`;
})
.catch((error) => {
console.error("Error fetching data:", error);
document.getElementById(
"quran"
).innerHTML = `${error}<br><br>Potential culprits: <br>- Website/Network problem. <br>- Are you sure the Ayah number is correct for that Surah?<br><br>Try the <a href="#result">URLs below</a> instead.`;
});
if (!tafsirHTML == "") {
// insert Tafsir
getTafsir(surah, ayahNum, tafsirHTML)
.then((tafsir) => {
document.getElementById("tafsir-print").style.display =
"block";
// Variables
const tafsirDetails = `${tafsir.name}, ${surah}:${ayahNum}`;
// newline fixes. double new line looks better
const tafsirPrint = tafsir.text.replace(/\n/g, "<br><br>");
const tafsirCopyTmp = tafsir.text.replace(/\n/g, "\n\n");
tafsirCopy = `${tafsirCopyTmp} \n\n [${tafsirDetails}]`;
// tafsir Print
document.getElementById(
"tafsir-print"
).innerHTML = `${tafsirDetails}<hr class="black-hr">${tafsirPrint}<hr class="black-hr">`;
// buttons to copy
// full copy
document.getElementById(
"tafsir-print"
).innerHTML += `<button onclick='copyToClipboard(tafsirCopy)'>Copy Tafsir</button>`;
})
.catch((error) => {
console.error("Error fetching tafsir: ", error);
document.getElementById(
"tafsir-print"
).innerHTML = `${error}<br><br>Potential culprits: <br>- Website/Network problem. <br>- Are you sure the Ayah number is correct for that Surah?<br><br>Try the <a href="#result">URLs below</a> instead.`;
});
} else {
document.getElementById("tafsir-print").style.display = "none";
}
}
});