searxng/docs/dev/plugins.rst
Martin Fischer 3f77ff29a3 [fix] plugin JS/CSS dependencies depend on themes
SearXNG supports plugins. Plugins can declare JavaScript and CSS
dependencies. Currently three plugins declare such dependencies:

* search_on_category_select (on by default)
* vim_hotkeys
* infinite_scroll

If a user enables a plugin its JavaScript and CSS dependencies are
embedded into every page. Sounds simple right?

Ironically in the Simple theme things start to get complicated:
The scripts were originally written for the Oscar theme and thus depend
on jQuery (which isn't loaded in the Simple theme) and look for certain
element identifiers (which aren't present in the Simple theme).  So how
did the plugins actually work with the simple theme?

The simple theme just didn't embed the plugin dependencies (which wasn't
even documented anywhere). Instead the simple theme checked if the paths
contained a certain script path, then set an attribute, which 00_init.js
detected and turned into a boolean, which was then used to enable
vanilla-JS reimplementations of the plugins.

This commit ends this horrible hack (and fixes #769).  The necessary
changes to the Simple theme are introduced in the next commit.
2022-01-18 19:03:15 +01:00

2.7 KiB

Plugins

Further reading ..

  • plugins generic

Plugins can extend or replace functionality of various components of searx.

Example plugin

from . import Resource

name = 'Example plugin'
description = 'This plugin extends the suggestions with the word "example"'
default_on = False  # disabled by default

js_dependencies = tuple()  # optional, list of static js resources
css_dependencies = tuple()  # optional, list of static css resources


# attach callback to the post search hook
#  request: flask request object
#  ctx: the whole local context of the post search hook
def post_search(request, search):
    search.result_container.suggestions.add('example')
    return True

External plugins

External plugins are standard python modules implementing all the requirements of the standard plugins. Plugins can be enabled by adding them to settings.yml's plugins section. Example external plugin can be found here.

Register your plugin

To enable your plugin register your plugin in searx > plugin > __init__.py. And at the bottom of the file add your plugin like. plugins.register(name_of_python_file)

Plugin entry points

Entry points (hooks) define when a plugin runs. Right now only three hooks are implemented. So feel free to implement a hook if it fits the behaviour of your plugin. A plugin doesn't need to implement all the hooks.