From 3ff5ba42baa5b8713e70e3d04aa210cce1b2be05 Mon Sep 17 00:00:00 2001 From: rgantzos <86856959+rgantzos@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:44:13 -0700 Subject: [PATCH] Better search --- extras/popup/popup.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/extras/popup/popup.js b/extras/popup/popup.js index 98ca971b..b0b064e0 100644 --- a/extras/popup/popup.js +++ b/extras/popup/popup.js @@ -457,6 +457,25 @@ document.querySelector(".support-btn")?.addEventListener("click", function () { document.querySelector(".searchbar").placeholder = chrome.i18n.getMessage("search") || "search"; +function getWords(text, search) { + if (!search) return true; + if (!search.includes(" ")) return text.toLowerCase().includes(search.toLowerCase()); + text = text.replaceAll("\n", " ").replaceAll("Credits:", "").replaceAll("Updated", "").replaceAll("New", "").toLowerCase() + search = search.toLowerCase() + while (text.includes(" ")) { + text = text.replaceAll(" ", " ") + } + var oneWords = text.includes(" ") ? text.split(" ") : [text] + + var twoWords = search.includes(" ") ? search.split(" ") : [search] + + var matchWords = oneWords.filter((el) => twoWords.includes(el)).length + + var matchedPercentage = matchWords / twoWords.length + + return matchedPercentage > .3 +} + document.querySelector(".searchbar").addEventListener("input", function () { if (document.querySelector(".welcome")) { if (document.querySelector(".searchbar").value) { @@ -467,11 +486,7 @@ document.querySelector(".searchbar").addEventListener("input", function () { } document.querySelectorAll(".feature").forEach(function (el) { if ( - ( - el.querySelector("h3").textContent.toLowerCase() + - el.querySelector("p").textContent.toLowerCase() + - el.querySelector("span").textContent.toLowerCase() - ).includes(document.querySelector(".searchbar").value.toLowerCase()) + getWords(el.innerText, document.querySelector(".searchbar").value) ) { el.style.display = null; } else {