Skip to content

Commit cdf8926

Browse files
authored
Merge pull request #917 from rgantzos/main
Improve search system
2 parents f443f92 + 59e5077 commit cdf8926

File tree

2 files changed

+122
-30
lines changed

2 files changed

+122
-30
lines changed

extras/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ <h1>
102102
</svg>
103103
</h1>
104104
<div class="suggested-features"></div>
105-
<h1 style="position: relative; top: 0.5rem">All features</h1>
105+
<h1 style="position: relative; top: 0.5rem" data-id="all-features-text">All features</h1>
106106
</div>
107107
<div class="enabled" style="display: none;">
108108
<img style="width: 55rem; border-radius: 1rem;" src="./icons/discord.svg">

extras/popup/popup.js

Lines changed: 121 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -501,23 +501,6 @@ function getWords(text, search) {
501501
return matchedPercentage > 0.5;
502502
}
503503

504-
document.querySelector(".searchbar").addEventListener("input", function () {
505-
if (document.querySelector(".welcome")) {
506-
if (document.querySelector(".searchbar").value) {
507-
document.querySelector(".welcome").style.display = "none";
508-
} else {
509-
document.querySelector(".welcome").style.display = null;
510-
}
511-
}
512-
document.querySelectorAll(".feature").forEach(function (el) {
513-
if (getWords(el.innerText, document.querySelector(".searchbar").value)) {
514-
el.style.display = null;
515-
} else {
516-
el.style.display = "none";
517-
}
518-
});
519-
});
520-
521504
if (document.querySelector(".settingsButton")) {
522505
document
523506
.querySelector(".settingsButton")
@@ -562,6 +545,8 @@ async function returnFeatureCode() {
562545
}
563546
}
564547

548+
let FEATURES = [];
549+
565550
async function getFeatures() {
566551
var languageData = await getFeatureLanguageData();
567552
const settings = (await chrome.storage.sync.get("features")).features || "";
@@ -587,6 +572,13 @@ async function getFeatures() {
587572
}
588573
div.dataset.type = feature.type.join("");
589574

575+
FEATURES.push({
576+
title: feature.title,
577+
description: feature.description,
578+
id: feature.id,
579+
new: feature.versionAdded === "v" + chrome.runtime.getManifest().version,
580+
});
581+
590582
var h3 = document.createElement("h3");
591583
h3.textContent =
592584
languageData[feature.id + "/title"]?.message || feature.title;
@@ -693,16 +685,16 @@ async function getFeatures() {
693685

694686
if (typeof option.type === "string") {
695687
let OPTION_TYPES = {
696-
"string": 0,
697-
"boolean": 1,
698-
"number": 2,
699-
"color": 3,
700-
"select": 4,
701-
}
702-
option.type = OPTION_TYPES[option.type]
688+
string: 0,
689+
boolean: 1,
690+
number: 2,
691+
color: 3,
692+
select: 4,
693+
};
694+
option.type = OPTION_TYPES[option.type];
703695
}
704696

705-
let type = option.type
697+
let type = option.type;
706698
if (type === 4) {
707699
var optionDiv = document.createElement("div");
708700
optionDiv.className = "option";
@@ -781,7 +773,7 @@ async function getFeatures() {
781773
];
782774
input.value = optionData || "";
783775
input.placeholder = `Enter ${input.type}`;
784-
input.dataset.validators = JSON.stringify(option.validators || {})
776+
input.dataset.validators = JSON.stringify(option.validators || {});
785777
var optionDiv = document.createElement("div");
786778
optionDiv.className = "option";
787779
var label = document.createElement("label");
@@ -812,7 +804,7 @@ async function getFeatures() {
812804
var validation = JSON.parse(atob(this.dataset.validation));
813805
var ready = true;
814806
var input = this;
815-
let validators = JSON.parse(this.dataset.validators)
807+
let validators = JSON.parse(this.dataset.validators);
816808
validation.forEach(function (validate) {
817809
if (ready) {
818810
input.style.outline = "none";
@@ -838,12 +830,12 @@ async function getFeatures() {
838830
if (ready) {
839831
if (validators.min) {
840832
if (this.value < validators.min) {
841-
this.value = validators.min
833+
this.value = validators.min;
842834
}
843835
}
844836
if (validators.max) {
845837
if (this.value > validators.max) {
846-
this.value = validators.max
838+
this.value = validators.max;
847839
}
848840
}
849841
if (this.type !== "checkbox") {
@@ -1502,3 +1494,103 @@ document
15021494
});
15031495
}
15041496
});
1497+
1498+
function searchAndSort(query) {
1499+
const lowerCaseQuery = query.toLowerCase();
1500+
1501+
const scoredData = FEATURES.map((item) => {
1502+
const nameMatches = item.title.toLowerCase().includes(lowerCaseQuery)
1503+
? 1
1504+
: 0;
1505+
const descriptionMatches = item.description
1506+
.toLowerCase()
1507+
.includes(lowerCaseQuery)
1508+
? 1
1509+
: 0;
1510+
const totalMatches = nameMatches + descriptionMatches;
1511+
1512+
return {
1513+
...item,
1514+
relevance: totalMatches,
1515+
};
1516+
});
1517+
1518+
scoredData.sort((a, b) => b.relevance - a.relevance);
1519+
1520+
const filteredData = scoredData.filter((item) => item.relevance > 0);
1521+
1522+
return filteredData;
1523+
}
1524+
1525+
let searchbar = document.querySelector(".searchbar");
1526+
searchbar.addEventListener("input", function () {
1527+
if (document.querySelector(".welcome")) {
1528+
if (searchbar.value) {
1529+
document.querySelector(".welcome").style.display = "none";
1530+
} else {
1531+
document.querySelector(".welcome").style.display = null;
1532+
}
1533+
}
1534+
if (!searchbar.value) {
1535+
if (document.querySelector('h1[data-id="all-features-text"]')) {
1536+
document.querySelector('h1[data-id="all-features-text"]').textContent =
1537+
"All features";
1538+
1539+
document.querySelector(".suggested h1").style.display = null;
1540+
document.querySelector(".suggested-features").style.display = null;
1541+
}
1542+
1543+
document.querySelectorAll(".feature").forEach(function (el) {
1544+
el.style.display = null;
1545+
});
1546+
1547+
for (var i in FEATURES) {
1548+
document
1549+
.querySelector(".settings")
1550+
.appendChild(
1551+
document.querySelector(
1552+
`div.settings .feature[data-id=${FEATURES[i].id}]`
1553+
)
1554+
);
1555+
}
1556+
1557+
for (var i in FEATURES.filter((el) => el.new)) {
1558+
document
1559+
.querySelector(".settings")
1560+
.prepend(
1561+
document.querySelector(
1562+
`div.settings .feature[data-id=${
1563+
FEATURES.filter((el) => el.new)[i].id
1564+
}]`
1565+
)
1566+
);
1567+
}
1568+
} else {
1569+
if (document.querySelector('h1[data-id="all-features-text"]')) {
1570+
document.querySelector('h1[data-id="all-features-text"]').textContent =
1571+
"Results";
1572+
1573+
document.querySelector(".suggested h1").style.display = "none";
1574+
document.querySelector(".suggested-features").style.display = "none";
1575+
}
1576+
1577+
let results = searchAndSort(searchbar.value);
1578+
document.querySelectorAll(".feature").forEach(function (el) {
1579+
if (results.find((result) => result.id === el.dataset.id)) {
1580+
el.style.display = null;
1581+
} else {
1582+
el.style.display = "none";
1583+
}
1584+
});
1585+
1586+
for (var i in results) {
1587+
document
1588+
.querySelector(".settings")
1589+
.appendChild(
1590+
document.querySelector(
1591+
`div.settings .feature[data-id=${results[i].id}]`
1592+
)
1593+
);
1594+
}
1595+
}
1596+
});

0 commit comments

Comments
 (0)