View Full Version : runJavascript result returned
Kasea
6th August 2017, 09:30
I'm trying to get the results from my JS statements, however I don't seem to get the desired result. It just appears 2 return an empty string.
For example I load google -> runJavascript document.body. and it prints an empty string.
Is this intended? is there a workaround so I would get the desired result?
Ginsengelf
7th August 2017, 08:46
Hi, could you show us your code?
Ginsengelf
Kasea
7th August 2017, 09:32
Yeah sure, couldn't find the edit button, if there is any. so I'll post it here instead of editing main post
#include <QApplication>
#include <QWebEngineView>
#include <iostream>
using namespace std;
class Webkit: public QWebEngineView{
public:
Webkit() {
connect(this->page(), &QWebEnginePage::loadFinished, this, &Webkit::onFinishedLoading);
}
public slots:
void onFinishedLoading(bool ok);
};
void Webkit::onFinishedLoading(bool ok){
this->page()->runJavaScript("document.body;", [](const QVariant &v) { cout << "JS Result:" << v.toString().toStdString() << endl; });
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);
QApplication app(argc, argv);
Webkit kit;
kit.show();
kit.setUrl(QUrl("http://google.com"));
return app.exec();
}
One would expect the following code to have some output on the JS result, however it's empty. (Qt 5.8)
patrik08
7th August 2017, 14:30
runJavaScript is to run your own javascript i think..
not script from other ...
i load Qt5::Script to test but i see javascript alert().
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
target_link_libraries(${PROJECT_NAME} Qt5::WebEngineWidgets)
target_link_libraries(${PROJECT_NAME} Qt5::Script)
#include <QApplication>
#include <QWebEngineView>
#include <iostream>
using namespace std;
class Webkit: public QWebEngineView{
public:
Webkit() {
connect(this->page(), &QWebEnginePage::loadFinished, this, &Webkit::onFinishedLoading);
}
public slots:
void onFinishedLoading(bool ok);
};
void Webkit::onFinishedLoading(bool ok){
this->page()->runJavaScript("document.body;alert(\"I am an alert box!\")", [](const QVariant &v) { cout << "JS Result:" << v.toString().toStdString() << endl; });
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);
QApplication app(argc, argv);
Webkit kit;
kit.show();
kit.setUrl(QUrl("https://wiki.selfhtml.org/wiki/JavaScript/Objekte/Date/getFullYear"));
return app.exec();
}
Kasea
8th August 2017, 03:15
If it was to run my own javascripts, that'd be fine. but what would be the purpose of a result lambda option then?
Could this be a bug or is it intended?
Ginsengelf
8th August 2017, 09:34
If it was to run my own javascripts, that'd be fine. but what would be the purpose of a result lambda option then?
I have used this to extract info like the used font or parts of the HTML of a file.
Ginsengelf
patrik08
8th August 2017, 14:56
Yes or to take resultCallback ... from javascript like a pointer..
On this way i think we can use google api to translate o other external game..
Kasea
9th August 2017, 08:10
I see.. Well I'll find a workaround for my problem then, thanks.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.