A lot of hook inside of hooks_gen.py are outdated without new implementations or replacements.
For example, the card_will_flush or note_will_flush hooks are practically deprecated however there is no comment telling the user that they are. If I check their references, they are only called inside the deprecated flush() function.
Furthermore, there has been no replacement for the card_will_flush hook. As far as I know, I can’t add a hook for when a new card is created and while gui_hooks.add_cards_did_add_note mostly suffices, when other add-ons like Ankiconnect add a new card, there is no way to detect it.
It effectively breaks my add-on if the user creates notes using Yomitan or other tools.
For anyone looking for a solution, I have found one but it is hacky. The following will only check for if Yomitan cards are created, but this can be modified to suit whatever add-on. AnkiConnect might by default tag its notes with “AnkiConnect” but I cannot verify that.
Quick summary:
Connect to Anki’s note_will_be_added hook
Filter for only notes with the tag Yomitan
Create a 1 second timer that repeats until a card is created on the note
Ensure the timer stops after 10 seconds of not finding a card (usually only gets to 1)
from aqt import qt
from anki import hooks_gen
# Called whenever a Yomitan card is added
def yomitan_card_created(note):
return
# For detecting cards being added through AnkiConnect.
def internal_note_added(col, note, deckId):
if not note.has_tag("Yomitan"):
return
print("note added by Yomitan")
elapsed = 0
timer = qt.QTimer()
def timer_ended():
nonlocal elapsed
nonlocal note
elapsed += 1
if len(note.card_ids()) > 0:
yomitan_card_created(note)
return
if elapsed < 10:
timer.singleShot(1000, timer_ended)
timer.singleShot(1000, timer_ended)
hooks_gen.note_will_be_added.append(internal_note_added)
Make sure if your add-on is adding its own notes to a deck that you filter them out with: