-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
214 lines (188 loc) · 5.73 KB
/
index.html
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
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quran</title>
<style>
body {
font-size: large;
padding-right: 50px;
padding-left: 50px;
}
.header {
text-align: center;
font-size: xx-large;
}
.title {
font-size: x-large;
}
</style>
</head>
<body>
<p class="header">بسم الله الرحمن الرحيم</p>
<div id="errorDiv" style="display: none; color: red">
<p>Something went wrong. reload page</p>
</div>
<div id="loadingDiv">
<p>جارى التحميل...</p>
</div>
<!-- Choose surah -->
<div>
<span class="title">السورة</span>
<select id="surahsSelect" style="font-size: large"></select>
</div>
<br />
<!-- Choose ayahs-->
<div>
<span class="title">الايات</span>
<span>من</span>
<input type="number" value="1" id="fromAyahInput" />
<span>الى</span>
<input type="number" value="7" id="toAyahInput" />
</div>
<br />
<!-- Repetition times-->
<div>
<span class="title">التكرار</span>
<input type="number" value="7" id="repeatInput" />
</div>
<hr />
<br />
<!-- Controls -->
<div>
<button onclick="run()">تشغيل</button>
<small>رقم التكرار</small>
<small style="color: steelblue" id="repeatView">الرقم</small>
</div>
<br />
<div>
<audio id="ayahAudio" controls onended="onAudioEnded()"></audio>
</div>
<!-- Recitation -->
<div>
<p id="ayahsText">
قل هو الله احمد الله الصمد لم يلد ولم يولد ولم يكن له كفوا احد
</p>
</div>
<script>
let ayahs;
let ayahNumber;
let repeatTimes;
let repeatNumber;
function setRepeatNumber(n) {
repeatNumber = n;
document.getElementById("repeatView").innerHTML = n;
}
function onAudioEnded() {
if (ayahNumber > ayahs.length - 1) {
// We just listened to all ayahs
if (repeatNumber < repeatTimes) {
// One more time, please
setRepeatNumber(repeatNumber 1);
playAyah(ayahs[0]);
ayahNumber = 1;
}
} else {
playAyah(ayahs[ayahNumber]);
ayahNumber ;
}
}
function handleError(e) {
ayahs = undefined;
ayahNumber = undefined;
repeatTimes = undefined;
setRepeatNumber(undefined);
// show error in dom
document.getElementById("errorDiv").style.display = "block";
console.error(e);
throw new Error(e);
}
function playAyah(ayah) {
console.log(ayah);
const ayahAudio = document.getElementById("ayahAudio");
ayahAudio.innerHTML = "";
for (let audioSource of ayah.audioSources) {
const source = document.createElement("source");
source.src = audioSource;
ayahAudio.appendChild(source);
}
ayahAudio.load();
ayahAudio.play();
}
function run() {
const surahNumber = document.getElementById("surahsSelect").value;
const fromAyah = document.getElementById("fromAyahInput").value;
const toAyah = document.getElementById("toAyahInput").value;
(async () => {
let ayahsList = [];
// Fetch ayahs and add them to ayahs list
for (let i = fromAyah; i < toAyah; i ) {
const ayahRes = await fetch(
`http://api.alquran.cloud/v1/ayah/${surahNumber}:${i}/ar.alafasy`
);
if (!ayahRes.ok) {
handleError(new Error(ayahRes.statusText));
}
const ayah = (await ayahRes.json()).data;
ayahsList.push({
audioSources: [ayah.audio, ...ayah.audioSecondary],
text: ayah.text,
});
}
return ayahsList;
})()
.then((ayahsList) => {
if (ayahsList.length === 0) {
throw new Error("There are no ayahs fetched");
}
// Set globals
ayahs = ayahsList;
repeatTimes = parseInt(
document.getElementById("repeatInput").value
);
setRepeatNumber(1);
ayahNumber = 1;
// Recitation
document.getElementById("ayahsText").innerHTML = ayahs
.map((ayah) => ayah.text)
.join("<br/>");
// Audio (Run first ayah)
playAyah(ayahs[0]);
})
.catch((e) => {
handleError(e);
});
}
window.onload = function () {
(async () => {
// Load surahs into the select element
const surahsRes = await fetch("http://api.alquran.cloud/v1/surah");
const surahs = (await surahsRes.json()).data;
const surahsSelect = document.getElementById("surahsSelect");
for (surah of surahs) {
const opt = document.createElement("option");
opt.value = surah.number;
opt.innerHTML =
surah.number
"."
surah.name
" - "
surah.numberOfAyahs
" ايه"
" - "
surah.revelationType;
surahsSelect.appendChild(opt);
}
})()
.then(() => {
document.getElementById("loadingDiv").remove();
})
.catch((e) => {
console.error(e);
document.write("error, reload the page");
});
};
</script>
</body>
</html>