diff --git a/template/commoncode.js b/template/commoncode.js index 7977db5c70..0f23b532d2 100644 --- a/template/commoncode.js +++ b/template/commoncode.js @@ -35,6 +35,8 @@ window.searchBar = +
+Searching...
'; + const params = new URLSearchParams(window.location.search); + const edition = params.get('edition'); + try { + if (edition !== cachedEdition || !cachedHadiths) { + resultsEl.innerHTML = 'Loading hadith data...
'; + const res = await fetch( + `https://cdn.jsdelivr.net/gh/fawazahmed0/hadith-api@1/editions/${edition}.json` + ); + const data = await res.json(); + cachedHadiths = data.hadiths; + cachedEdition = edition; + } + const fuse = new Fuse(cachedHadiths, { + keys: ['text'], + threshold: 0.3, + minMatchCharLength: 3, + includeScore: true, + }); + const results = fuse.search(query).slice(0, 20); + if (results.length === 0) { + resultsEl.innerHTML = 'No results found.
'; + divider.classList.add('d-none'); + return; + } + resultsEl.innerHTML = results.map(({ item }) => ` +Failed to load hadith data. Please try again.
'; + divider.classList.add('d-none'); + console.error(err); + } +}; window.isObject = function (obj) { return obj === Object(obj);