Skip to content

Commit

Permalink
Merge pull request #22 from ShahRishi/docs/setWebExports
Browse files Browse the repository at this point in the history
docs/setWebExports
dae authored Feb 21, 2024
2 parents 3cd3ad8 + a649c9c commit 0daa95f
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/hooks-and-filters.md
Original file line number Diff line number Diff line change
@@ -116,6 +116,27 @@ From Anki 2.1.36:
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 utilizing `aqt.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:
```python
from aqt import mw
mw.addonManager.setWebExports(__name__, r"web/.*(css|js)")
```
Then, append the subpaths to the corresponding web_content fields within a function subscribing to `gui_hooks.webview_will_set_content`:
```python
def on_webview_will_set_content(web_content: WebContent, context) -> None:
addon_package = mw.addonManager.addonFromModule(__name__)
web_content.css.append(f"/_addons/{addon_package}/web/my-addon.css")
web_content.js.append(f"/_addons/{addon_package}/web/my-addon.js")
```
Note that '/' will also match the os specific path separator.



## Legacy Hook Handling

Older versions of Anki used a different hook system, using the functions
2 changes: 1 addition & 1 deletion src/reviewer-javascript.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ For a general solution not specific to card review, see

Anki provides a hook to modify the question and answer HTML before it is
displayed in the review screen, preview dialog, and card layout screen.
This can be useful for adding Javascript to the card.
This can be useful for adding Javascript to the card. If you wish to load external resources in your card, please see [managing external resources in webviews](hooks-and-filters.md#managing-external-resources-in-webviews).

An example:

0 comments on commit 0daa95f

Please sign in to comment.