From 1ea35605d186272dd69537065acc3161ddb4c7a9 Mon Sep 17 00:00:00 2001 From: Mohamad Safadieh Date: Sun, 9 Aug 2020 07:59:49 -0400 Subject: [PATCH] Use query params for browser autocomplete Sending query params over GET seems to be the only way to be able to enable autocomplete in the browser. This commit adds the necessary URL formatting to opensearch.xml. In order to identify queries coming from the URL bar (rather than an AJAX request), which requires a different JSON format and MIME type, the request headers are checked for "X-Requested-With: XMLHttpRequest" which is added by jQuery request. --- searx/templates/__common__/opensearch.xml | 2 +- searx/webapp.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/searx/templates/__common__/opensearch.xml b/searx/templates/__common__/opensearch.xml index 27634245..7fdc1f7d 100644 --- a/searx/templates/__common__/opensearch.xml +++ b/searx/templates/__common__/opensearch.xml @@ -13,6 +13,6 @@ {% endif %} {% if autocomplete %} - + {% endif %} diff --git a/searx/webapp.py b/searx/webapp.py index 2df96e19..f79525d3 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -790,12 +790,12 @@ def autocompleter(): results.append(raw_text_query.getFullQuery()) # return autocompleter results - if request.form.get('format') == 'x-suggestions': - return Response(json.dumps([raw_text_query.query, results]), + if request.headers.get('X-Requested-With') == 'XMLHttpRequest': + return Response(json.dumps(results), mimetype='application/json') - return Response(json.dumps(results), - mimetype='application/json') + return Response(json.dumps([raw_text_query.query, results]), + mimetype='application/x-suggestions+json') @app.route('/preferences', methods=['GET', 'POST'])