QWebEnginePage: Solutions for running JavaScript and returning values from HTML document?
-
I have been using
QWebEnginePage::runJavaScript();
with this being executed after loading a web page withQWebEnginePage::setUrl();
and calling a slot afterQWebEnginPage::loadFinished();
I am running into a few issues.
-
1: runJavaScript returns values that change after being quickly called a second time. Usually this amounts to more elements found by
querySelectorAll();
..eventually the actual amount of elements the HTML page contains are returned... but sometimes this isn't true. -
2: Certain pages do not return large groups of elements that
querySelectorAll();
would normally return.
Is there a more sophisticated way to run and manage JavaScripts with QWebEnginePage? Or a better way altogether? Is this a injection point or worldid issue?
I am aware of QWebEngineScript class, although I'm not sure how to use this to execute a JavaScript. I have been introduced to Greasemonkey but don't know where to begin with chromium.
-
-
This seems like you want to use the QWebChannel api.
I did that one time following their API docs, and here is the result:
https://github.com/enjoysmath/Marked-KaTeX-example-cpp
It's a Markdown editor + KaTeX (for math type-setting) using a QWebEngineWidget and QWebChannel to pass data between. The host language is C++.
-
@desun_evan Did you get solutions to this problem?
-
@desun_evan
@enjoysmath'sQWebChannel
may well be the correct way to go.I have a recollection of using
QWebEnginePage::runJavaScript()
andQWebEnginPage::loadFinished()
which just might be relevant.I think page load finished comes before JS document ready event. I used
runJavaScipt()
to setdocument.onReady
(or whatever it is, or jQueryready()
) to execute code which needed to be sure the document tree had been fully constructed. Worth a try for your case? -
I will try these suggestions. Thank you very much!