Editor Scripts & Symbols

AnkiWeb addon 2065559429

Runs custom JavaScript/Python scripts and inserts symbols/strings in the Anki editor via keyboard shortcuts or popup menus, configured through JSON menus and CodeMirror.
AI-generated summary; may contain mistakes.

card-creationshortcutsdeveloper-toolsfork-or-reupload

Open on AnkiWeb GitHub Forum topic Ask about alternatives

AnkiWeb

Rating
5 (πŸ‘ 4 Β· πŸ‘Ž 1)
Updated
2022-07-25
Anki versions
2.1.66~
Description language
en

Maintenance

abandoned

  • Last update or commit was 1231 days before the snapshot (2023-05-19).

Will it work on my Anki?

Version branches
Min AnkiMax AnkiUpdated
2.1.502.1.66+2022-07-25
History across monthly snapshots

Loading…

Similar addons

Symbols As You Type

Inserts Unicode symbols into Anki cards via predefined key sequences, with customizable symbol lists synced per profile and optional raw HTML support.

card-creationformattingshortcuts

Rating 203 πŸ‘ 186 Β· πŸ‘Ž 17 ⭐ 30 Anki 25.09.2~ Updated 2026-01-25
Override shortcuts

Automatically detects Python-side Anki shortcuts and lets users override or remove them via configuration, including window-close bindings, though detection may be imprecise.

shortcuts

Rating 0 πŸ‘ 0 Β· πŸ‘Ž 0 ⭐ 0 Anki 2.1.66~ Updated 2023-06-05
Asset Manager

Simplifies managing card template scripts and HTML snippets, enabling reusable JavaScript insertion with conditions, template overwriting, developer hooks, and version updates.

card-templatesdeveloper-tools

Rating 11 πŸ‘ 9 Β· πŸ‘Ž 2 ⭐ 6 Anki 2.1.47~ Updated 2021-08-29

README

Editor Scripts & Symbols

Anki addon (https://ankiweb.net/shared/info/2065559429) to run custom scripts and insert symbols/strings in the Anki editor using keyboard shortcuts or popup menus (Anki forum https://forums.ankiweb.net/t/editor-js-snippets-support-thread/14958).

Configure symbols/strings, JavaScript or Python snippets to be inserted/executed in the editor from keyboard shortcuts (Qt format, see https://doc.qt.io/qt-5/qkeysequence.html).

<img src="https://github.com/TRIAEIOU/Editor-Scripts-Symbols/blob/main/Screenshots/overview.png" height="800">

General configuration

Addon configuration of scripts and symbols to run/insert is made in the addon configuration. To speed up and facilitate editing of configured scripts and symbols a CodeMirror 6 editor with JSON syntax highlighting is available from the context menu from the editor fields. Alternatively you can use the built in Anki addon configuration editor available from the Tools β†’ Addons β†’ Configure addon menu. The ESS editor has the following key board shortcuts

  • https://codemirror.net/6/docs/ref/#commands.defaultKeymap
  • https://codemirror.net/6/docs/ref/#search.searchKeymap
  • https://codemirror.net/6/docs/ref/#commands.historyKeymap
  • https://codemirror.net/6/docs/ref/#language.foldKeymap
  • https://codemirror.net/6/docs/ref/#lint.lintKeymap
  • Ctrl+B is set to "jump to matching bracket"
  • Ctrl+Enter is "save and close editor"
  • Shift+Escape is "discard and close editor"
  • <unconfigured> is "open configuration editor"

Addon general configuration is under Β΄SettingsΒ΄.

  • Editor shortcut: Keyboard shorcut to open the ESS editor from the Anki editor. (optional, default none)
  • Popup shortcut: Keyboard shortcut to open root menu (corresponding to the 'Scripts & Symbols' node) at caret position when in an editor field. (optional)
  • Size mode: Size mode for config editor, valid options are last (same size as last time), parent (same size as editor window) and XXXXxYYYY where XXXX is the width in pixels and YYYY is the height. (optional, default last)
  • CSS: Custom CSS to be applied to the config editor, for instance .cm-editor .cm-content {white-space: pre-wrap; word-break: break-word;} to word wrap (see https://codemirror.net/6/examples/styling/ for styling). (optional, default none)
  • Last geometry: Used by addon to store last geometry, do not edit.

Editing scripts and symbols

Add scripts or symbols by adding JSON objects and lists in hierarchal "node tree" under "Scripts & Symbols" in the configuration. The current version of the configuration is backed up in the addon directory before saving new configuration (overwritten on next save). Each node is one of the following:

  • Menu: Node is menu containing end nodes and/or submenus. Keys:
    • 1. Menu: Menu name.
    • 2. Shortcut: Keyboard shortcut to open menu at caret position. (optional)
    • 3. Items: JSON list of children.
  • Symbol: Node is symbol/string to insert. Keys:
    • 1. Symbol: Symbol/string to insert
    • 2. Shortcut: Keyboard shortcut to insert symbol/string. (optional)
    • 3. HTML: Set to string "true" to interpret string as HTML: "HTML": "true" (optional, defaults to false).
  • Script/JavaScript: Node is JavaScript to execute in editor. The script is executed through document.execCommand(), the note contents are in the shadow root (document.activeElement.shadowRoot). The clipboard contents are made available in the object "ess_clipboard": ess_clipboard = {html: [clipboard HTML content], text: [clipboard text content]}. Keys:
    • 1. Script: Title/name of script.
    • 2. Shortcut: Keyboard shortcut to execute script. (optional)
    • 3. Language: Script language, must be "JS".
    • 4. Pre: JavaScript command(s) to prepend to the file contents. (optional)
    • 5. File: File to load script commands from in [addon]/user_files. (optional)
    • 6. Post: JavaScript command(s) to append to the file contents. (optional)
  • Script/Python: Node is Python to execute in editor. The script is executed through compile/exec statements. The Anki Editor object instance is available through the variable "editor", the QApplication clipboard is available through the variable "clipboard" and the main window object through the variable "mw". Keys:
    • 1. Script: Title/name of script.
    • 2. Shortcut: Keyboard shortcut to execute script. (optional)
    • 3. Language: Script language, must be "PY".
    • 4. Pre: Python command(s) to prepend to the file contents. (optional)
    • 5. File: File to load script commands from in [addon]/user_files. (optional)
    • 6. Post: Python command(s) to append to the file contents. (optional)

Sample configuration below. <pre><code>{ "Settings": { "Editor shortcut": "", "Popup shortcut": "Ctrl+Alt+j", "Size mode": "last", "CSS": "" }, "Scripts & Symbols": [ { "1. Menu": "Symbols", "2. Shortcut": "Shift+Alt+S", "3. Items": [ { "1. Menu": "Arrows", "2. Shortcut": "Shift+Alt+A", "3. Items": [ { "1. Symbol": "←", "2. Shortcut": "Alt+Left", "3. HTML": "false" }, { "1. Symbol": "β†’", "2. Shortcut": "Alt+Right", "3. HTML": "false" } ] }, { "1. Symbol": "Ξ±", "2. Shortcut": "Alt+a", "3. HTML": "false" }, { "1. Symbol": "Ξ²", "2. Shortcut": "Alt+b", "3. HTML": "false" } ] }, { "1. Script": "Lowercase", "2. Shortcut": "Shift+Alt+L", "3. Language": "JS", "4. Pre": "(function(){const sel = document.activeElement.shadowRoot.getSelection(); for (i = 0; i < sel.rangeCount; i++) { const rng = sel.getRangeAt(i); let input = new XMLSerializer().serializeToString(rng.extractContents()); input = input.toLowerCase(); rng.insertNode(rng.createContextualFragment(input)); sel.addRange(rng);}})();", "5. File": "", "6. Post": "" } ] }</code></pre>

Misc

Feel free to share your script snippets in https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536

CHANGELOG

  • Reimplementation from "Editor JS snippets": new format of config file (see above) to allow arbitrary submenu structure (unfortunately breaks previous configs), added python script support, added clipboard content to JavaScript environment, change addon name to match use.
  • 2022-02-04 Add option to not cache script files, i.e. reload them from disk each script execution.
  • 2022-05-13 Qt5 β†’ 6 bug fixes (QMenu and QKeySequence).
  • 2022-05-18 Qt5 β†’ 6 bug fixes (QMenu and QKeySequence).
  • 2022-06-02 Code refactor, update config.json format, adding addon CodeMirror 6 config.json dialog and automatic reload to context menu, remove cache option (scripts now always reloaded on each invocation), add automatic backup of config on save.