OctoSearch/popup/search.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

document.getElementById("searchbox").focus();
2019-05-06 08:20:28 -05:00
browser.search.get().then(engines => {
2019-05-06 08:20:28 -05:00
for (engine of engines) {
let checkbox = document.createElement("INPUT");
checkbox.type = "checkbox";
checkbox.setAttribute('value', 'engine ' + engine.name);
try {
let defaultengines = localStorage.getItem('defaultengines').split(",");
for (eng of defaultengines) {
if (engine.name == eng) checkbox.checked = true;
}
} catch {}
2019-05-06 08:20:28 -05:00
let label = document.createElement('label');
label.appendChild(document.createTextNode(engine.name));
document.getElementById("boxes").appendChild(checkbox);
document.getElementById("boxes").appendChild(label);
document.getElementById("boxes").appendChild(document.createElement("BR"))
2019-05-06 08:20:28 -05:00
}
});
const search = tab => {
browser.tabs.query({active: true, currentWindow: true})
.then(() => {
for (selected of engineselection()) {
browser.search.search({
query: document.getElementById("searchbox").value,
engine: selected,
});
}
}
);
2019-05-06 08:20:28 -05:00
}
const engineselection = () => {
let selection = [];
2019-05-06 08:20:28 -05:00
for (engine of document.getElementsByTagName('INPUT')) {
if (engine.value.includes("engine") && engine.checked) selection.push(engine.value.slice(7));
2019-05-06 08:20:28 -05:00
}
return selection;
}
const saveselection = () => {
try {
localStorage.removeItem('defaultengines');
} catch {}
localStorage.setItem('defaultengines', engineselection());
}
document.getElementById("searchbox").addEventListener("keypress", key => {
if (event.key == "Enter") search();
});
document.addEventListener("click", e => {
if (e.target.classList.contains("search")) search();
else if (e.target.classList.contains("default")) saveselection();
2019-05-06 08:20:28 -05:00
});