Extend Bridge class with new method that passes return value to JS #228
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While putting the final touches on Mouseover Dictionary and porting the add-on to Anki 2.1, I ran into a roadblock with setting up a py<->js interface. On Anki 2.0 I had been using
addToJavaScriptWindowObject
to expose a custom Python object in JS, but that method was longer available with QtWebEngine.I tried the usual by monkey-patching AnkiWebPage, or trying to register my Python object with the QWebChannel at runtime, but none of these attempts were fruitful. So eventually I decided to work on a PR to address this.
At its core, this commit simply reintroduces the
py.run
command that was available in Anki 2.0. It adds a newrun
method to the Bridge class – exposed aspyrun
in JavaScript – for which the return value is passed back to the JavaScript caller. In cases where simply calling a Python handler throughpycmd
is not enough this will allow add-on authors to set up an easy-to-use JS-Py interface.Here's how I made use of that in my add-on, for instance: Python and JavaScript.