Split communal/utility functions into `common.js`

`common.js` will be prepended to other JS files using the Makefile.
This commit is contained in:
Jaidyn Ann 2023-04-04 08:17:50 -05:00
parent add342f854
commit 404479be6c
4 changed files with 57 additions and 34 deletions

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
default: all
all: popup
clean:
rm pages/popup.js
popup:
cat common.js pages/popup.src.js > pages/popup.js

21
common.js Normal file
View File

@ -0,0 +1,21 @@
// 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) {
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.push(presetName);
localStorage.setItem('preset_' + presetName, preset);
localStorage.setItem('presets', presets);
}
// Load an array of all saved preset's names.
function loadpresets() {
try { return localStorage.getItem('presets').split(','); } catch { return []; }
}

View File

@ -2,17 +2,20 @@
"manifest_version": 2,
"name": "Multi engine search",
"version": "1.1",
"version": "1.2",
"description": "Search on Multiple search engines.",
"homepage_url": "https://github.com/Javojav/Multi-engine-search",
"description": "Search with multiple search engines simultaneously.",
"homepage_url": "https://notabug.org/jadedctrl/octosearch",
"icons": {
"48": "icons/icon.png"
},
"permissions": [
"search"
"search",
"webRequest",
"webRequestBlocking",
"*://octosearch.invalid/*"
],
"commands": {
@ -25,8 +28,14 @@
"browser_action": {
"default_icon": "icons/icon.png",
"default_title": "Multi engine search",
"default_title": "OctoSearch",
"default_popup": "pages/popup.html"
},
"background": {
"scripts": [
"background.js"
]
}
}

View File

@ -19,8 +19,7 @@ const populateengines = (engines) => {
const populatepresets = () => {
let presetDropdown = document.getElementById("presets");
let presets = Array();
try { presets = localStorage.getItem('presets').split(','); } catch {}
let presets = loadpresets()
while (presetDropdown.firstChild) {
presetDropdown.removeChild(presetDropdown.lastChild);
@ -71,25 +70,6 @@ const enablepreset = (preset) => {
}
}
const 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.push(presetName);
localStorage.setItem('preset_' + presetName, preset);
localStorage.setItem('presets', presets);
// Update UI & listed presets
resetsearchbox(true);
populatepresets();
enablepresetbyname(name);
}
const removecurrentpreset = () => {
let presetName = document.getElementById("presets").value;
if (presetName == "noPreset" || presetName == "newPreset")
@ -145,6 +125,10 @@ const saveselection = () => {
if (selection == "newPreset")
name = textbox.value;
savepreset(engineselection(), name);
// Update UI & listed presets
resetsearchbox(true);
populatepresets();
enablepresetbyname(name);
}
const onpresetselected = (event) => {