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,6 +1,8 @@
|
||||||
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,
|
||||||
|
// adding a corresponding checkbox and label for each.
|
||||||
|
function populateEngines(engines) {
|
||||||
for (engine of engines) {
|
for (engine of engines) {
|
||||||
let checkbox = document.createElement("INPUT");
|
let checkbox = document.createElement("INPUT");
|
||||||
checkbox.type = "checkbox";
|
checkbox.type = "checkbox";
|
||||||
|
@ -10,16 +12,19 @@ const populateengines = (engines) => {
|
||||||
label.appendChild(document.createTextNode(engine.name));
|
label.appendChild(document.createTextNode(engine.name));
|
||||||
document.getElementById("boxes").appendChild(checkbox);
|
document.getElementById("boxes").appendChild(checkbox);
|
||||||
document.getElementById("boxes").appendChild(label);
|
document.getElementById("boxes").appendChild(label);
|
||||||
document.getElementById("boxes").appendChild(document.createElement("BR"))
|
document.getElementById("boxes").appendChild(document.createElement("BR"));
|
||||||
}
|
}
|
||||||
|
|
||||||
populatepresets();
|
populatePresets();
|
||||||
try { enablepresetbyname(localStorage.getItem('defaultPreset')); } catch {}
|
try { enablePresetByName(localStorage.getItem('defaultPreset')); } catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const populatepresets = () => {
|
|
||||||
|
// Populate the .presets selection input (drop-down menu) with all currently-saved
|
||||||
|
// search-groups (presets).
|
||||||
|
function populatePresets() {
|
||||||
let presetDropdown = document.getElementById("presets");
|
let presetDropdown = document.getElementById("presets");
|
||||||
let presets = loadpresets()
|
let presets = loadPresets();
|
||||||
|
|
||||||
while (presetDropdown.firstChild) {
|
while (presetDropdown.firstChild) {
|
||||||
presetDropdown.removeChild(presetDropdown.lastChild);
|
presetDropdown.removeChild(presetDropdown.lastChild);
|
||||||
|
@ -43,10 +48,13 @@ const populatepresets = () => {
|
||||||
presetDropdown.appendChild(newPreset);
|
presetDropdown.appendChild(newPreset);
|
||||||
}
|
}
|
||||||
|
|
||||||
const search = tab => {
|
|
||||||
|
// Search the input search-term on all enabled engines.
|
||||||
|
function search(tab) {
|
||||||
browser.tabs.query({active: true, currentWindow: true})
|
browser.tabs.query({active: true, currentWindow: true})
|
||||||
.then(() => {
|
.then(() =>
|
||||||
for (selected of engineselection()) {
|
{
|
||||||
|
for (selected of engineSelection()) {
|
||||||
browser.search.search({
|
browser.search.search({
|
||||||
query: document.getElementById("searchbox").value,
|
query: document.getElementById("searchbox").value,
|
||||||
engine: selected,
|
engine: selected,
|
||||||
|
@ -56,21 +64,27 @@ const search = tab => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const enablepresetbyname = (name) => {
|
|
||||||
|
// Enable a preset's corresponding search engines, by the preset's name.
|
||||||
|
function enablePresetByName(name) {
|
||||||
document.getElementById("presets").value = name;
|
document.getElementById("presets").value = name;
|
||||||
try { enablepreset(localStorage.getItem('preset_' + name)); }
|
try { enablePreset(localStorage.getItem('preset_' + name)); }
|
||||||
catch {}
|
catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const enablepreset = (preset) => {
|
|
||||||
deselectengines();
|
// Check all search engines' check boxes by name, in a comma-delimited list.
|
||||||
|
function enablePreset(preset) {
|
||||||
|
deselectEngines();
|
||||||
for (engine of preset.split(",")) {
|
for (engine of preset.split(",")) {
|
||||||
let checkbox = document.getElementById(engine.replace(' ', '-'));
|
let checkbox = document.getElementById(engine.replace(' ', '-'));
|
||||||
checkbox.checked = true;
|
checkbox.checked = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removecurrentpreset = () => {
|
|
||||||
|
// Delete the currently-selected preset.
|
||||||
|
function removeCurrentPreset() {
|
||||||
let presetName = document.getElementById("presets").value;
|
let presetName = document.getElementById("presets").value;
|
||||||
if (presetName == "noPreset" || presetName == "newPreset")
|
if (presetName == "noPreset" || presetName == "newPreset")
|
||||||
return;
|
return;
|
||||||
|
@ -78,38 +92,44 @@ const removecurrentpreset = () => {
|
||||||
try {
|
try {
|
||||||
let presets = localStorage.getItem('presets').split(',');
|
let presets = localStorage.getItem('presets').split(',');
|
||||||
localStorage.removeItem('preset_' + presetName);
|
localStorage.removeItem('preset_' + presetName);
|
||||||
localStorage.setItem('presets', removefromarray(presets, presetName));
|
localStorage.setItem('presets', removeFromArray(presets, presetName));
|
||||||
}
|
}
|
||||||
catch {}
|
catch {}
|
||||||
|
|
||||||
deselectengines();
|
deselectEngines();
|
||||||
populatepresets();
|
populatePresets();
|
||||||
document.getElementById("presets").value = "noPreset";
|
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) => {
|
|
||||||
|
// Clear the search-box, and reset the placeholder.
|
||||||
|
function resetSearchBox(clearText = false) {
|
||||||
let textbox = document.getElementById("searchbox");
|
let textbox = document.getElementById("searchbox");
|
||||||
textbox.setAttribute("placeholder", "Search…");
|
textbox.setAttribute("placeholder", "Search…");
|
||||||
if (clearText)
|
if (clearText)
|
||||||
textbox.value = '';
|
textbox.value = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const deselectengines = () => {
|
|
||||||
|
// Deselect all engines in the list.
|
||||||
|
function deselectEngines() {
|
||||||
for (checkbox of document.getElementsByTagName("INPUT"))
|
for (checkbox of document.getElementsByTagName("INPUT"))
|
||||||
checkbox.checked = false;
|
checkbox.checked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const engineselection = () => {
|
|
||||||
|
// Return a list of all selected search-engines, in a comma-delimited list.
|
||||||
|
function engineSelection() {
|
||||||
let selection = [];
|
let selection = [];
|
||||||
for (engine of document.getElementsByTagName('INPUT')) {
|
for (engine of document.getElementsByTagName('INPUT')) {
|
||||||
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
||||||
|
@ -117,64 +137,80 @@ const engineselection = () => {
|
||||||
return selection;
|
return selection;
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveselection = () => {
|
|
||||||
|
// Save all selected engines into a present; whether new or old.
|
||||||
|
function saveSelection() {
|
||||||
let textbox = document.getElementById("searchbox");
|
let textbox = document.getElementById("searchbox");
|
||||||
let selection = document.getElementById("presets").value;
|
let selection = document.getElementById("presets").value;
|
||||||
let name = selection;
|
let name = selection;
|
||||||
|
|
||||||
if (selection == "newPreset")
|
if (selection == "newPreset")
|
||||||
name = textbox.value;
|
name = textbox.value;
|
||||||
savepreset(engineselection(), name);
|
savePreset(engineSelection(), name);
|
||||||
// Update UI & listed presets
|
// Update UI & listed presets
|
||||||
resetsearchbox(true);
|
resetSearchBox(true);
|
||||||
populatepresets();
|
populatePresets();
|
||||||
enablepresetbyname(name);
|
enablePresetByName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const onpresetselected = (event) => {
|
|
||||||
|
// An event handler that corrects engine-selection whenever the preset is changed.
|
||||||
|
function onPresetSelected(event) {
|
||||||
let value = document.getElementById("presets").value;
|
let value = document.getElementById("presets").value;
|
||||||
if (value != "newPreset")
|
if (value != "newPreset")
|
||||||
localStorage.setItem("defaultPreset", value);
|
localStorage.setItem("defaultPreset", value);
|
||||||
resetsearchbox();
|
resetSearchBox();
|
||||||
|
|
||||||
if (value == "noPreset")
|
if (value == "noPreset")
|
||||||
deselectengines();
|
deselectEngines();
|
||||||
else if (value == "newPreset")
|
else if (value == "newPreset")
|
||||||
searchboxaspresetname();
|
searchBoxAsPresetName();
|
||||||
else
|
else
|
||||||
enablepresetbyname(value);
|
enablePresetByName(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const removefromarray = (array, item) => {
|
|
||||||
|
// Remove a specific item from the given array.
|
||||||
|
function removeFromArray(array, item) {
|
||||||
return array.filter((testItem, index) =>
|
return array.filter((testItem, index) =>
|
||||||
{
|
{
|
||||||
return testItem != item;
|
return testItem != item;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeredundantitems = (array) => {
|
|
||||||
|
// Remove all repeated items from an array.
|
||||||
|
function removeRedundantItems(array) {
|
||||||
return array.filter((item, index) =>
|
return array.filter((item, index) =>
|
||||||
{
|
{
|
||||||
return (array.indexOf(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