I'm trying to add to a PyQt5 application a QtWebEngineView widget. This widget must load the following very simple SVG:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="100" r="50" fill="red" />
<script type="text/javascript"><![CDATA[
alert("bar")
mynamespace.myfunction();
]]></script>
</svg>
But obviously, I need to define mynamespace beforehand, so I'm trying the following in my widget constructor:
class MyWebPageView(QtWebEngineView):
def __init__(svg,*args,**kwargs):
super().__init__(*args,**kwargs)
self.page().runJavaScript(
"""
alert("foo");
const mynamespace = { myfunction: function() { alert("success");}};
""")
time.sleep(3) #to be sure this is not a timing issue
self.load(QUrl("file:///path/to/svg.svg"))
I see the "foo" alert, then the "bar" alert. The red circle is properly drawn, but the "success" alert never shows, and I get the following error message:
js: Uncaught ReferenceError: mynamespace is not defined
What have I done wrong?
InjectionPoint.DocumentCreation, theQWebEngineScriptcode is called after the JS embedded in the SVG, thus generating the same error message.