|
91 | 91 | }; |
92 | 92 |
|
93 | 93 | const detectLanguage = (query, params) => { |
94 | | - const en = 'en'; |
95 | 94 | const allowedLanguages = params.get('languages'); |
96 | | - const only = allowedLanguages ? allowedLanguages.split(',') : LANGUAGES; |
97 | | - console.log('allowed languages', only); |
98 | | - if (only.length <= 1) return en; |
| 95 | + const languagesRaw = allowedLanguages ? allowedLanguages.split(',') : LANGUAGES; |
99 | 96 |
|
100 | | - const all = detectAll(query, { only }); |
101 | | - console.log('detected languages', all); |
| 97 | + const all = 'all'; |
| 98 | + const only = languagesRaw.filter(i => i !== all); |
| 99 | + console.log('allowed languages', languagesRaw); |
| 100 | + if (only.length === 0) return all; |
| 101 | + if (languagesRaw.length === 1) return only[0]; |
102 | 102 |
|
103 | | - const detected = all |
| 103 | + const detected = detectAll(query, { only }); |
| 104 | + console.log('detected languages', detected); |
| 105 | + |
| 106 | + const ranked = detected |
104 | 107 | .filter(i => i.accuracy > 0.01) |
105 | 108 | .sort((a, b) => b.accuracy - a.accuracy); |
106 | | - if (detected.length === 0) return en; |
107 | 109 |
|
108 | | - return detected[0].lang; |
| 110 | + return ranked[0]?.lang || (languagesRaw.length > only.length ? all : only[0]); |
109 | 111 | }; |
110 | 112 |
|
111 | 113 | const shuffle = xs => { |
|
133 | 135 | .forEach(([k, v]) => newParams.set(k, v)); |
134 | 136 |
|
135 | 137 | const query = queryFromForm || params.get('q') || ''; |
| 138 | + console.log(`query "${query}"`); |
136 | 139 | newParams.set('language', detectLanguage(query, params)); |
137 | 140 | newParams.set('q', query); |
138 | 141 | newParams.set('safesearch', params.get('safesearch') || '0'); |
|
218 | 221 | <hr> |
219 | 222 | <p>Examples for <strong>browser search engine settings</strong> (<code>about:preferences#search</code> or <code>chrome://settings/search</code>):</p> |
220 | 223 | <ul> |
221 | | - <li><code>${pageUrl}#q=%s&languages=en,fr,ru</code></li> |
222 | | - <li><code>${pageUrl}#q=%s&fast=1&image_proxy=False&categories=images&languages=en,fr,ru&get=1</code></li> |
| 224 | + <li><code>${pageUrl}#q=%s&languages=all,en,fr,ru</code></li> |
| 225 | + <li><code>${pageUrl}#q=%s&fast=1&image_proxy=False&categories=images,videos&languages=all,en,fr,ru&get=1</code></li> |
223 | 226 | </ul> |
224 | 227 | <p>Supports the following parameters (provided as an anchor for privacy):</p> |
225 | 228 | <ul> |
226 | 229 | <li>normal SearXNG GET/POST <a rel="noopener" target="_blank" href="https://docs.searxng.org/dev/search_api.html">parameters</a> (<code>safesearch=0</code> and <code>image_proxy=True</code> are set by default)</li> |
227 | | - <li><code>languages</code> limit query-based language detection; comma-separated, default is <code>${LANGUAGES.join(',')}</code></li> |
| 230 | + <li><code>languages</code> limit query-based language detection, fallbacks to the first language in the list when not detected (use <code>all</code> to fallback to SearXNG-based language detection instead); comma-separated, default is <code>${LANGUAGES.join(',')}</code></li> |
228 | 231 | <li><code>get</code> pass parameters to the instance using GET method; default is <code>0</code></li> |
229 | 232 | <li><code>timeout</code> in milliseconds until attempting the next instance; default is <code>${TIMEOUT}</code></li> |
230 | 233 | <li><code>attempts</code> how many instances to try; default is <code>${ATTEMPTS}</code></li> |
|
234 | 237 | <p>For local use (doesn't require any server): <code>wget ${pageUrl} -O searxng.html</code></p> |
235 | 238 | <p>Additionally can be combined with the Violentmonkey userscripts:</p> |
236 | 239 | <ul> |
237 | | - <li><a rel="noopener" target="_blank" href="https://userscripts.codonaft.com/redirect-searxng-on-failure.js">redirect-searxng-on-failure.js</a> — turn search failures into redirects</li> |
| 240 | + <li><a rel="noopener" target="_blank" href="https://userscripts.codonaft.com/redirect-searxng-on-failure.js">redirect-searxng-on-failure.js</a> — turn search failures into the redirects</li> |
238 | 241 | <li><a rel="noopener" target="_blank" href="https://userscripts.codonaft.com/force-searxng-parameters.js">force-searxng-parameters.js</a> — always enable image proxy and disable SafeSearch</li> |
239 | 242 | </ul> |
240 | 243 | <hr> |
|
0 commit comments