whoogle-search/app/static/js/controller.js
Ben Busby 0c0ebb8917 Added POST search, encrypted query strings, refactoring
The implementation of POST search support comes with a few benefits. The
most apparent is the avoidance of search queries appearing in web server
logs -- instead of the prior GET approach (i.e.
/search?q=my+search+query), using POST requests with the query stored in
the request body creates logs that simply appear as "/search".

Since a lot of relative links are generated in the results page, I came
up with a way to generate a unique key at run time that is used to
encrypt any query strings before sending to the user. This benefits both
regular text queries as well as fetching of image links and means that
web logs will only show an encrypted string where a link or query
string might slip through.

Unfortunately, GET search requests still need to be supported, as it
doesn't seem that Firefox (on iOS) supports loading search engines by
their opensearch.xml file, but instead relies on manual entry of a
search query string. Once this is updated, I'll probably remove GET
request search support.
2020-04-28 18:19:34 -06:00

87 lines
2.9 KiB
JavaScript

document.addEventListener("DOMContentLoaded", function() {
setTimeout(function() {
document.getElementById("main").style.display = "block";
}, 100);
// Setup search field
const searchBar = document.getElementById("search-bar");
const searchBtn = document.getElementById("search-submit");
// Automatically focus on search field
searchBar.focus();
searchBar.select();
searchBar.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
searchBtn.click();
}
});
// Setup shoogle config
const collapsible = document.getElementById("config-collapsible");
collapsible.addEventListener("click", function() {
this.classList.toggle("active");
let content = this.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
content.classList.toggle("open");
});
const saveConfig = document.getElementById("config-submit");
const nearConfig = document.getElementById("config-near");
const noJSConfig = document.getElementById("config-nojs");
const darkConfig = document.getElementById("config-dark");
// Request existing config info
let xhrGET = new XMLHttpRequest();
xhrGET.open("GET", "/config");
xhrGET.onload = function() {
if (xhrGET.readyState === 4 && xhrGET.status !== 200) {
alert("Error loading Shoogle config");
return;
}
// Allow for updating/saving config values
let configSettings = JSON.parse(xhrGET.responseText);
nearConfig.value = configSettings["near"] ? configSettings["near"] : "";
nearConfig.addEventListener("keyup", function() {
configSettings["near"] = nearConfig.value;
});
noJSConfig.checked = !!configSettings["nojs"];
noJSConfig.addEventListener("change", function() {
configSettings["nojs"] = noJSConfig.checked ? 1 : 0;
});
darkConfig.checked = !!configSettings["dark"];
darkConfig.addEventListener("change", function() {
configSettings["dark"] = darkConfig.checked ? 1 : 0;
});
saveConfig.onclick = function() {
let xhrPOST = new XMLHttpRequest();
xhrPOST.open("POST", "/config");
xhrPOST.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhrPOST.send(JSON.stringify(configSettings));
xhrPOST.onload = function() {
if (xhrGET.readyState === 4 && xhrPOST.status !== 200) {
alert("Failure to save config file");
return;
}
if (confirm("Configuration saved. Reload now?")) {
window.location.reload();
}
}
}
};
xhrGET.send();
});