From db7f5b4b9b607aabe6f745956d6487365fdd0d57 Mon Sep 17 00:00:00 2001
From: Jaidyn Levesque <10477760+JadedCtrl@users.noreply.github.com>
Date: Sat, 26 Nov 2022 12:48:58 -0600
Subject: [PATCH] Support for multiple combos ("presets") of engines
Instead of having only one default combination of engines,
now an arbitrary amount of combinations under custom names
is supported.
---
popup/search.html | 6 +-
popup/search.js | 142 ++++++++++++++++++++++++++++++++++++++++------
2 files changed, 128 insertions(+), 20 deletions(-)
diff --git a/popup/search.html b/popup/search.html
index 92b30bc..757f57f 100644
--- a/popup/search.html
+++ b/popup/search.html
@@ -4,12 +4,14 @@
-
+
Search
+
+
- Save selection as default
+ Save preset
diff --git a/popup/search.js b/popup/search.js
index c12ba6a..54bcdf8 100644
--- a/popup/search.js
+++ b/popup/search.js
@@ -1,35 +1,119 @@
document.getElementById("searchbox").focus();
-browser.search.get().then(engines => {
+const populateengines = (engines) => {
for (engine of engines) {
let checkbox = document.createElement("INPUT");
checkbox.type = "checkbox";
checkbox.setAttribute('value', 'engine ' + engine.name);
- try {
- let defaultengines = localStorage.getItem('defaultengines').split(",");
- for (eng of defaultengines) {
- if (engine.name == eng) checkbox.checked = true;
- }
- } catch {}
+ checkbox.id = engine.name.replace(' ', '-');
let label = document.createElement('label');
label.appendChild(document.createTextNode(engine.name));
document.getElementById("boxes").appendChild(checkbox);
document.getElementById("boxes").appendChild(label);
document.getElementById("boxes").appendChild(document.createElement("BR"))
}
-});
+
+ populatepresets();
+ try { enablepresetbyname(localStorage.getItem('defaultPreset')); } catch {}
+}
+
+const populatepresets = () => {
+ let presetDropdown = document.getElementById("presets");
+ let presets = Array();
+ try { presets = localStorage.getItem('presets').split(','); } catch {}
+
+ while (presetDropdown.firstChild) {
+ presetDropdown.removeChild(presetDropdown.lastChild);
+ }
+
+ let noPreset = document.createElement("OPTION");
+ noPreset.setAttribute('value', 'noPreset');
+ noPreset.innerText = '—';
+ presetDropdown.appendChild(noPreset);
+
+ for (presetName of presets) {
+ let option = document.createElement("OPTION");
+ option.setAttribute('value', presetName);
+ option.innerText = presetName;
+ presetDropdown.appendChild(option);
+ }
+
+ let newPreset = document.createElement("OPTION");
+ newPreset.setAttribute('value', 'newPreset');
+ newPreset.innerText = 'Create new preset…';
+ presetDropdown.appendChild(newPreset);
+}
const search = tab => {
browser.tabs.query({active: true, currentWindow: true})
- .then(() => {
- for (selected of engineselection()) {
- browser.search.search({
+ .then(() => {
+ for (selected of engineselection()) {
+ browser.search.search({
query: document.getElementById("searchbox").value,
engine: selected,
});
}
}
- );
+ );
+}
+
+const enablepresetbyname = (name) => {
+ document.getElementById("presets").value = name;
+ try { enablepreset(localStorage.getItem('preset_' + name)); }
+ catch {}
+}
+
+const enablepreset = (preset) => {
+ deselectengines();
+ for (engine of preset.split(",")) {
+ let checkbox = document.getElementById(engine.replace(' ', '-'));
+ checkbox.checked = true;
+ }
+}
+
+const savepreset = (preset, name) => {
+ let presets = Array();
+ let presetName = name.replace(',', ' ');
+ try { presets = localStorage.getItem('presets').split(','); } catch {}
+
+ try { localStorage.removeItem('preset_' + presetName); } catch {}
+
+ let sanitizedPresets = presets.filter((thisName, i) =>
+ {
+ return (presets.indexOf(thisName) === i)
+ && (thisName != name);
+ });
+
+ sanitizedPresets.push(presetName);
+
+ localStorage.setItem('preset_' + presetName, preset);
+ localStorage.setItem('presets', sanitizedPresets);
+
+ // Update UI & listed presets
+ resetsearchbox(true);
+ populatepresets();
+ enablepresetbyname(name);
+}
+
+// Search-box is also used for setting the name of new
+// presets; this sets the placeholder accordingly
+const searchboxaspresetname = () => {
+ let textbox = document.getElementById("searchbox");
+ textbox.focus();
+ textbox.setAttribute("placeholder", "New preset name…");
+ deselectengines();
+}
+
+const resetsearchbox = (clearText = false) => {
+ let textbox = document.getElementById("searchbox");
+ textbox.setAttribute("placeholder", "Search…");
+ if (clearText)
+ textbox.value = '';
+}
+
+const deselectengines = () => {
+ for (checkbox of document.getElementsByTagName("INPUT"))
+ checkbox.checked = false;
}
const engineselection = () => {
@@ -41,18 +125,40 @@ const engineselection = () => {
}
const saveselection = () => {
- try {
- localStorage.removeItem('defaultengines');
- } catch {}
- localStorage.setItem('defaultengines', engineselection());
+ let textbox = document.getElementById("searchbox");
+ let selection = document.getElementById("presets").value;
+ let name = selection;
+
+ if (selection == "newPreset")
+ name = textbox.value;
+ savepreset(engineselection(), name);
}
+const onpresetselected = (value) => {
+ if (value != "newPreset")
+ localStorage.setItem("defaultPreset", value);
+ resetsearchbox();
+
+ if (value == "noPreset")
+ deselectengines();
+ else if (value == "newPreset")
+ searchboxaspresetname();
+ else
+ enablepresetbyname(value);
+}
+
+browser.search.get().then(populateengines);
document.getElementById("searchbox").addEventListener("keypress", key => {
if (event.key == "Enter") search();
});
document.addEventListener("click", e => {
- if (e.target.classList.contains("search")) search();
- else if (e.target.classList.contains("default")) saveselection();
+ if (e.target.classList.contains("search"))
+ search();
+ else if (e.target.classList.contains("default"))
+ saveselection();
+ else if (e.target.tagName == "OPTION") {
+ onpresetselected(e.target.getAttribute("value"));
+ }
});