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
|
||||
// `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 presetName = name.replace(',', ' ');
|
||||
try { presets = localStorage.getItem('presets').split(','); } catch {}
|
||||
|
||||
try { localStorage.removeItem('preset_' + presetName); } catch {}
|
||||
|
||||
presets = removefromarray(removeredundantitems(presets), presetName);
|
||||
presets = removeFromArray(removeRedundantItems(presets), presetName);
|
||||
presets.push(presetName);
|
||||
|
||||
localStorage.setItem('preset_' + presetName, preset);
|
||||
|
@ -16,6 +16,6 @@ function savepreset(preset, name) {
|
|||
|
||||
|
||||
// Load an array of all saved preset's names.
|
||||
function loadpresets() {
|
||||
function loadPresets() {
|
||||
try { return localStorage.getItem('presets').split(','); } catch { return []; }
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
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) {
|
||||
let checkbox = document.createElement("INPUT");
|
||||
checkbox.type = "checkbox";
|
||||
|
@ -10,16 +12,19 @@ const populateengines = (engines) => {
|
|||
label.appendChild(document.createTextNode(engine.name));
|
||||
document.getElementById("boxes").appendChild(checkbox);
|
||||
document.getElementById("boxes").appendChild(label);
|
||||
document.getElementById("boxes").appendChild(document.createElement("BR"))
|
||||
document.getElementById("boxes").appendChild(document.createElement("BR"));
|
||||
}
|
||||
|
||||
populatepresets();
|
||||
try { enablepresetbyname(localStorage.getItem('defaultPreset')); } catch {}
|
||||
populatePresets();
|
||||
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 presets = loadpresets()
|
||||
let presets = loadPresets();
|
||||
|
||||
while (presetDropdown.firstChild) {
|
||||
presetDropdown.removeChild(presetDropdown.lastChild);
|
||||
|
@ -43,10 +48,13 @@ const populatepresets = () => {
|
|||
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})
|
||||
.then(() => {
|
||||
for (selected of engineselection()) {
|
||||
.then(() =>
|
||||
{
|
||||
for (selected of engineSelection()) {
|
||||
browser.search.search({
|
||||
query: document.getElementById("searchbox").value,
|
||||
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;
|
||||
try { enablepreset(localStorage.getItem('preset_' + 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.
|
||||
function enablePreset(preset) {
|
||||
deselectEngines();
|
||||
for (engine of preset.split(",")) {
|
||||
let checkbox = document.getElementById(engine.replace(' ', '-'));
|
||||
checkbox.checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
const removecurrentpreset = () => {
|
||||
|
||||
// Delete the currently-selected preset.
|
||||
function removeCurrentPreset() {
|
||||
let presetName = document.getElementById("presets").value;
|
||||
if (presetName == "noPreset" || presetName == "newPreset")
|
||||
return;
|
||||
|
@ -78,38 +92,44 @@ const removecurrentpreset = () => {
|
|||
try {
|
||||
let presets = localStorage.getItem('presets').split(',');
|
||||
localStorage.removeItem('preset_' + presetName);
|
||||
localStorage.setItem('presets', removefromarray(presets, presetName));
|
||||
localStorage.setItem('presets', removeFromArray(presets, presetName));
|
||||
}
|
||||
catch {}
|
||||
|
||||
deselectengines();
|
||||
populatepresets();
|
||||
deselectEngines();
|
||||
populatePresets();
|
||||
document.getElementById("presets").value = "noPreset";
|
||||
}
|
||||
|
||||
|
||||
// Search-box is also used for setting the name of new
|
||||
// presets; this sets the placeholder accordingly
|
||||
const searchboxaspresetname = () => {
|
||||
function searchBoxAsPresetName() {
|
||||
let textbox = document.getElementById("searchbox");
|
||||
textbox.setAttribute("placeholder", "New preset name…");
|
||||
deselectengines();
|
||||
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");
|
||||
textbox.setAttribute("placeholder", "Search…");
|
||||
if (clearText)
|
||||
textbox.value = '';
|
||||
}
|
||||
|
||||
const deselectengines = () => {
|
||||
|
||||
// Deselect all engines in the list.
|
||||
function deselectEngines() {
|
||||
for (checkbox of document.getElementsByTagName("INPUT"))
|
||||
checkbox.checked = false;
|
||||
}
|
||||
|
||||
const engineselection = () => {
|
||||
|
||||
// Return a list of all selected search-engines, in a comma-delimited list.
|
||||
function engineSelection() {
|
||||
let selection = [];
|
||||
for (engine of document.getElementsByTagName('INPUT')) {
|
||||
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
|
||||
|
@ -117,64 +137,80 @@ const engineselection = () => {
|
|||
return selection;
|
||||
}
|
||||
|
||||
const saveselection = () => {
|
||||
|
||||
// Save all selected engines into a present; whether new or old.
|
||||
function saveSelection() {
|
||||
let textbox = document.getElementById("searchbox");
|
||||
let selection = document.getElementById("presets").value;
|
||||
let name = selection;
|
||||
|
||||
if (selection == "newPreset")
|
||||
name = textbox.value;
|
||||
savepreset(engineselection(), name);
|
||||
savePreset(engineSelection(), name);
|
||||
// Update UI & listed presets
|
||||
resetsearchbox(true);
|
||||
populatepresets();
|
||||
enablepresetbyname(name);
|
||||
resetSearchBox(true);
|
||||
populatePresets();
|
||||
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;
|
||||
if (value != "newPreset")
|
||||
localStorage.setItem("defaultPreset", value);
|
||||
resetsearchbox();
|
||||
resetSearchBox();
|
||||
|
||||
if (value == "noPreset")
|
||||
deselectengines();
|
||||
deselectEngines();
|
||||
else if (value == "newPreset")
|
||||
searchboxaspresetname();
|
||||
searchBoxAsPresetName();
|
||||
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 testItem != item;
|
||||
});
|
||||
}
|
||||
|
||||
const removeredundantitems = (array) => {
|
||||
|
||||
// Remove all repeated items from an array.
|
||||
function removeRedundantItems(array) {
|
||||
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 => {
|
||||
if (event.key == "Enter") search();
|
||||
});
|
||||
|
||||
|
||||
// Wait for a preset to be selected, so that we can change search-engine selections.
|
||||
document.addEventListener("change", e => {
|
||||
if (e.target.id == "presets")
|
||||
onpresetselected();
|
||||
onPresetSelected();
|
||||
})
|
||||
|
||||
|
||||
// Handle searches, as well and save/removal of search presets.
|
||||
document.addEventListener("click", e => {
|
||||
if (e.target.classList.contains("search"))
|
||||
search();
|
||||
else if (e.target.classList.contains("save"))
|
||||
saveselection();
|
||||
saveSelection();
|
||||
else if (e.target.classList.contains("remove"))
|
||||
removecurrentpreset();
|
||||
removeCurrentPreset();
|
||||
});
|
||||
|
|
Ŝarĝante…
Reference in New Issue