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

View File

@ -19,8 +19,7 @@ const populateengines = (engines) => {
const populatepresets = () => { const populatepresets = () => {
let presetDropdown = document.getElementById("presets"); let presetDropdown = document.getElementById("presets");
let presets = Array(); let presets = loadpresets()
try { presets = localStorage.getItem('presets').split(','); } catch {}
while (presetDropdown.firstChild) { while (presetDropdown.firstChild) {
presetDropdown.removeChild(presetDropdown.lastChild); 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 = () => { const removecurrentpreset = () => {
let presetName = document.getElementById("presets").value; let presetName = document.getElementById("presets").value;
if (presetName == "noPreset" || presetName == "newPreset") if (presetName == "noPreset" || presetName == "newPreset")
@ -138,13 +118,17 @@ const engineselection = () => {
} }
const saveselection = () => { const 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
resetsearchbox(true);
populatepresets();
enablepresetbyname(name);
} }
const onpresetselected = (event) => { const onpresetselected = (event) => {