/* Noor plus: tajweed colours, word by word, more translations, the mushaf by page, a memorization loop, badges and statistics. Tajweed, translations and word-by-word come from free Quran APIs the first time and are kept on the device after that. */ "use strict"; (() => { /* ---------- translations in more languages (alquran.cloud) ---------- */ const LANGS = [["", "English only", "الإنجليزية فقط"], ["ur.jalandhry", "اردو · Urdu", "الأردية"], ["fr.hamidullah", "Français", "الفرنسية"], ["tr.diyanet", "Türkçe", "التركية"], ["id.indonesian", "Bahasa Indonesia", "الإندونيسية"], ["ms.basmeih", "Bahasa Melayu", "الملايوية"], ["es.cortes", "Español", "الإسبانية"], ["de.bubenheim", "Deutsch", "الألمانية"], ["bn.bengali", "বাংলা · Bengali", "البنغالية"], ["ru.kuliev", "Русский", "الروسية"], ["fa.makarem", "فارسی · Persian", "الفارسية"], ["hi.farooq", "हिन्दी · Hindi", "الهندية"], ["zh.jian", "中文 · Chinese", "الصينية"], ["so.abduh", "Soomaali", "الصومالية"], ["sw.barwani", "Kiswahili", "السواحيلية"], ["bs.korkut", "Bosanski", "البوسنية"], ["sq.ahmeti", "Shqip · Albanian", "الألبانية"], ["nl.siregar", "Nederlands", "الهولندية"], ["it.piccardo", "Italiano", "الإيطالية"], ["ta.tamil", "தமிழ் · Tamil", "التاميلية"], ["ha.gumi", "Hausa", "الهوسا"]]; const RTL_LANGS = /^(ur|fa)\./; const trCache = {}; async function apiSurah(n, ed) { const key = `${ed}:${n}`; if (trCache[key]) return trCache[key]; const u = `https://api.alquran.cloud/v1/surah/${n}/${ed}`; let res = null; try { const c = await caches.open("noor-extra-v1"); res = await c.match(u); if (!res) { const r = await fetch(u); if (r.ok) { await c.put(u, r.clone()); res = r; } } } catch { } if (!res) { try { res = await fetch(u); } catch { return null; } } try { const j = await res.json(); return trCache[key] = j.data.ayahs.map(a => a.text); } catch { return null; } } window.noorTr = async n => S.tl ? apiSurah(n, S.tl) : null; window.noorTrDir = () => RTL_LANGS.test(S.tl || "") ? "rtl" : "ltr"; /* ---------- tajweed colours ---------- */ const TJ = { h: ["#9aa1a6", "Hamzat al-wasl (not said)", "همزة وصل"], s: ["#9aa1a6", "Silent letter", "حرف لا يُنطق"], l: ["#9aa1a6", "Lam shamsiyyah", "لام شمسية"], n: ["#537fff", "Madd, 2 counts", "مد طبيعي"], p: ["#4050ff", "Madd, 2/4/6 counts", "مد جائز"], m: ["#000ebc", "Madd lazim, 6 counts", "مد لازم"], o: ["#2144c1", "Madd wajib, 4-5 counts", "مد واجب"], q: ["#dd0008", "Qalqalah", "قلقلة"], c: ["#d500b7", "Ikhfa shafawi", "إخفاء شفوي"], f: ["#9400a8", "Ikhfa", "إخفاء"], w: ["#58b800", "Idgham shafawi", "إدغام شفوي"], i: ["#26bffd", "Iqlab", "إقلاب"], a: ["#169777", "Idgham with ghunnah", "إدغام بغنة"], u: ["#169200", "Idgham without ghunnah", "إدغام بلا غنة"], d: ["#a1a1a1", "Idgham mutajanisayn", "إدغام متجانسين"], b: ["#a1a1a1", "Idgham mutaqaribayn", "إدغام متقاربين"], g: ["#ff7e1e", "Ghunnah, 2 counts", "غنة"] }; const esc2 = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c])); function tajweedHtml(t) { return esc2(t).replace(/\[([a-z])(?::\d+)?\[([^\]]*)\]/g, (m, k, txt) => TJ[k] ? `${txt}` : txt); } const bare = w => w.replace(/[ؐ-ًؚ-ٰٟۖ-ۭ\[\]:a-z0-9]/g, "").replace(/ٱ/g, "ا"); window.noorTajweed = async n => { if (!S.tajweed) return null; const list = await apiSurah(n, "quran-tajweed"); if (!list) return null; return list.map((t, i) => { if (i === 0 && n !== 1 && n !== 9) { const w = t.split(" "); if (bare(w.slice(0, 4).join(" ")).startsWith("بسم الله الرحمن الرحيم".slice(0, 6))) t = w.slice(4).join(" "); } return tajweedHtml(t); }); }; window.noorTajweedKey = () => sheet(`

${L("Tajweed colours", "ألوان التجويد")}

${Object.entries(TJ).filter(([k], i, a) => a.findIndex(x => x[1][1] === TJ[k][1]) === i) .map(([, v]) => `
${isAr() ? v[2] : v[1]}
`).join("")}`); /* ---------- word by word (quran.com) ---------- */ const wbwCache = {}; async function words(s, a) { const key = `${s}:${a}`; if (wbwCache[key]) return wbwCache[key]; const u = `https://api.quran.com/api/v4/verses/by_key/${key}?words=true&word_fields=text_uthmani&language=${["ur", "id", "bn", "tr", "fa", "hi"].find(x => (S.tl || "").startsWith(x)) || "en"}`; let res = null; try { const c = await caches.open("noor-extra-v1"); res = await c.match(u); if (!res) { const r = await fetch(u); if (r.ok) { await c.put(u, r.clone()); res = r; } } } catch { } if (!res) res = await fetch(u); const j = await res.json(); return wbwCache[key] = j.verse.words.filter(w => w.char_type_name === "word").map(w => ({ ar: w.text_uthmani, en: (w.translation || {}).text || "", tr: (w.transliteration || {}).text || "", audio: w.audio_url })); } window.noorWbw = async (s, a, el) => { const old = el.querySelector(".wbw"); if (old) return old.remove(); el.insertAdjacentHTML("beforeend", `
…
`); const box = el.querySelector(".wbw"); try { const ws = await words(s, a); box.innerHTML = ws.map((w, i) => ``).join(""); box.onclick = e => { const b = e.target.closest(".wd"); if (!b) return; const w = ws[+b.dataset.i]; if (w.audio) { const au = new Audio("https://audio.qurancdn.com/" + w.audio); au.play().catch(() => { }); } box.querySelectorAll(".wd").forEach(x => x.classList.toggle("on", x === b)); }; } catch { box.innerHTML = `
${L("Word by word needs the internet the first time.", "معاني الكلمات تحتاج إلى إنترنت أول مرة.")}
`; } }; /* ---------- the mushaf by page (1-604) ---------- */ let PAGES = null; // page -> [[surah, from, to], ...] async function pageMap() { if (!PAGES) PAGES = await (await fetch("/static/q/pages.json?v=1")).json(); return PAGES; } async function viewPage(m) { const n = Math.min(604, Math.max(1, +(m && m[1]) || store.get("lastPage", 1))); store.set("lastPage", n); app.innerHTML = topbar(`

${L("Page", "صفحة")} ${num(n)}

`, true, ``) + `
…
`; nav("quran"); const P = await pageMap(), idx = await index(), rows = []; for (const [sn, a, b] of P[n - 1] || []) { const d = await surah(sn); for (let i = a; i <= b; i++) rows.push([sn, d.ayahs[i - 1]]); } const tj = {}; if (S.tajweed) for (const sn of [...new Set(rows.map(r => r[0]))]) tj[sn] = await noorTajweed(sn); let html = "", cur = 0; for (const [sn, a] of rows) { if (sn !== cur) { cur = sn; if (a.n === 1) html += `${html ? "" : ""}
${idx.surahs[sn - 1].name}
${sn !== 1 && sn !== 9 ? `
بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
` : ""}
`; else if (!html) html += `
`; } html += `${tj[sn] ? tj[sn][a.n - 1] : a.ar}﴿${arNum(a.n)}﴾ `; } html += "
"; const first = rows[0], juz = first ? first[1].juz : 1; $("#pgw0").outerHTML = `
${first ? esc(sName(idx.surahs[first[0] - 1])) : ""}${L("Juz", "الجزء")} ${num(juz)}
${html}

${num(n)} / ${num(604)}

`; for (const [sn, a] of rows) markRead(sn, a.n); $("#pgbody").onclick = e => { const w = e.target.closest(".w"); if (w) player.start(+w.dataset.s, +w.dataset.a); }; // swipe: in Arabic the next page is to the left, as in a printed mushaf let x0 = null; const body = $("#pgw"); body.ontouchstart = e => { x0 = e.touches[0].clientX; }; body.ontouchend = e => { if (x0 == null) return; const dx = e.changedTouches[0].clientX - x0; x0 = null; if (Math.abs(dx) < 70) return; location.hash = `#/page/${Math.min(604, Math.max(1, n + (dx > 0 ? 1 : -1)))}`; }; } window.noorPageJump = () => { const v = prompt(L("Go to page (1-604)", "اذهب إلى الصفحة (١-٦٠٤)"), store.get("lastPage", 1)); if (v && +v >= 1 && +v <= 604) location.hash = "#/page/" + (+v); }; /* ---------- memorization loop ---------- */ window.noorLoop = async n => { const d = await surah(n); sheet(`

${L("Memorization loop", "تكرار للحفظ")}

${L("Listen to a few ayahs again and again until they stay with you.", "استمع إلى بضع آيات مرة بعد مرة حتى تحفظها.")}

${L("From ayah", "من آية")}
${L("To ayah", "إلى آية")}
${L("Each ayah", "كل آية")}
${[1, 2, 3, 5].map((v, i) => ``).join("")}
${L("Pause for me to repeat", "توقف لأكرر بعده")}
${L("After each ayah, a quiet gap as long as the ayah", "بعد كل آية، صمت بطولها لتعيدها")}
${L("The whole range", "المقطع كله")}
${[1, 3, 5, 10].map((v, i) => ``).join("")}
`); for (const id of ["#lpE", "#lpR"]) $(id).onclick = e => { const b = e.target.closest("button"); if (b) $(id).querySelectorAll("button").forEach(x => x.classList.toggle("on", x === b)); }; $("#lpGo").onclick = () => { const a = Math.max(1, Math.min(d.count, +$("#lpA").value || 1)), b = Math.max(a, Math.min(d.count, +$("#lpB").value || a)); player.loop = { s: n, a, b, each: +$("#lpE .on").dataset.v, times: +$("#lpR .on").dataset.v, done: 0, gap: $("#lpGap").classList.contains("on") }; player.repeat = player.loop.each; closeSheet(); player.start(n, a); toast(L(`Ayahs ${a}–${b}, ${player.loop.times} times`, `الآيات ${a}–${b}، ${player.loop.times} مرات`)); }; }; /* ---------- badges and statistics ---------- */ const BADGES = [ ["streak3", "🔥", "3 days in a row", "٣ أيام متتالية", s => s.best >= 3], ["streak7", "🔥", "A week in a row", "أسبوع متواصل", s => s.best >= 7], ["streak30", "🏆", "30 days in a row", "٣٠ يومًا متتالية", s => s.best >= 30], ["streak100", "👑", "100 days in a row", "١٠٠ يوم متتالية", s => s.best >= 100], ["read100", "📖", "100 ayahs read", "١٠٠ آية", s => s.total >= 100], ["read1000", "📚", "1,000 ayahs read", "١٠٠٠ آية", s => s.total >= 1000], ["khatma", "🌙", "Finished a khatma", "أتممت ختمة", s => s.khatma], ["hifz1", "🧠", "First surah memorized", "أول سورة محفوظة", s => s.memo >= 1], ["hifz10", "🌟", "10 surahs memorized", "١٠ سور محفوظة", s => s.memo >= 10], ["amma", "💎", "Juz Amma memorized", "حفظت جزء عمّ", s => s.amma], ["perfect", "🎯", "A perfect recitation", "تسميع بلا أخطاء", s => s.perfect], ["recite10", "🎙", "10 recitations", "١٠ تسميعات", s => s.recites >= 10], ["quiz", "🧩", "10/10 in the quiz", "١٠/١٠ في المسابقة", s => s.quiz >= 10], ["game3", "🎮", "Reached level 3 in Noor Quest", "المرحلة ٣ في رحلة نور", s => s.game >= 3], ["tasbih", "📿", "1,000 tasbih", "١٠٠٠ تسبيحة", s => s.tasbih >= 1000], ["names", "✨", "Learned the 99 Names", "تعلمت الأسماء الحسنى", s => s.names >= 99], ["prayed", "🕌", "All five prayers in a day", "الصلوات الخمس في يوم", s => s.five], ["notes", "✎", "Wrote 5 reflections", "٥ تأملات", s => s.notes >= 5], ]; function myStats() { const days = store.get("days", {}), H = store.get("hifz", {}), log = store.get("reciteLog", []), K = store.get("khatma", null); const memo = Object.entries(H).filter(([, v]) => v === "done").map(([s]) => +s); let five = false; for (let i = 0; i < 60 && !five; i++) { const P = store.get("prayed-" + dayKey(new Date(Date.now() - i * 864e5)), {}); five = ["fajr", "dhuhr", "asr", "maghrib", "isha"].every(k => P[k]); } return { best: Math.max(store.get("bestStreak", 0), streakNow()), total: Object.values(days).reduce((a, b) => a + b, 0), khatma: !!(K && Object.keys(K.done || {}).length >= K.days), memo: memo.length, amma: Array.from({ length: 37 }, (_, i) => 78 + i).every(s => H[s] === "done"), perfect: log.some(x => x.score === 100), recites: log.length, quiz: (store.get("quiz", {}).best || 0), game: store.get("gameLevel", 0), tasbih: (store.get("tasbih", {}).total || 0), names: Object.keys(store.get("names", {})).length, five, notes: Object.values(store.get("marks", {})).filter(m => m.note).length, days }; } function checkBadges() { const s = myStats(), have = store.get("badges", {}); for (const [id, ic, en, ar, ok] of BADGES) if (!have[id] && ok(s)) { have[id] = Date.now(); store.set("badges", have); toast(`${ic} ${L("New badge", "وسام جديد")}: ${isAr() ? ar : en}`); break; } } window.addEventListener("hashchange", () => setTimeout(checkBadges, 800)); function viewStats() { const s = myStats(), have = store.get("badges", {}); const last30 = Array.from({ length: 30 }, (_, i) => { const d = new Date(Date.now() - (29 - i) * 864e5); return [d, s.days[dayKey(d)] || 0]; }); const max = Math.max(5, ...last30.map(x => x[1])), active = last30.filter(x => x[1]).length; app.innerHTML = topbar(`

${L("My progress", "تقدمي")}

`) + `
${num(s.total)}
${L("ayahs read", "آية مقروءة")}
${num(s.best)}
${L("best streak", "أطول سلسلة")}
${num(s.memo)}
${L("surahs memorized", "سورة محفوظة")}
${L("Last 30 days", "آخر ٣٠ يومًا")}${num(active)} ${L("active days", "يوم نشط")}
${last30.map(([d, v]) => ``).join("")}
${last30[0][0].toLocaleDateString(isAr() ? "ar-EG" : "en-GB", { day: "numeric", month: "short" })}${L("today", "اليوم")}
${num(noorReadMinutes(1))}
${L("minutes today", "دقيقة اليوم")}
${num(noorReadMinutes(7))}
${L("this week", "هذا الأسبوع")}
${num(noorReadMinutes(30))}
${L("this month", "هذا الشهر")}
${BADGES.map(([id, ic, en, ar]) => `
${have[id] ? ic : "🔒"}${isAr() ? ar : en}
`).join("")}
`; nav("more"); } VIEWS.push([/^\/page(?:\/(\d+))?/, viewPage], [/^\/stats/, viewStats]); window.noorPlus = { LANGS, myStats, BADGES, tajweedHtml }; setTimeout(checkBadges, 2500); })();