Skip to content

Commit c0afe35

Browse files
committed
Update
1 parent e878f7b commit c0afe35

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

searxng.html

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,31 @@
5555
return best;
5656
};
5757

58-
const detectLang = query => {
59-
const all = detectAll(query, { only: ['en', 'ru'] });
60-
console.log('detected languages', all)
61-
const good = all.filter(i => i.accuracy > 0.01);
62-
let lang = 'en';
63-
if (good.length > 0) {
64-
good.sort((a, b) => b.accuracy - a.accuracy);
65-
lang = good[0].lang;
58+
const detectLanguage = query => {
59+
const en = 'en';
60+
const allowedLanguages = params.get('allowed_languages');
61+
const only = allowedLanguages ? allowedLanguages.split(',') : [en];
62+
console.log('allowed languages', only);
63+
if (only.length <= 1) {
64+
return en;
6665
}
67-
return lang;
66+
67+
const all = detectAll(query, { only });
68+
console.log('detected languages', all);
69+
70+
return all
71+
.filter(i => i.accuracy > 0.01)
72+
.sort((a, b) => b.accuracy - a.accuracy)[0]?
73+
.lang || en;
6874
};
6975

7076
const redirect = (url, query) => {
7177
params
7278
.entries()
73-
.filter(([k, _]) => k !== 'fast')
79+
.filter(([k, _]) => k !== 'fast' && k !== 'allowed_languages')
7480
.forEach(([k, v]) => url.searchParams.set(k, v));
7581
url.searchParams.set('q', query)
76-
url.searchParams.set('language', detectLang(query));
82+
url.searchParams.set('language', detectLanguage(query));
7783
console.log(url);
7884
window.location.replace(url.toString());
7985
};
@@ -87,7 +93,7 @@
8793
if (query) {
8894
redirect(targetUrl, query);
8995
} else {
90-
document.body.innerHTML = '<p>This page accepts SearXNG <a target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> + special optional parameter <code>fast=1</code> for hardcoded instances.</p><p>Example for browser settings (<code>chrome://settings/search</code> or <code>about:preferences#search</code>):</p><p><code>{{ site.url }}{{ page.url }}?q=%s&fast=1&image_proxy=True&categories=images</code></p>';
96+
document.body.innerHTML = '<p>This page accepts SearXNG <a target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> + special optional parameter <code>fast=1</code> for hardcoded instances and <code>allowed_languages=en,ru</code> for automatic language limit derived from the query.</p><p>Example for browser settings (<code>chrome://settings/search</code> or <code>about:preferences#search</code>):</p><p><code>{{ site.url }}{{ page.url }}?q=%s&fast=1&image_proxy=True&categories=images</code></p>';
9197

9298
const input = document.createElement('input');
9399
input.name = 'q';

0 commit comments

Comments
 (0)