Skip to content

Instantly share code, notes, and snippets.

@glutanimate
Last active October 9, 2018 20:28
Show Gist options
  • Save glutanimate/b5c16c7e5bc3b96e7388af767bfd6080 to your computer and use it in GitHub Desktop.
Save glutanimate/b5c16c7e5bc3b96e7388af767bfd6080 to your computer and use it in GitHub Desktop.
Quick proof-of-concept add-on for Anki web view hooks PR. Please copy into subfolder in addons21 directory to test.
# Quick proof-of-concept add-on for Anki PR:
from anki.hooks import addHook
def addCSS(head, instance):
print(head, instance)
return head + "<style>.addition1{color:green;}</style>"
def addCSS2(head, instance):
print(head, instance)
return head + "<style>.addition2{color:blue}</style>"
def addHTML(html, instance, screen, part):
print(html, instance)
return html + "<div class=addition1><b>first {} to {}</b></div>".format(part, screen)
def addHTML2(html, instance, screen, part):
print(html, instance)
return html + "<div class=addition2><b>second {} to {}</b></div>".format(part, screen)
# general usage: addHook("deckbrowserWebPrefix", addHTML)
# Heads, prefixes, and suffixes
for screen in ("overview", "deckbrowser", "bottombar", "reviewer",
"reviewerBottom", "preview", "editor", "stats", "toolbar",
"cardlayout"):
addHook(screen + "WebPrefix", lambda h, c, s=screen, p="prefix":addHTML(h, c, s, p))
addHook(screen + "WebPrefix", lambda h, c, s=screen, p="prefix":addHTML2(h, c, s, p))
addHook(screen + "WebSuffix", lambda h, c, s=screen, p="suffix":addHTML(h, c, s, p))
addHook(screen + "WebSuffix", lambda h, c, s=screen, p="suffix":addHTML2(h, c, s, p))
addHook(screen + "WebHead", addCSS)
addHook(screen + "WebHead", addCSS2)
# FIXME?: Toolbar is drawn in mw.setupMainWindow(), i.e. before mw.setupAddons(),
# thus needs to be redrawn for changes to appear
from aqt import mw
mw.toolbar.draw()
# Stats area in Overview and DeckBrowser
addHook("deckBrowserWebStats", lambda h, c, s="deckbrowser", p="stats":addHTML(h, c, s, p))
addHook("deckBrowserWebStats", lambda h, c, s="deckbrowser", p="stats":addHTML2(h, c, s, p))
addHook("overviewWebStats", lambda h, c, s="overview", p="stats":addHTML(h, c, s, p))
addHook("overviewWebStats", lambda h, c, s="overview", p="stats":addHTML2(h, c, s, p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment