- Regular hooks are functions that don’t return anything. They are run for their side effects, and may sometimes alter the objects they have been passed, such as inserting an extra item in a list.
- “Filters” are functions that return their first argument, after maybe changing it. An example filter is one that takes the text of a field during card display, and returns an altered version.
New Style Hooks
A new style of hook was added in Anki 2.1.20. Imagine you wish to show a message each time the front side of a card is shown in the review screen. You’ve looked at the source code in reviewer.py, and seen the following line in the showQuestion() function:Notable Hooks
For a full list of hooks and their documentation, see the Hooks Reference page, or browse the source directly:Webview
Many of Anki’s screens are built with one or more webviews, and there are some hooks you can use to intercept their use. From Anki 2.1.22:gui_hooks.webview_will_set_content()allows you to modify the HTML that various screens send to the webview. You can use this for adding your own HTML/CSS/Javascript to particular screens. This will not work for external pages - see the Anki 2.1.36 section below.gui_hooks.webview_did_receive_js_message()allows you to intercept messages sent from Javascript. Anki provides apycmd(string)function in Javascript which sends a message back to Python, and various screens such as reviewer.py respond to the messages. By using this hook, you can respond to your own messages as well.
webview_did_inject_style_into_page()gives you an opportunity to inject styling or content into external pages like the graphs screen and congratulations page that are loaded with load_ts_page().
Managing External Resources in Webviews
Add-ons may expose their own web assets by utilizingaqt.addons.AddonManager.setWebExports(). Web exports registered in this manner may then be accessed under the /_addons subpath.
For example, to allow access to a my-addon.js and my-addon.css residing
in a “web” subfolder in your add-on package, first register the corresponding web export:
gui_hooks.webview_will_set_content: