on master help sign in
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.

commit1ea35605d186272dd69537065acc3161ddb4c7a9 authorMohamad Safadieh <me@msafadieh.com> dateSun 9 Aug 2020, 07:59 (6 years ago) parent3c45fb7
2 files changed+5−5
searx/templates/__common__/opensearch.xml+1−1 searx/webapp.py+4−4
searx/templates/__common__/opensearch.xml+1−1
@@ -13,6 +13,6 @@
1313 </Url>
1414 {% endif %}
1515 {% if autocomplete %}
16− <Url rel="suggestions" type="application/json" template="{{ host }}autocompleter"/>
16+ <Url rel="suggestions" type="application/x-suggestions+json" template="{{ host }}autocompleter?q={searchTerms}"/>
1717 {% endif %}
1818</OpenSearchDescription>
searx/webapp.py+4−4
@@ -790,12 +790,12 @@ def autocompleter():
790790 results.append(raw_text_query.getFullQuery())
791791
792792 # return autocompleter results
793− if request.form.get('format') == 'x-suggestions':
794− return Response(json.dumps([raw_text_query.query, results]),
793+ if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
794+ return Response(json.dumps(results),
795795 mimetype='application/json')
796796
797− return Response(json.dumps(results),
798− mimetype='application/json')
797+ return Response(json.dumps([raw_text_query.query, results]),
798+ mimetype='application/x-suggestions+json')
799799
800800
801801@app.route('/preferences', methods=['GET', 'POST'])