I have created a small helper function to modify the text of the editor. For the moment, I am able to modify the text but not the underlying HTML. Any idea how I could do that?
Here is the code:
def formatting(editor, treatment) -> None:
"""
Helper function used by formatting functions
Allows to write new text in editor
"""
# data = editor.web.selectedText()
# data = editor.web.page().runJavaScript("document.getElementsByTagName('html')[0].innerHTML", callback_func)
data_formatted = treatment(data)
data_formatted_json = json.dumps(data_formatted)
editor.web.eval(
f"document.execCommand('inserthtml', false, {data_formatted_json});"
)
I have tried to replace
data = editor.web.selectedText()
by
data = editor.web.page().runJavaScript(âdocument.getElementsByTagName(âhtmlâ)[0].innerHTMLâ, callback_func)`
with a simple callback function defined as followed:
Since QWebEnginePage::runJavaScript() is a void method (returns None in Python), you cannot assign the result of the last executed javascript statement to a variable as the return value of runJavaScript(). The result will be delivered to the callback function as the first argument.
By the way, you can get the entire <html> element via document.documentElement. In addition, you can use aqt.webview.AnkiWebView.evalWithCallback() instead of page().runJavaScript().
Thank you very much. Actually, the purpose of the function is to modify the text of the field, not the editor itself (I should have been clearer in my first message). The result of the new function was quite unexpected!
You might want to consider using editor.doPaste() instead of directly doing an eval call with document.execCommand('inserthtml', ...), because it eventually does that under-the-hood anyway. Then thereâs no need to call json.dumps() manually either.