/* 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 = `