From edcaf5210ee5ae4c301bbae630e789784120d081 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Mon, 15 Feb 2021 16:15:29 +0100 Subject: [PATCH] Implement script message for edge backend --- include/wx/msw/private/webview_edge.h | 2 ++ src/msw/webview_edge.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/wx/msw/private/webview_edge.h b/include/wx/msw/private/webview_edge.h index f78d984395..bb5063807e 100644 --- a/include/wx/msw/private/webview_edge.h +++ b/include/wx/msw/private/webview_edge.h @@ -47,6 +47,7 @@ public: EventRegistrationToken m_documentTitleChangedToken = { }; EventRegistrationToken m_contentLoadingToken = { }; EventRegistrationToken m_containsFullScreenElementChangedToken = { }; + EventRegistrationToken m_webMessageReceivedToken = { }; // WebView Event handlers HRESULT OnNavigationStarting(ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args); @@ -56,6 +57,7 @@ public: HRESULT OnDocumentTitleChanged(ICoreWebView2* sender, IUnknown* args); HRESULT OnContentLoading(ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args); HRESULT OnContainsFullScreenElementChanged(ICoreWebView2* sender, IUnknown* args); + HRESULT OnWebMessageReceived(ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args); HRESULT OnEnvironmentCreated(HRESULT result, ICoreWebView2Environment* environment); HRESULT OnWebViewCreated(HRESULT result, ICoreWebView2Controller* webViewController); diff --git a/src/msw/webview_edge.cpp b/src/msw/webview_edge.cpp index 355d384e31..22846b93a4 100644 --- a/src/msw/webview_edge.cpp +++ b/src/msw/webview_edge.cpp @@ -73,6 +73,7 @@ wxWebViewEdgeImpl::~wxWebViewEdgeImpl() m_webView->remove_DocumentTitleChanged(m_documentTitleChangedToken); m_webView->remove_ContentLoading(m_contentLoadingToken); m_webView->remove_ContainsFullScreenElementChanged(m_containsFullScreenElementChangedToken); + m_webView->remove_WebMessageReceived(m_webMessageReceivedToken); } } @@ -318,6 +319,20 @@ HRESULT wxWebViewEdgeImpl::OnContainsFullScreenElementChanged(ICoreWebView2* WXU return S_OK; } +HRESULT wxWebViewEdgeImpl::OnWebMessageReceived(ICoreWebView2* WXUNUSED(sender), ICoreWebView2WebMessageReceivedEventArgs* args) +{ + wxCoTaskMemPtr msgContent; + + wxWebViewEvent event(wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, m_ctrl->GetId(), + m_ctrl->GetCurrentURL(), wxString()); + event.SetEventObject(m_ctrl); + if (SUCCEEDED(args->get_WebMessageAsJson(&msgContent))) + event.SetString(wxString(msgContent)); + m_ctrl->HandleWindowEvent(event); + + return S_OK; +} + HRESULT wxWebViewEdgeImpl::OnWebViewCreated(HRESULT result, ICoreWebView2Controller* webViewController) { if (FAILED(result)) @@ -367,6 +382,10 @@ HRESULT wxWebViewEdgeImpl::OnWebViewCreated(HRESULT result, ICoreWebView2Control Callback( this, &wxWebViewEdgeImpl::OnContainsFullScreenElementChanged).Get(), &m_containsFullScreenElementChangedToken); + m_webView->add_WebMessageReceived( + Callback( + this, &wxWebViewEdgeImpl::OnWebMessageReceived).Get(), + &m_webMessageReceivedToken); if (m_pendingContextMenuEnabled != -1) {