Add-on update: API for card templates
Similarly to Card Info earlier, this update introduces additional backend features to the add-on, enabling more advanced customization for all card templates.
1. Reviewer Actions
Several actions normally available in the bottom right More menu during card reviews, can now be performed by template scripts through respective pycmd commands:
-
Suspend/bury actions
pycmd(`action::bury_card`); pycmd(`action::bury_note`); pycmd(`action::suspend_card`); pycmd(`action::suspend_note`); -
Flags
pycmd(`action::flag:1`); // flags the card red pycmd(`action::flag:2`); // flags the card orange ... pycmd(`action::flag:7`); // flags the card purple pycmd(`action::flag:0`); // clears the flag -
Voice actions
pycmd(`action::voice_record`); pycmd(`action::voice_replay`); -
Undo
pycmd(`action::undo`);(equivalent to
Ctrl+ZorEdit→Undo)
Any of the actions above can be easily turned into a button and added to a card template:
<button onclick=pycmd("action::suspend_card")>Suspend</button>

2. Browser Search
pycmd(`action::search:_your_search_query_`);
allows opening the Anki Browser and quering for cards with all the standard syntax (no extra escaping after action::search: is required). Example use:
search_query = '"English*:to * out" OR tag:phrasal_verb::_out';
pycmd(`action::search:${search_query}`);
This will search for cards with phrasal verbs ending with " out" by matching contents of any field against the pattern or by looking at cards explicitly marked with the respective hierarchical tag:
Similarly,
pycmd(`action::search:cid:1653480217073`);
can be used to open any specific card for editing.
3. Persistent localStorage
Desktop Anki does not keep the contents of localStorage after closing the application, practically making it function as another sessionStorage instead (unlike AnkiDroid, where saving localStorage data can be enabled by toggling on localStorage in Study Screen in Advanced settings, or AnkiWeb, where it works by default in any browser).
This limits card templates, as features, such as appearance settings, can’t be made work reliably across all the platforms, unless they are edited directly in the source code for every Card Type.
There already is an add-on for making localStorage function as intended, but this implementation works differently and has multiple advantages:
- It does not modify card templates themselves so it does not leave any garbage that has to be manually cleared after the add-on is disabled or uninstalled
- It does not require any setup, and makes all localStorage content automatically saved simply by enabling the add-on
- The localStorage is loaded on startup, making it available to card templates immediately, instead of only after a first card is flipped
- Works in all Anki windows, not just the Reviewer. This provides more opportunities for other add-ons and add-on/card-template interaction. For example, adjusting appearance of field contents in the Editor depending on options selected during reviews is possible
- All localStorage changes are saved immediately (instead of on finishing a review or similar events) which prevents data loss when the window is terminated with
[x]or in case the app crashes (the save is not triggered by assignments such aslocalStorage['key'] = ..., so explicitlocalStorage.setItem('key', "value")should be used instead at all times) - Stores data on the disk in a safe, strict data format (json) instead of a mutable javascript. It also does not force desktop storage to mobile, which would prevent changes made in AnkiDroid itself from having any consistent effect (the storage is still synced between all desktop profiles connected to the same cloud collection)
