2019-05-08 12:45:21 -05:00
|
|
|
document.getElementById("searchbox").focus();
|
2019-05-06 08:20:28 -05:00
|
|
|
|
2022-11-26 12:48:58 -06:00
|
|
|
const populateengines = (engines) => {
|
2019-05-06 08:20:28 -05:00
|
|
|
for (engine of engines) {
|
|
|
|
let checkbox = document.createElement("INPUT");
|
|
|
|
checkbox.type = "checkbox";
|
|
|
|
checkbox.setAttribute('value', 'engine ' + engine.name);
|
2022-11-26 12:48:58 -06:00
|
|
|
checkbox.id = engine.name.replace(' ', '-');
|
2019-05-06 08:20:28 -05:00
|
|
|
let label = document.createElement('label');
|
|
|
|
label.appendChild(document.createTextNode(engine.name));
|
|
|
|
document.getElementById("boxes").appendChild(checkbox);
|
|
|
|
document.getElementById("boxes").appendChild(label);
|
2019-05-08 12:45:21 -05:00
|
|
|
document.getElementById("boxes").appendChild(document.createElement("BR"))
|
2019-05-06 08:20:28 -05:00
|
|
|
}
|
2022-11-26 12:48:58 -06:00
|
|
|
|
|
|
|
populatepresets();
|
|
|
|
try { enablepresetbyname(localStorage.getItem('defaultPreset')); } catch {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const populatepresets = () => {
|
|
|
|
let presetDropdown = document.getElementById("presets");
|
2023-04-04 08:17:50 -05:00
|
|
|
let presets = loadpresets()
|
2022-11-26 12:48:58 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-05-08 12:45:21 -05:00
|
|
|
|
|
|
|
const search = tab => {
|
|
|
|
browser.tabs.query({active: true, currentWindow: true})
|
2022-11-26 12:48:58 -06:00
|
|
|
.then(() => {
|
|
|
|
for (selected of engineselection()) {
|
|
|
|
browser.search.search({
|
2019-05-08 12:45:21 -05:00
|
|
|
query: document.getElementById("searchbox").value,
|
|
|
|
engine: selected,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 12:48:58 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:15:02 -06:00
|
|
|
const removecurrentpreset = () => {
|
|
|
|
let presetName = document.getElementById("presets").value;
|
|
|
|
if (presetName == "noPreset" || presetName == "newPreset")
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let presets = localStorage.getItem('presets').split(',');
|
|
|
|
localStorage.removeItem('preset_' + presetName);
|
|
|
|
localStorage.setItem('presets', removefromarray(presets, presetName));
|
|
|
|
}
|
|
|
|
catch {}
|
|
|
|
|
|
|
|
deselectengines();
|
|
|
|
populatepresets();
|
|
|
|
document.getElementById("presets").value = "noPreset";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 12:48:58 -06:00
|
|
|
// 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.setAttribute("placeholder", "New preset name…");
|
|
|
|
deselectengines();
|
2022-11-26 13:39:20 -06:00
|
|
|
document.getElementById("searchbox").focus();
|
2022-11-26 12:48:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-06 08:20:28 -05:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:45:21 -05:00
|
|
|
const engineselection = () => {
|
|
|
|
let selection = [];
|
2019-05-06 08:20:28 -05:00
|
|
|
for (engine of document.getElementsByTagName('INPUT')) {
|
2019-05-08 12:45:21 -05:00
|
|
|
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
2019-05-06 08:20:28 -05:00
|
|
|
}
|
2019-05-08 12:45:21 -05:00
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
const saveselection = () => {
|
2023-04-04 08:17:50 -05:00
|
|
|
let textbox = document.getElementById("searchbox");
|
|
|
|
let selection = document.getElementById("presets").value;
|
|
|
|
let name = selection;
|
|
|
|
|
|
|
|
if (selection == "newPreset")
|
|
|
|
name = textbox.value;
|
|
|
|
savepreset(engineselection(), name);
|
|
|
|
// Update UI & listed presets
|
|
|
|
resetsearchbox(true);
|
|
|
|
populatepresets();
|
|
|
|
enablepresetbyname(name);
|
2019-05-08 12:45:21 -05:00
|
|
|
}
|
|
|
|
|
2022-11-26 13:39:20 -06:00
|
|
|
const onpresetselected = (event) => {
|
|
|
|
let value = document.getElementById("presets").value;
|
2022-11-26 12:48:58 -06:00
|
|
|
if (value != "newPreset")
|
|
|
|
localStorage.setItem("defaultPreset", value);
|
|
|
|
resetsearchbox();
|
|
|
|
|
|
|
|
if (value == "noPreset")
|
|
|
|
deselectengines();
|
|
|
|
else if (value == "newPreset")
|
|
|
|
searchboxaspresetname();
|
|
|
|
else
|
|
|
|
enablepresetbyname(value);
|
2022-11-26 13:15:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const removefromarray = (array, item) => {
|
|
|
|
return array.filter((testItem, index) =>
|
|
|
|
{
|
|
|
|
return testItem != item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeredundantitems = (array) => {
|
|
|
|
return array.filter((item, index) =>
|
|
|
|
{
|
|
|
|
return (array.indexOf(item) === index);
|
|
|
|
})
|
2022-11-26 12:48:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
browser.search.get().then(populateengines);
|
2019-05-08 12:45:21 -05:00
|
|
|
|
|
|
|
document.getElementById("searchbox").addEventListener("keypress", key => {
|
|
|
|
if (event.key == "Enter") search();
|
|
|
|
});
|
|
|
|
|
2022-11-26 13:39:20 -06:00
|
|
|
document.addEventListener("change", e => {
|
|
|
|
if (e.target.id == "presets")
|
|
|
|
onpresetselected();
|
|
|
|
})
|
|
|
|
|
2019-05-08 12:45:21 -05:00
|
|
|
document.addEventListener("click", e => {
|
2022-11-26 12:48:58 -06:00
|
|
|
if (e.target.classList.contains("search"))
|
|
|
|
search();
|
2022-11-26 13:15:02 -06:00
|
|
|
else if (e.target.classList.contains("save"))
|
2022-11-26 12:48:58 -06:00
|
|
|
saveselection();
|
2022-11-26 13:15:02 -06:00
|
|
|
else if (e.target.classList.contains("remove"))
|
|
|
|
removecurrentpreset();
|
2019-05-06 08:20:28 -05:00
|
|
|
});
|