diff --git a/include/wx/msw/private/webview_edge.h b/include/wx/msw/private/webview_edge.h index f9bdb76917..e771848fdc 100644 --- a/include/wx/msw/private/webview_edge.h +++ b/include/wx/msw/private/webview_edge.h @@ -58,6 +58,7 @@ public: wxVector m_pendingUserScripts; wxVector m_userScriptIds; wxString m_scriptMsgHandlerName; + wxString m_customUserAgent; // WebView Events tokens EventRegistrationToken m_navigationStartingToken = { }; diff --git a/include/wx/msw/webview_edge.h b/include/wx/msw/webview_edge.h index bb58d97b50..c273697a29 100644 --- a/include/wx/msw/webview_edge.h +++ b/include/wx/msw/webview_edge.h @@ -86,6 +86,8 @@ public: virtual void EnableAccessToDevTools(bool enable = true) wxOVERRIDE; virtual bool IsAccessToDevToolsEnabled() const wxOVERRIDE; + virtual bool SetUserAgent(const wxString& userAgent) 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; diff --git a/src/msw/webview_edge.cpp b/src/msw/webview_edge.cpp index 6ab2b9d1a8..3abb6382eb 100644 --- a/src/msw/webview_edge.cpp +++ b/src/msw/webview_edge.cpp @@ -29,6 +29,7 @@ #ifdef __VISUALC__ #include using namespace Microsoft::WRL; +#include "WebView2EnvironmentOptions.h" #else #include #endif // !__VISUALC__ @@ -91,11 +92,23 @@ bool wxWebViewEdgeImpl::Create() m_historyPosition = -1; wxString userDataPath = wxStandardPaths::Get().GetUserLocalDataDir(); +#ifdef __VISUALC__ + auto options = + Make(); + + if (!m_customUserAgent.empty()) + options->put_AdditionalBrowserArguments( + wxString::Format("--user-agent=\"%s\"", m_customUserAgent).wc_str()); +#endif HRESULT hr = wxCreateCoreWebView2EnvironmentWithOptions( ms_browserExecutableDir.wc_str(), userDataPath.wc_str(), +#ifdef __VISUALC__ + options.Get(), +#else nullptr, +#endif Callback(this, &wxWebViewEdgeImpl::OnEnvironmentCreated).Get()); if (FAILED(hr)) @@ -778,6 +791,20 @@ bool wxWebViewEdge::IsAccessToDevToolsEnabled() const return true; } +bool wxWebViewEdge::SetUserAgent(const wxString& userAgent) +{ + m_impl->m_customUserAgent = userAgent; + // Can currently only be set before Create() + wxCHECK_MSG(!m_impl->m_webViewController, false, "Can't be called after Create()"); + if (m_impl->m_webViewController) + return false; + else + return true; + + // TODO: As of Edge SDK 1.0.790 an experimental API to set the user agent + // is available. Reimplement using m_impl->GetSettings() when it's stable. +} + void* wxWebViewEdge::GetNativeBackend() const { return m_impl->m_webView;