1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

WebPage: Add timeout to execJavaScript

This commit is contained in:
David Rosca 2015-09-30 11:01:10 +02:00
parent 36929a45bf
commit c042d86467
2 changed files with 14 additions and 7 deletions

View File

@ -107,15 +107,22 @@ WebView *WebPage::view() const
return static_cast<WebView*>(QWebEnginePage::view()); return static_cast<WebView*>(QWebEnginePage::view());
} }
QVariant WebPage::execJavaScript(const QString &scriptSource) QVariant WebPage::execJavaScript(const QString &scriptSource, int timeout)
{ {
QEventLoop loop; QPointer<QEventLoop> loop = new QEventLoop;
QVariant result; QVariant result;
runJavaScript(scriptSource, [&loop, &result](const QVariant &res) { QTimer::singleShot(timeout, loop, &QEventLoop::quit);
result = res;
loop.exit(); runJavaScript(scriptSource, [loop, &result](const QVariant &res) {
if (loop && loop->isRunning()) {
result = res;
loop->quit();
}
}); });
loop.exec();
loop->exec();
delete loop;
return result; return result;
} }

View File

@ -50,7 +50,7 @@ public:
WebView *view() const; WebView *view() const;
QVariant execJavaScript(const QString &scriptSource); QVariant execJavaScript(const QString &scriptSource, int timeout = 500);
WebHitTestResult hitTestContent(const QPoint &pos) const; WebHitTestResult hitTestContent(const QPoint &pos) const;