Stylistic changes, commenting; no functional changes
This commit is contained in:
parent
404479be6c
commit
b053d4bd26
|
@ -1,13 +1,13 @@
|
||||||
// Saves a search-group of the given `name`, containing all search-engines of
|
// Saves a search-group of the given `name`, containing all search-engines of
|
||||||
// `preset` (which is an array of search-engine display-names truncated to 7 chars).
|
// `preset` (which is an array of search-engine display-names truncated to 7 chars).
|
||||||
function savepreset(preset, name) {
|
function savePreset(preset, name) {
|
||||||
let presets = Array();
|
let presets = Array();
|
||||||
let presetName = name.replace(',', ' ');
|
let presetName = name.replace(',', ' ');
|
||||||
try { presets = localStorage.getItem('presets').split(','); } catch {}
|
try { presets = localStorage.getItem('presets').split(','); } catch {}
|
||||||
|
|
||||||
try { localStorage.removeItem('preset_' + presetName); } catch {}
|
try { localStorage.removeItem('preset_' + presetName); } catch {}
|
||||||
|
|
||||||
presets = removefromarray(removeredundantitems(presets), presetName);
|
presets = removeFromArray(removeRedundantItems(presets), presetName);
|
||||||
presets.push(presetName);
|
presets.push(presetName);
|
||||||
|
|
||||||
localStorage.setItem('preset_' + presetName, preset);
|
localStorage.setItem('preset_' + presetName, preset);
|
||||||
|
@ -16,6 +16,6 @@ function savepreset(preset, name) {
|
||||||
|
|
||||||
|
|
||||||
// Load an array of all saved preset's names.
|
// Load an array of all saved preset's names.
|
||||||
function loadpresets() {
|
function loadPresets() {
|
||||||
try { return localStorage.getItem('presets').split(','); } catch { return []; }
|
try { return localStorage.getItem('presets').split(','); } catch { return []; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,180 +1,216 @@
|
||||||
document.getElementById("searchbox").focus();
|
document.getElementById("searchbox").focus();
|
||||||
|
|
||||||
const populateengines = (engines) => {
|
// Populates the .boxes div in popup.html with a list of available search-engines,
|
||||||
for (engine of engines) {
|
// adding a corresponding checkbox and label for each.
|
||||||
let checkbox = document.createElement("INPUT");
|
function populateEngines(engines) {
|
||||||
checkbox.type = "checkbox";
|
for (engine of engines) {
|
||||||
checkbox.setAttribute('value', 'engine ' + engine.name);
|
let checkbox = document.createElement("INPUT");
|
||||||
checkbox.id = engine.name.replace(' ', '-');
|
checkbox.type = "checkbox";
|
||||||
let label = document.createElement('label');
|
checkbox.setAttribute('value', 'engine ' + engine.name);
|
||||||
label.appendChild(document.createTextNode(engine.name));
|
checkbox.id = engine.name.replace(' ', '-');
|
||||||
document.getElementById("boxes").appendChild(checkbox);
|
let label = document.createElement('label');
|
||||||
document.getElementById("boxes").appendChild(label);
|
label.appendChild(document.createTextNode(engine.name));
|
||||||
document.getElementById("boxes").appendChild(document.createElement("BR"))
|
document.getElementById("boxes").appendChild(checkbox);
|
||||||
}
|
document.getElementById("boxes").appendChild(label);
|
||||||
|
document.getElementById("boxes").appendChild(document.createElement("BR"));
|
||||||
|
}
|
||||||
|
|
||||||
populatepresets();
|
populatePresets();
|
||||||
try { enablepresetbyname(localStorage.getItem('defaultPreset')); } catch {}
|
try { enablePresetByName(localStorage.getItem('defaultPreset')); } catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const populatepresets = () => {
|
|
||||||
let presetDropdown = document.getElementById("presets");
|
|
||||||
let presets = loadpresets()
|
|
||||||
|
|
||||||
while (presetDropdown.firstChild) {
|
// Populate the .presets selection input (drop-down menu) with all currently-saved
|
||||||
presetDropdown.removeChild(presetDropdown.lastChild);
|
// search-groups (presets).
|
||||||
}
|
function populatePresets() {
|
||||||
|
let presetDropdown = document.getElementById("presets");
|
||||||
|
let presets = loadPresets();
|
||||||
|
|
||||||
let noPreset = document.createElement("OPTION");
|
while (presetDropdown.firstChild) {
|
||||||
noPreset.setAttribute('value', 'noPreset');
|
presetDropdown.removeChild(presetDropdown.lastChild);
|
||||||
noPreset.innerText = '—';
|
}
|
||||||
presetDropdown.appendChild(noPreset);
|
|
||||||
|
|
||||||
for (presetName of presets) {
|
let noPreset = document.createElement("OPTION");
|
||||||
let option = document.createElement("OPTION");
|
noPreset.setAttribute('value', 'noPreset');
|
||||||
option.setAttribute('value', presetName);
|
noPreset.innerText = '—';
|
||||||
option.innerText = presetName;
|
presetDropdown.appendChild(noPreset);
|
||||||
presetDropdown.appendChild(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
let newPreset = document.createElement("OPTION");
|
for (presetName of presets) {
|
||||||
newPreset.setAttribute('value', 'newPreset');
|
let option = document.createElement("OPTION");
|
||||||
newPreset.innerText = 'Create new preset…';
|
option.setAttribute('value', presetName);
|
||||||
presetDropdown.appendChild(newPreset);
|
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})
|
// Search the input search-term on all enabled engines.
|
||||||
.then(() => {
|
function search(tab) {
|
||||||
for (selected of engineselection()) {
|
browser.tabs.query({active: true, currentWindow: true})
|
||||||
browser.search.search({
|
.then(() =>
|
||||||
query: document.getElementById("searchbox").value,
|
{
|
||||||
engine: selected,
|
for (selected of engineSelection()) {
|
||||||
});
|
browser.search.search({
|
||||||
}
|
query: document.getElementById("searchbox").value,
|
||||||
}
|
engine: selected,
|
||||||
);
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const enablepresetbyname = (name) => {
|
|
||||||
document.getElementById("presets").value = name;
|
// Enable a preset's corresponding search engines, by the preset's name.
|
||||||
try { enablepreset(localStorage.getItem('preset_' + name)); }
|
function enablePresetByName(name) {
|
||||||
catch {}
|
document.getElementById("presets").value = name;
|
||||||
|
try { enablePreset(localStorage.getItem('preset_' + name)); }
|
||||||
|
catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const enablepreset = (preset) => {
|
|
||||||
deselectengines();
|
// Check all search engines' check boxes by name, in a comma-delimited list.
|
||||||
for (engine of preset.split(",")) {
|
function enablePreset(preset) {
|
||||||
let checkbox = document.getElementById(engine.replace(' ', '-'));
|
deselectEngines();
|
||||||
checkbox.checked = true;
|
for (engine of preset.split(",")) {
|
||||||
}
|
let checkbox = document.getElementById(engine.replace(' ', '-'));
|
||||||
|
checkbox.checked = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removecurrentpreset = () => {
|
|
||||||
let presetName = document.getElementById("presets").value;
|
|
||||||
if (presetName == "noPreset" || presetName == "newPreset")
|
|
||||||
return;
|
|
||||||
|
|
||||||
try {
|
// Delete the currently-selected preset.
|
||||||
let presets = localStorage.getItem('presets').split(',');
|
function removeCurrentPreset() {
|
||||||
localStorage.removeItem('preset_' + presetName);
|
let presetName = document.getElementById("presets").value;
|
||||||
localStorage.setItem('presets', removefromarray(presets, presetName));
|
if (presetName == "noPreset" || presetName == "newPreset")
|
||||||
}
|
return;
|
||||||
catch {}
|
|
||||||
|
|
||||||
deselectengines();
|
try {
|
||||||
populatepresets();
|
let presets = localStorage.getItem('presets').split(',');
|
||||||
document.getElementById("presets").value = "noPreset";
|
localStorage.removeItem('preset_' + presetName);
|
||||||
|
localStorage.setItem('presets', removeFromArray(presets, presetName));
|
||||||
|
}
|
||||||
|
catch {}
|
||||||
|
|
||||||
|
deselectEngines();
|
||||||
|
populatePresets();
|
||||||
|
document.getElementById("presets").value = "noPreset";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Search-box is also used for setting the name of new
|
// Search-box is also used for setting the name of new
|
||||||
// presets; this sets the placeholder accordingly
|
// presets; this sets the placeholder accordingly
|
||||||
const searchboxaspresetname = () => {
|
function searchBoxAsPresetName() {
|
||||||
let textbox = document.getElementById("searchbox");
|
let textbox = document.getElementById("searchbox");
|
||||||
textbox.setAttribute("placeholder", "New preset name…");
|
textbox.setAttribute("placeholder", "New preset name…");
|
||||||
deselectengines();
|
deselectengines();
|
||||||
document.getElementById("searchbox").focus();
|
document.getElementById("searchbox").focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
const resetsearchbox = (clearText = false) => {
|
|
||||||
let textbox = document.getElementById("searchbox");
|
// Clear the search-box, and reset the placeholder.
|
||||||
textbox.setAttribute("placeholder", "Search…");
|
function resetSearchBox(clearText = false) {
|
||||||
if (clearText)
|
let textbox = document.getElementById("searchbox");
|
||||||
textbox.value = '';
|
textbox.setAttribute("placeholder", "Search…");
|
||||||
|
if (clearText)
|
||||||
|
textbox.value = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const deselectengines = () => {
|
|
||||||
for (checkbox of document.getElementsByTagName("INPUT"))
|
// Deselect all engines in the list.
|
||||||
checkbox.checked = false;
|
function deselectEngines() {
|
||||||
|
for (checkbox of document.getElementsByTagName("INPUT"))
|
||||||
|
checkbox.checked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const engineselection = () => {
|
|
||||||
let selection = [];
|
// Return a list of all selected search-engines, in a comma-delimited list.
|
||||||
for (engine of document.getElementsByTagName('INPUT')) {
|
function engineSelection() {
|
||||||
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
let selection = [];
|
||||||
}
|
for (engine of document.getElementsByTagName('INPUT')) {
|
||||||
return selection;
|
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
||||||
|
}
|
||||||
|
return selection;
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveselection = () => {
|
|
||||||
let textbox = document.getElementById("searchbox");
|
|
||||||
let selection = document.getElementById("presets").value;
|
|
||||||
let name = selection;
|
|
||||||
|
|
||||||
if (selection == "newPreset")
|
// Save all selected engines into a present; whether new or old.
|
||||||
name = textbox.value;
|
function saveSelection() {
|
||||||
savepreset(engineselection(), name);
|
let textbox = document.getElementById("searchbox");
|
||||||
// Update UI & listed presets
|
let selection = document.getElementById("presets").value;
|
||||||
resetsearchbox(true);
|
let name = selection;
|
||||||
populatepresets();
|
|
||||||
enablepresetbyname(name);
|
if (selection == "newPreset")
|
||||||
|
name = textbox.value;
|
||||||
|
savePreset(engineSelection(), name);
|
||||||
|
// Update UI & listed presets
|
||||||
|
resetSearchBox(true);
|
||||||
|
populatePresets();
|
||||||
|
enablePresetByName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const onpresetselected = (event) => {
|
|
||||||
let value = document.getElementById("presets").value;
|
|
||||||
if (value != "newPreset")
|
|
||||||
localStorage.setItem("defaultPreset", value);
|
|
||||||
resetsearchbox();
|
|
||||||
|
|
||||||
if (value == "noPreset")
|
// An event handler that corrects engine-selection whenever the preset is changed.
|
||||||
deselectengines();
|
function onPresetSelected(event) {
|
||||||
else if (value == "newPreset")
|
let value = document.getElementById("presets").value;
|
||||||
searchboxaspresetname();
|
if (value != "newPreset")
|
||||||
else
|
localStorage.setItem("defaultPreset", value);
|
||||||
enablepresetbyname(value);
|
resetSearchBox();
|
||||||
|
|
||||||
|
if (value == "noPreset")
|
||||||
|
deselectEngines();
|
||||||
|
else if (value == "newPreset")
|
||||||
|
searchBoxAsPresetName();
|
||||||
|
else
|
||||||
|
enablePresetByName(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const removefromarray = (array, item) => {
|
|
||||||
return array.filter((testItem, index) =>
|
// Remove a specific item from the given array.
|
||||||
{
|
function removeFromArray(array, item) {
|
||||||
return testItem != item;
|
return array.filter((testItem, index) =>
|
||||||
});
|
{
|
||||||
|
return testItem != item;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeredundantitems = (array) => {
|
|
||||||
return array.filter((item, index) =>
|
// Remove all repeated items from an array.
|
||||||
{
|
function removeRedundantItems(array) {
|
||||||
return (array.indexOf(item) === index);
|
return array.filter((item, index) =>
|
||||||
})
|
{
|
||||||
|
return (array.indexOf(item) === index);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.search.get().then(populateengines);
|
|
||||||
|
|
||||||
|
// Populate the list of selectable search-engines.
|
||||||
|
browser.search.get().then(populateEngines);
|
||||||
|
|
||||||
|
|
||||||
|
// Watch for the user hitting RETURN and triggering a result.
|
||||||
document.getElementById("searchbox").addEventListener("keypress", key => {
|
document.getElementById("searchbox").addEventListener("keypress", key => {
|
||||||
if (event.key == "Enter") search();
|
if (event.key == "Enter") search();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Wait for a preset to be selected, so that we can change search-engine selections.
|
||||||
document.addEventListener("change", e => {
|
document.addEventListener("change", e => {
|
||||||
if (e.target.id == "presets")
|
if (e.target.id == "presets")
|
||||||
onpresetselected();
|
onPresetSelected();
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// Handle searches, as well and save/removal of search presets.
|
||||||
document.addEventListener("click", e => {
|
document.addEventListener("click", e => {
|
||||||
if (e.target.classList.contains("search"))
|
if (e.target.classList.contains("search"))
|
||||||
search();
|
search();
|
||||||
else if (e.target.classList.contains("save"))
|
else if (e.target.classList.contains("save"))
|
||||||
saveselection();
|
saveSelection();
|
||||||
else if (e.target.classList.contains("remove"))
|
else if (e.target.classList.contains("remove"))
|
||||||
removecurrentpreset();
|
removeCurrentPreset();
|
||||||
});
|
});
|
||||||
|
|
Ŝarĝante…
Reference in New Issue