How to edit context menu on right click in reviewer?

Unfortunately reviewer’s showContextMenu has nothing to do with right clicking but is the menu spawned by the Morev button.

I tried using oncontextmenu in HTML but that also didn’t work.

Maybe this might help:

https://wiki.python.org/moin/PyQt/Handling%20context%20menus

1 Like

Thank you. I’ve also previously found out how to add a right click handler to QWidget. But I don’t know how to get the QWidget that is the reviewer from inside of anki. How can I get the widget?

I’m not sure I understand the problem but maybe this is useful:

anki/reviewer.py at fd013f68d2de8cfc58a6f4ecce4519e6c3acd260 · ankitects/anki · GitHub passes the QMenu so that with reviewer_will_show_context_menu you can use and modify the QMenu. reviewer_will_show_context_menu also passes the reviewer instance and the webview that shows the actual cards/reviews is stored in self.web

e.g. I have this code in one of my add-ons: anki21__editor_add_hyperlink/add_hyperlinks.py at de080aa4f317f24a2a720eee1c24dc88c6bcd04b · ijgnd/anki21__editor_add_hyperlink · GitHub

5 Likes

Your code is extremely helpful, thank you. The AnkiWebView.contextMenuEvent hook is it, I should be able to do what I want now. Do you have any idea how I may be able to tell what was clicked? I specifically want to add context menus to audio play buttons.

check the html source code of the reviewer and what the media buttons do. They send some code to python with pycmd. on the python side in aqt/reviewer.py you have a a linkhandler command that catches those “signals” coming from js, see anki/reviewer.py at fd013f68d2de8cfc58a6f4ecce4519e6c3acd260 · ankitects/anki · GitHub

 

To learn about the reviewer webview there’s:

You can now debug the webviews using an external Chrome instance, by setting the env var QTWEBENGINE_REMOTE_DEBUGGING to 8080 prior to starting Anki, then surfing to localhost:8080 in Chrome.

Porting 2.0 Add-ons - Writing Anki Add-ons. this is also in an add-on named AnkiWebView Inspector.

 

To send the pycmd play in the webview side I guess you have some sort of clickable hyperlink where the target is maybe a js function that emits play: … So when you right click the button you maybe have this link data and - similar to my hyperlink detection in my add hyperlink add-on - you can analyze each link clicked

1 Like

There still seems to be no way to get the HTML element that was clicked from the contextMenuEvent.

It is passed (webview: AnkiWebView, menu: QMenu) arguments. None of these can help me get the onclick="pycmd('play:q:0')" attribute of the audio element that was right-clicked. I can get link URL (href attribute) or link text (.innerText I’m assuming) but not the HTML element itself.

Is there a way to get the HTML element that was right-clicked in the context menu event?

Otherwise, how do you think I can keep track of what’s being hovered? The only hover event I found so far, QWebEnginePage#linkHovered(const QString & url), doesn’t pass the HTML Element either only the link’s href.

I probably don’t have useful ideas anymore. I thought that you only needed the path of the audio file and not the html element.

I exactly want to get the path of the audio file.

I don’t see how I can do that from the context menu event. I made another question for it: How to get the right-clicked audio filename in webview context menu event?

check how in aqt/sound.py the real path is derived from the “play:q:0”, i.e. at anki/sound.py at fd013f68d2de8cfc58a6f4ecce4519e6c3acd260 · ankitects/anki · GitHub

card.question_av_tags() looks promising. A crude but effective way to find out the output of this method would be to use print debugging - simply wrap play_clicked_audio and print the output of card.question_av_tags() - for details about wrapping see Monkey Patching - Writing Anki Add-ons

then you could continue to build your actual add-on.