Merge remote-tracking branch 'return42/fix-url-bar-suggestion'

This commit is contained in:
Alexandre Flament 2021-04-21 16:23:52 +02:00
commit 624d3ec6db

View file

@ -788,20 +788,26 @@ def autocompleter():
# parse query # parse query
raw_text_query = RawTextQuery(request.form.get('q', ''), disabled_engines) raw_text_query = RawTextQuery(request.form.get('q', ''), disabled_engines)
sug_prefix = raw_text_query.getQuery()
# normal autocompletion results only appear if no inner results returned # normal autocompletion results only appear if no inner results returned
# and there is a query part # and there is a query part
if len(raw_text_query.autocomplete_list) == 0 and len(raw_text_query.getQuery()) > 0: if len(raw_text_query.autocomplete_list) == 0 and len(sug_prefix) > 0:
# get language from cookie # get language from cookie
language = request.preferences.get_value('language') language = request.preferences.get_value('language')
if not language or language == 'all': if not language or language == 'all':
language = 'en' language = 'en'
else: else:
language = language.split('-')[0] language = language.split('-')[0]
# run autocompletion # run autocompletion
raw_results = search_autocomplete(request.preferences.get_value('autocomplete'), raw_results = search_autocomplete(
raw_text_query.getQuery(), language) request.preferences.get_value('autocomplete'), sug_prefix, language
)
for result in raw_results: for result in raw_results:
# attention: this loop will change raw_text_query object and this is
# the reason why the sug_prefix was stored before (see above)
results.append(raw_text_query.changeQuery(result).getFullQuery()) results.append(raw_text_query.changeQuery(result).getFullQuery())
if len(raw_text_query.autocomplete_list) > 0: if len(raw_text_query.autocomplete_list) > 0:
@ -812,13 +818,16 @@ def autocompleter():
for answer in answers: for answer in answers:
results.append(str(answer['answer'])) results.append(str(answer['answer']))
# return autocompleter results
if request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return Response(json.dumps(results), # the suggestion request comes from the searx search form
mimetype='application/json') suggestions = json.dumps(results)
mimetype = 'application/json'
else:
# the suggestion request comes from browser's URL bar
suggestions = json.dumps([sug_prefix, results])
mimetype = 'application/x-suggestions+json'
return Response(json.dumps([raw_text_query.query, results]), return Response(suggestions, mimetype=mimetype)
mimetype='application/x-suggestions+json')
@app.route('/preferences', methods=['GET', 'POST']) @app.route('/preferences', methods=['GET', 'POST'])