mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Update documentation for webview_will_set_content and WebContent
This commit is contained in:
parent
0e5dea4c9f
commit
3637f466b4
3 changed files with 60 additions and 36 deletions
|
@ -1140,13 +1140,24 @@ class _WebviewWillSetContentHook:
|
||||||
in, and to get a reference to the screen. For example, if your
|
in, and to get a reference to the screen. For example, if your
|
||||||
code wishes to function only in the review screen, you could do:
|
code wishes to function only in the review screen, you could do:
|
||||||
|
|
||||||
|
def on_webview_will_set_content(web_content: WebContent, context):
|
||||||
|
|
||||||
if not isinstance(context, aqt.reviewer.Reviewer):
|
if not isinstance(context, aqt.reviewer.Reviewer):
|
||||||
# not reviewer, do not modify content
|
# not reviewer, do not modify content
|
||||||
return
|
return
|
||||||
|
|
||||||
web_content.js.append("my_addon.js")
|
# reviewer, perform changes to content
|
||||||
web_content.css.append("my_addon.css")
|
|
||||||
web_content.head += "<script>console.log('my_addon')</script>"
|
context: aqt.reviewer.Reviewer
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
web_content.head += "<script>console.log('my-addon')</script>"
|
||||||
web_content.body += "<div id='my-addon'></div>"
|
web_content.body += "<div id='my-addon'></div>"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -103,50 +103,52 @@ class AnkiWebPage(QWebEnginePage): # type: ignore
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class WebContent:
|
class WebContent:
|
||||||
"""Stores all dynamically modified content that a particular web view will be
|
"""Stores all dynamically modified content that a particular web view
|
||||||
populated with.
|
will be populated with.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
body {str} -- HTML body
|
body {str} -- HTML body
|
||||||
head {str} -- HTML head
|
head {str} -- HTML head
|
||||||
css {List[str]} -- List of media server subpaths, each pointing to a CSS file
|
css {List[str]} -- List of media server subpaths,
|
||||||
js {List[str]} -- List of media server subpaths, each pointing to a JS file
|
each pointing to a CSS file
|
||||||
|
js {List[str]} -- List of media server subpaths,
|
||||||
|
each pointing to a JS file
|
||||||
|
|
||||||
Important Notes:
|
Important Notes:
|
||||||
- When modifying the attributes specified above, please make sure your changes
|
- When modifying the attributes specified above, please make sure your
|
||||||
only perform the minimum requried edits to make your add-on work. You should
|
changes only perform the minimum requried edits to make your add-on work.
|
||||||
avoid overwriting or interfering with existing data as much as possible,
|
You should avoid overwriting or interfering with existing data as much
|
||||||
instead opting to append your own changes, e.g.:
|
as possible, instead opting to append your own changes, e.g.:
|
||||||
|
|
||||||
def on_webview_will_set_content(web_content: WebContent, context):
|
def on_webview_will_set_content(web_content: WebContent, context):
|
||||||
web_content.body += "<my_html>"
|
web_content.body += "<my_html>"
|
||||||
web_content.head += "<my_head>"
|
web_content.head += "<my_head>"
|
||||||
web_content.css.append("my_addon.css")
|
|
||||||
web_content.js.append("my_addon.js")
|
|
||||||
|
|
||||||
- The paths specified in `css` and `js` need to be accessible by Anki's media
|
- The paths specified in `css` and `js` need to be accessible by Anki's
|
||||||
server. Web components shipping with Anki are located under the `_anki`
|
media server. All list members without a specified subpath are assumed
|
||||||
subpath.
|
to be located under `/_anki`, which is the media server subpath used
|
||||||
|
for all web assets shipped with Anki.
|
||||||
|
|
||||||
Add-ons may expose their own web components by utilizing
|
Add-ons may expose their own web assets by utilizing
|
||||||
aqt.addons.AddonManager.setWebExports(). Web exports registered in this
|
aqt.addons.AddonManager.setWebExports(). Web exports registered
|
||||||
manner may then be accessed under the `_addons` subpath.
|
in this manner may then be accessed under the `/_addons` subpath.
|
||||||
|
|
||||||
E.g., to allow access to an `addon.js` and `addon.css` residing in a "web"
|
E.g., to allow access to a `my-addon.js` and `my-addon.css` residing
|
||||||
subfolder in your add-on package, first register the corresponding web export:
|
in a "web" subfolder in your add-on package, first register the
|
||||||
|
corresponding web export:
|
||||||
|
|
||||||
> from aqt import mw
|
> from aqt import mw
|
||||||
> mw.addonManager.setWebExports(__name__, r"web/.*(css|js)")
|
> mw.addonManager.setWebExports(__name__, r"web/.*(css|js)")
|
||||||
|
|
||||||
Then append the subpaths to the corresponding web_content fields within a
|
Then append the subpaths to the corresponding web_content fields
|
||||||
function subscribing to gui_hooks.webview_will_set_content:
|
within a function subscribing to gui_hooks.webview_will_set_content:
|
||||||
|
|
||||||
def on_webview_will_set_content(web_content: WebContent, context):
|
def on_webview_will_set_content(web_content: WebContent, context):
|
||||||
addon_package = mw.addonManager.addonFromModule(__name__)
|
addon_package = mw.addonManager.addonFromModule(__name__)
|
||||||
web_content.css.append(
|
web_content.css.append(
|
||||||
f"_addons/{addon_package}/web/addon.css")
|
f"/_addons/{addon_package}/web/my-addon.css")
|
||||||
web_content.js.append(
|
web_content.js.append(
|
||||||
f"_addons/{addon_package}/web/addon.js")
|
f"/_addons/{addon_package}/web/my-addon.js")
|
||||||
"""
|
"""
|
||||||
|
|
||||||
body: str = ""
|
body: str = ""
|
||||||
|
|
|
@ -176,13 +176,24 @@ hooks = [
|
||||||
in, and to get a reference to the screen. For example, if your
|
in, and to get a reference to the screen. For example, if your
|
||||||
code wishes to function only in the review screen, you could do:
|
code wishes to function only in the review screen, you could do:
|
||||||
|
|
||||||
|
def on_webview_will_set_content(web_content: WebContent, context):
|
||||||
|
|
||||||
if not isinstance(context, aqt.reviewer.Reviewer):
|
if not isinstance(context, aqt.reviewer.Reviewer):
|
||||||
# not reviewer, do not modify content
|
# not reviewer, do not modify content
|
||||||
return
|
return
|
||||||
|
|
||||||
web_content.js.append("my_addon.js")
|
# reviewer, perform changes to content
|
||||||
web_content.css.append("my_addon.css")
|
|
||||||
web_content.head += "<script>console.log('my_addon')</script>"
|
context: aqt.reviewer.Reviewer
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
web_content.head += "<script>console.log('my-addon')</script>"
|
||||||
web_content.body += "<div id='my-addon'></div>"
|
web_content.body += "<div id='my-addon'></div>"
|
||||||
""",
|
""",
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue