Add named script handler to edge

This commit is contained in:
Tobias Taschner
2021-02-26 21:51:15 +01:00
parent 140e9825b7
commit 2d007ff3f1
3 changed files with 31 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ public:
wxString m_pendingURL;
int m_pendingContextMenuEnabled;
int m_pendingAccessToDevToolsEnabled;
wxString m_scriptMsgHandlerName;
// WebView Events tokens
EventRegistrationToken m_navigationStartingToken = { };

View File

@@ -90,6 +90,8 @@ public:
virtual bool IsAccessToDevToolsEnabled() const wxOVERRIDE;
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const wxOVERRIDE;
virtual bool AddScriptMessageHandler(const wxString& name) wxOVERRIDE;
virtual bool RemoveScriptMessageHandler(const wxString& name) wxOVERRIDE;
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE;

View File

@@ -333,7 +333,8 @@ wxWebViewEdgeImpl::OnWebMessageReceived(ICoreWebView2* WXUNUSED(sender),
}
wxWebViewEvent event(wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, m_ctrl->GetId(),
m_ctrl->GetCurrentURL(), wxString());
m_ctrl->GetCurrentURL(), wxString(),
wxWEBVIEW_NAV_ACTION_NONE, m_scriptMsgHandlerName);
event.SetEventObject(m_ctrl);
// Try to decode JSON string or return original
@@ -417,7 +418,10 @@ HRESULT wxWebViewEdgeImpl::OnWebViewCreated(HRESULT result, ICoreWebView2Control
wxCOMPtr<ICoreWebView2Settings> settings(GetSettings());
if (settings)
{
settings->put_IsStatusBarEnabled(false);
settings->put_IsWebMessageEnabled(!m_scriptMsgHandlerName.empty());
}
if (!m_pendingURL.empty())
{
@@ -807,6 +811,29 @@ bool wxWebViewEdge::RunScript(const wxString& javascript, wxString* output) cons
return true;
}
bool wxWebViewEdge::AddScriptMessageHandler(const wxString& name)
{
// Edge only supports a single message handler
if (!m_impl->m_scriptMsgHandlerName.empty())
return false;
m_impl->m_scriptMsgHandlerName = name;
wxCOMPtr<ICoreWebView2Settings> settings(m_impl->GetSettings());
if (settings)
settings->put_IsWebMessageEnabled(true);
return true;
}
bool wxWebViewEdge::RemoveScriptMessageHandler(const wxString& WXUNUSED(name))
{
m_impl->m_scriptMsgHandlerName.clear();
wxCOMPtr<ICoreWebView2Settings> settings(m_impl->GetSettings());
if (settings)
settings->put_IsWebMessageEnabled(false);
return true;
}
void wxWebViewEdge::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
{
// TODO: could maybe be implemented via IWebView2WebView5::add_WebResourceRequested