plugins.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. .. _dev plugin:
  2. =======
  3. Plugins
  4. =======
  5. .. sidebar:: Further reading ..
  6. - :ref:`plugins generic`
  7. Plugins can extend or replace functionality of various components of searx.
  8. Example plugin
  9. ==============
  10. .. code:: python
  11. name = 'Example plugin'
  12. description = 'This plugin extends the suggestions with the word "example"'
  13. default_on = False # disabled by default
  14. js_dependencies = tuple() # optional, list of static js files
  15. css_dependencies = tuple() # optional, list of static css files
  16. # attach callback to the post search hook
  17. # request: flask request object
  18. # ctx: the whole local context of the post search hook
  19. def post_search(request, search):
  20. search.result_container.suggestions.add('example')
  21. return True
  22. External plugins
  23. ================
  24. SearXNG supports *external plugins* / there is no need to install one, SearXNG
  25. runs out of the box. But to demonstrate; in the example below we install the
  26. SearXNG plugins from *The Green Web Foundation* `[ref]
  27. <https://www.thegreenwebfoundation.org/news/searching-the-green-web-with-searx/>`__:
  28. .. code:: bash
  29. $ sudo utils/searxng.sh instance cmd bash
  30. (searxng-pyenv)$ pip install git+https://github.com/return42/tgwf-searx-plugins
  31. In the :ref:`settings.yml` activate the ``plugins:`` section and add module
  32. ``only_show_green_results`` from ``tgwf-searx-plugins``.
  33. .. code:: yaml
  34. plugins:
  35. ...
  36. - only_show_green_results
  37. ...
  38. Plugin entry points
  39. ===================
  40. Entry points (hooks) define when a plugin runs. Right now only three hooks are
  41. implemented. So feel free to implement a hook if it fits the behaviour of your
  42. plugin. A plugin doesn't need to implement all the hooks.
  43. .. py:function:: pre_search(request, search) -> bool
  44. Runs BEFORE the search request.
  45. `search.result_container` can be changed.
  46. Return a boolean:
  47. * True to continue the search
  48. * False to stop the search
  49. :param flask.request request:
  50. :param searx.search.SearchWithPlugins search:
  51. :return: False to stop the search
  52. :rtype: bool
  53. .. py:function:: post_search(request, search) -> None
  54. Runs AFTER the search request.
  55. :param flask.request request: Flask request.
  56. :param searx.search.SearchWithPlugins search: Context.
  57. .. py:function:: on_result(request, search, result) -> bool
  58. Runs for each result of each engine.
  59. `result` can be changed.
  60. If `result["url"]` is defined, then `result["parsed_url"] = urlparse(result['url'])`
  61. .. warning::
  62. `result["url"]` can be changed, but `result["parsed_url"]` must be updated too.
  63. Return a boolean:
  64. * True to keep the result
  65. * False to remove the result
  66. :param flask.request request:
  67. :param searx.search.SearchWithPlugins search:
  68. :param typing.Dict result: Result, see - :ref:`engine results`
  69. :return: True to keep the result
  70. :rtype: bool