[mod] client_settings: pass settings from server to JS client

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2022-04-13 17:08:19 +02:00
parent 9177172ea2
commit ed2a4c8087
8 changed files with 45 additions and 41 deletions

View file

@ -1,9 +1,4 @@
/**
* @license
* (C) Copyright Contributors to the SearXNG project.
* (C) Copyright Contributors to the searx project (2014 - 2021).
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/* SPDX-License-Identifier: AGPL-3.0-or-later */
(function (w, d) {
'use strict';
@ -13,23 +8,13 @@
return scripts[scripts.length - 1];
})();
// try to detect touch screen
w.searxng = {
method: script.getAttribute('data-method'),
autocompleter: script.getAttribute('data-autocompleter') === 'true',
search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true',
infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true',
hotkeys: script.getAttribute('data-hotkeys') === 'true',
static_path: script.getAttribute('data-static-path'),
translations: JSON.parse(script.getAttribute('data-translations')),
theme: {
// image that is displayed if load of <img src='...'> failed
img_load_error: 'img/img_load_error.svg'
}
settings: JSON.parse(atob(script.getAttribute('client_settings')))
};
// update the css
var hmtlElement = d.getElementsByTagName("html")[0];
hmtlElement.classList.remove('no-js');
hmtlElement.classList.add('js');
})(window, document);
})(window, document);

View file

@ -101,7 +101,7 @@ window.searxng = (function (w, d) {
};
searxng.loadStyle = function (src) {
var path = searxng.static_path + src,
var path = searxng.settings.theme_static_path + src,
id = "style_" + src.replace('.', '_'),
s = d.getElementById(id);
if (s === null) {
@ -115,7 +115,7 @@ window.searxng = (function (w, d) {
};
searxng.loadScript = function (src, callback) {
var path = searxng.static_path + src,
var path = searxng.settings.theme_static_path + src,
id = "script_" + src.replace('.', '_'),
s = d.getElementById(id);
if (s === null) {

View file

@ -62,7 +62,7 @@ searxng.ready(function () {
function (err) {
console.log(err);
var e = d.createElement('div');
e.textContent = searxng.translations.error_loading_next_page;
e.textContent = searxng.settings.translations.error_loading_next_page;
e.classList.add('dialog-error');
e.setAttribute('role', 'alert');
replaceChildrenWith(d.querySelector('#pagination'), [ e ]);
@ -70,7 +70,7 @@ searxng.ready(function () {
)
}
if (searxng.infinite_scroll && searxng.infinite_scroll_supported) {
if (searxng.settings.infinite_scroll && searxng.infinite_scroll_supported) {
const intersectionObserveOptions = {
rootMargin: "20rem",
};

View file

@ -154,7 +154,7 @@ searxng.ready(function () {
}
};
if (searxng.hotkeys) {
if (searxng.settings.hotkeys) {
searxng.on(document, "keydown", function (e) {
// check for modifiers so we don't break browser's hotkeys
if (Object.prototype.hasOwnProperty.call(vimKeys, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {

View file

@ -15,7 +15,7 @@
for (const [engine_name, description] of Object.entries(engine_descriptions)) {
let elements = d.querySelectorAll('[data-engine-name="' + engine_name + '"] .engine-description');
for (const element of elements) {
let source = ' (<i>' + searxng.translations['Source'] + ':&nbsp;' + description[1] + '</i>)';
let source = ' (<i>' + searxng.settings.translations.Source + ':&nbsp;' + description[1] + '</i>)';
element.innerHTML = description[0] + source;
}
}

View file

@ -59,11 +59,11 @@
createClearButton(qinput);
// autocompleter
if (searxng.autocompleter) {
if (searxng.settings.autocomplete_provider) {
searxng.autocomplete = AutoComplete.call(w, {
Url: "./autocompleter",
EmptyMessage: searxng.translations.no_item_found,
HttpMethod: searxng.method,
EmptyMessage: searxng.settings.translations.no_item_found,
HttpMethod: searxng.settings.http_method,
HttpHeaders: {
"Content-type": "application/x-www-form-urlencoded",
"X-Requested-With": "XMLHttpRequest"
@ -92,7 +92,7 @@
}
// vanilla js version of search_on_category_select.js
if (qinput !== null && d.querySelector('.help') != null && searxng.search_on_category_select) {
if (qinput !== null && d.querySelector('.help') != null && searxng.settings.search_on_category_select) {
d.querySelector('.help').className = 'invisible';
searxng.on('#categories input', 'change', function () {