I’m developing an addon that acts on a left click during review. Finishing a deck causes this to stop working.
The addon wraps the web view’s event filter:
AnkiWebView.eventFilter = wrap(AnkiWebView.eventFilter, eventFilter, “around”)
or
AnkiWebView.eventFilter = eventFilter
with
def eventFilter(self, obj, evt):
# code here is still executed
if evt.type() == QEvent.MouseButtonRelease and evt.button() == Qt.LeftButton:
# code here doesn’t work anymore
Everything works fine until a deck is finished, i.e. all cards in the deck are completed. Afterwards, eventFilter is still called, but it doesn’t receive mouse events anymore. I digged around in the source code, but couldn’t find an answer myself. Another addon written by someone else is also affected by this.
Can someone shed light on what’s happening here? The issue is easily reproduced by adding an empty deck, choosing it in the deck brower, then switching to another deck.
Instead of monkey-patching that routine, perhaps you’d have more success injecting an event handler into the DOM using .eval(). The congrats screen is a separate webpage, so you’d need to do it there too.
Thanks for pointing me into this direction. Since I have close to zero knowledge in web development, this is very hard to use and debug. Can you point me at some example or other resource? Even after googling and trying other addons for a while now I’m almost clueless.
Thanks for your hint, I could solve it. However, document.addEventListener didn’t work for two reasons:
.eval() needs to be evoked multiple times, because every time the content of the web view changes, the event handler is lost.
depending on the hook that triggers injection of the javascript code, addEventListener accumulates handlers. These are all executed in a row afterwards.
I faced similar problems developing Review Hotmouse addon. (addon for mouse shortcuts/gestures)
To solve this purely on qt side, you need to listen for AnkiWebView’s child event for child creation, then install event filters on the newly created child as well.
As long as it’s only mouse press/release event you are listening to, you won’t need to inject a script to the web. But you can find the code to do both in my addon code which needed to inject a js script for the mouse wheel event.
Thanks for your response! However, the other addon with the same problem I mentioned in my initial post is actually yours. I searched Ankiweb for mouse and keyboard-related addons and checked your code for a possible solution. In Anki 2.1.44, it has the same problem I described in my post and in 2.1.46, it’s not loading anymore. If you’re interested interested in my solution, I can share some code with you.