forked from Ponysearch/Ponysearch
[mod] client_settings: pass settings from server to JS client
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
parent
9177172ea2
commit
ed2a4c8087
8 changed files with 45 additions and 41 deletions
|
@ -1,9 +1,4 @@
|
||||||
/**
|
/* SPDX-License-Identifier: AGPL-3.0-or-later */
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
(function (w, d) {
|
(function (w, d) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
@ -13,23 +8,13 @@
|
||||||
return scripts[scripts.length - 1];
|
return scripts[scripts.length - 1];
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// try to detect touch screen
|
|
||||||
w.searxng = {
|
w.searxng = {
|
||||||
method: script.getAttribute('data-method'),
|
settings: JSON.parse(atob(script.getAttribute('client_settings')))
|
||||||
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'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// update the css
|
// update the css
|
||||||
var hmtlElement = d.getElementsByTagName("html")[0];
|
var hmtlElement = d.getElementsByTagName("html")[0];
|
||||||
hmtlElement.classList.remove('no-js');
|
hmtlElement.classList.remove('no-js');
|
||||||
hmtlElement.classList.add('js');
|
hmtlElement.classList.add('js');
|
||||||
|
|
||||||
})(window, document);
|
})(window, document);
|
|
@ -101,7 +101,7 @@ window.searxng = (function (w, d) {
|
||||||
};
|
};
|
||||||
|
|
||||||
searxng.loadStyle = function (src) {
|
searxng.loadStyle = function (src) {
|
||||||
var path = searxng.static_path + src,
|
var path = searxng.settings.theme_static_path + src,
|
||||||
id = "style_" + src.replace('.', '_'),
|
id = "style_" + src.replace('.', '_'),
|
||||||
s = d.getElementById(id);
|
s = d.getElementById(id);
|
||||||
if (s === null) {
|
if (s === null) {
|
||||||
|
@ -115,7 +115,7 @@ window.searxng = (function (w, d) {
|
||||||
};
|
};
|
||||||
|
|
||||||
searxng.loadScript = function (src, callback) {
|
searxng.loadScript = function (src, callback) {
|
||||||
var path = searxng.static_path + src,
|
var path = searxng.settings.theme_static_path + src,
|
||||||
id = "script_" + src.replace('.', '_'),
|
id = "script_" + src.replace('.', '_'),
|
||||||
s = d.getElementById(id);
|
s = d.getElementById(id);
|
||||||
if (s === null) {
|
if (s === null) {
|
||||||
|
|
|
@ -62,7 +62,7 @@ searxng.ready(function () {
|
||||||
function (err) {
|
function (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
var e = d.createElement('div');
|
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.classList.add('dialog-error');
|
||||||
e.setAttribute('role', 'alert');
|
e.setAttribute('role', 'alert');
|
||||||
replaceChildrenWith(d.querySelector('#pagination'), [ e ]);
|
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 = {
|
const intersectionObserveOptions = {
|
||||||
rootMargin: "20rem",
|
rootMargin: "20rem",
|
||||||
};
|
};
|
||||||
|
|
|
@ -154,7 +154,7 @@ searxng.ready(function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (searxng.hotkeys) {
|
if (searxng.settings.hotkeys) {
|
||||||
searxng.on(document, "keydown", function (e) {
|
searxng.on(document, "keydown", function (e) {
|
||||||
// check for modifiers so we don't break browser's hotkeys
|
// 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) {
|
if (Object.prototype.hasOwnProperty.call(vimKeys, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
for (const [engine_name, description] of Object.entries(engine_descriptions)) {
|
for (const [engine_name, description] of Object.entries(engine_descriptions)) {
|
||||||
let elements = d.querySelectorAll('[data-engine-name="' + engine_name + '"] .engine-description');
|
let elements = d.querySelectorAll('[data-engine-name="' + engine_name + '"] .engine-description');
|
||||||
for (const element of elements) {
|
for (const element of elements) {
|
||||||
let source = ' (<i>' + searxng.translations['Source'] + ': ' + description[1] + '</i>)';
|
let source = ' (<i>' + searxng.settings.translations.Source + ': ' + description[1] + '</i>)';
|
||||||
element.innerHTML = description[0] + source;
|
element.innerHTML = description[0] + source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,11 +59,11 @@
|
||||||
createClearButton(qinput);
|
createClearButton(qinput);
|
||||||
|
|
||||||
// autocompleter
|
// autocompleter
|
||||||
if (searxng.autocompleter) {
|
if (searxng.settings.autocomplete_provider) {
|
||||||
searxng.autocomplete = AutoComplete.call(w, {
|
searxng.autocomplete = AutoComplete.call(w, {
|
||||||
Url: "./autocompleter",
|
Url: "./autocompleter",
|
||||||
EmptyMessage: searxng.translations.no_item_found,
|
EmptyMessage: searxng.settings.translations.no_item_found,
|
||||||
HttpMethod: searxng.method,
|
HttpMethod: searxng.settings.http_method,
|
||||||
HttpHeaders: {
|
HttpHeaders: {
|
||||||
"Content-type": "application/x-www-form-urlencoded",
|
"Content-type": "application/x-www-form-urlencoded",
|
||||||
"X-Requested-With": "XMLHttpRequest"
|
"X-Requested-With": "XMLHttpRequest"
|
||||||
|
@ -92,7 +92,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// vanilla js version of search_on_category_select.js
|
// 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';
|
d.querySelector('.help').className = 'invisible';
|
||||||
|
|
||||||
searxng.on('#categories input', 'change', function () {
|
searxng.on('#categories input', 'change', function () {
|
||||||
|
|
|
@ -19,14 +19,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% block styles %}{% endblock %}
|
{% block styles %}{% endblock %}
|
||||||
<!--[if gte IE 9]>-->
|
<!--[if gte IE 9]>-->
|
||||||
<script src="{{ url_for('static', filename='js/searxng.head.min.js') }}"
|
<script src="{{ url_for('static', filename='js/searxng.head.min.js') }}" client_settings="{{ client_settings }}"></script>
|
||||||
data-method="{{ method or 'POST' }}"
|
|
||||||
data-autocompleter="{% if autocomplete %}true{% else %}false{% endif %}"
|
|
||||||
data-search-on-category-select="{{ 'true' if 'plugins/js/search_on_category_select.js' in scripts else 'false'}}"
|
|
||||||
data-infinite-scroll="{% if infinite_scroll %}true{% else %}false{% endif %}"
|
|
||||||
data-hotkeys="{{ 'true' if 'plugins/js/vim_hotkeys.js' in scripts else 'false' }}"
|
|
||||||
data-static-path="{{ url_for('static', filename='themes/simple') }}/"
|
|
||||||
data-translations="{{ translations }}"></script>
|
|
||||||
<!--<![endif]-->
|
<!--<![endif]-->
|
||||||
{% block head %}
|
{% block head %}
|
||||||
<link title="{{ instance_name }}" type="application/opensearchdescription+xml" rel="search" href="{{ opensearch_url }}"/>
|
<link title="{{ instance_name }}" type="application/opensearchdescription+xml" rel="search" href="{{ opensearch_url }}"/>
|
||||||
|
|
|
@ -10,6 +10,7 @@ import hmac
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import base64
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
|
@ -444,7 +445,36 @@ def get_pretty_url(parsed_url: urllib.parse.ParseResult):
|
||||||
return [parsed_url.scheme + "://" + parsed_url.netloc, path]
|
return [parsed_url.scheme + "://" + parsed_url.netloc, path]
|
||||||
|
|
||||||
|
|
||||||
|
def get_client_settings():
|
||||||
|
req_pref = request.preferences
|
||||||
|
return {
|
||||||
|
'autocomplete_provider': req_pref.get_value('autocomplete'),
|
||||||
|
'http_method': req_pref.get_value('method'),
|
||||||
|
'infinite_scroll': req_pref.get_value('infinite_scroll'),
|
||||||
|
'translations': get_translations(),
|
||||||
|
'search_on_category_select': req_pref.plugins.choices['searx.plugins.search_on_category_select'],
|
||||||
|
'hotkeys': req_pref.plugins.choices['searx.plugins.vim_hotkeys'],
|
||||||
|
'theme_static_path': custom_url_for('static', filename='themes/simple'),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def render(template_name: str, override_theme: str = None, **kwargs):
|
def render(template_name: str, override_theme: str = None, **kwargs):
|
||||||
|
|
||||||
|
kwargs['client_settings'] = str(
|
||||||
|
base64.b64encode(
|
||||||
|
bytes(
|
||||||
|
json.dumps(get_client_settings()),
|
||||||
|
encoding='utf-8',
|
||||||
|
)
|
||||||
|
), encoding='utf-8',
|
||||||
|
)
|
||||||
|
|
||||||
|
# obsolete, only needed by oscar
|
||||||
|
kwargs['autocomplete'] = request.preferences.get_value('autocomplete')
|
||||||
|
kwargs['method'] = request.preferences.get_value('method')
|
||||||
|
kwargs['infinite_scroll'] = request.preferences.get_value('infinite_scroll')
|
||||||
|
kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':'))
|
||||||
|
|
||||||
# values from the HTTP requests
|
# values from the HTTP requests
|
||||||
kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
|
kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
|
||||||
kwargs['cookies'] = request.cookies
|
kwargs['cookies'] = request.cookies
|
||||||
|
@ -452,9 +482,6 @@ def render(template_name: str, override_theme: str = None, **kwargs):
|
||||||
|
|
||||||
# values from the preferences
|
# values from the preferences
|
||||||
kwargs['preferences'] = request.preferences
|
kwargs['preferences'] = request.preferences
|
||||||
kwargs['method'] = request.preferences.get_value('method')
|
|
||||||
kwargs['autocomplete'] = request.preferences.get_value('autocomplete')
|
|
||||||
kwargs['infinite_scroll'] = request.preferences.get_value('infinite_scroll')
|
|
||||||
kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')
|
kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')
|
||||||
kwargs['advanced_search'] = request.preferences.get_value('advanced_search')
|
kwargs['advanced_search'] = request.preferences.get_value('advanced_search')
|
||||||
kwargs['query_in_title'] = request.preferences.get_value('query_in_title')
|
kwargs['query_in_title'] = request.preferences.get_value('query_in_title')
|
||||||
|
@ -466,7 +493,6 @@ def render(template_name: str, override_theme: str = None, **kwargs):
|
||||||
|
|
||||||
# i18n
|
# i18n
|
||||||
kwargs['language_codes'] = [l for l in languages if l[0] in settings['search']['languages']]
|
kwargs['language_codes'] = [l for l in languages if l[0] in settings['search']['languages']]
|
||||||
kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':'))
|
|
||||||
|
|
||||||
locale = request.preferences.get_value('locale')
|
locale = request.preferences.get_value('locale')
|
||||||
kwargs['locale_rfc5646'] = _get_locale_rfc5646(locale)
|
kwargs['locale_rfc5646'] = _get_locale_rfc5646(locale)
|
||||||
|
|
Loading…
Reference in a new issue