diff options
| author | Kaloyan Chehlarski <kaloyan.chehlarski@qt.io> | 2025-11-05 16:25:46 +0100 |
|---|---|---|
| committer | Michal Klocek <michal.klocek@qt.io> | 2025-12-01 17:39:09 +0100 |
| commit | 388251ce343c617fc7a383485e9417a5306adb14 (patch) | |
| tree | d658d527356e3ebea469c59ef15af5dc05d3a8e8 /src/plugins/windows/qwebview2webview.cpp | |
| parent | c0b0855c702854c65ecdfe39a97d3f969b8780ad (diff) | |
Add runJavascript() to C++ API
The API follows what we have in WebEngine, with an optional
std::function callback. No world ID argument is provided, however;
most of the backends don't support the concept.
The new function is not exposed to QML, since we cannot expose
std::function arguments. Instead, the existing QML API is kept,
and calls to it simply redirect to the C++ implementation.
Also removes runJavaScriptPrivate() from QWebView, and its related
implementation details in the plugins.
Task-number: QTBUG-131837
Change-Id: Idc1e492916e17caa3f061c0b2738b3c735837cf8
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'src/plugins/windows/qwebview2webview.cpp')
| -rw-r--r-- | src/plugins/windows/qwebview2webview.cpp | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/plugins/windows/qwebview2webview.cpp b/src/plugins/windows/qwebview2webview.cpp index 204d9d8..205f7bb 100644 --- a/src/plugins/windows/qwebview2webview.cpp +++ b/src/plugins/windows/qwebview2webview.cpp @@ -637,14 +637,15 @@ void QWebView2WebViewPrivate::updateWindowGeometry() } } -void QWebView2WebViewPrivate::runJavaScriptPrivate(const QString &script, int callbackId) +void QWebView2WebViewPrivate::runJavaScript( + const QString &script, const std::function<void(const QVariant &)> &resultCallback) { if (m_webview) { - const HRESULT hr = m_webview->ExecuteScript( + m_webview->ExecuteScript( (wchar_t *)script.utf16(), Microsoft::WRL::Callback<ICoreWebView2ExecuteScriptCompletedHandler>( - [this, callbackId](HRESULT errorCode, - LPCWSTR resultObjectAsJson) -> HRESULT { + [this, resultCallback](HRESULT errorCode, + LPCWSTR resultObjectAsJson) -> HRESULT { QString resultStr = QString::fromWCharArray(resultObjectAsJson); QJsonParseError parseError; @@ -664,15 +665,16 @@ void QWebView2WebViewPrivate::runJavaScriptPrivate(const QString &script, int ca resultVariant = val.toVariant(); } } - if (errorCode != S_OK) - emit q_ptr->javaScriptResult(callbackId, - qt_error_string(errorCode)); - else - emit q_ptr->javaScriptResult(callbackId, resultVariant); + QVariant r = + errorCode == S_OK ? resultVariant : qt_error_string(errorCode); + if (resultCallback) + resultCallback(r); return errorCode; }) .Get()); - Q_ASSERT_SUCCEEDED(hr); + } else { + if (resultCallback) + resultCallback(QVariant()); } } |
