|
| 1 | +# Background |
| 2 | + |
| 3 | +Currently, a developer can pass the --user-agent browser args to the CreateWebView2EnvironmentWithDetails function. |
| 4 | + |
| 5 | +Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\\"myUA\\"", ...); |
| 6 | + |
| 7 | +For more info about the ‘--user-agent’ flag visit: https://peter.sh/experiments/chromium-command-line-switches/#user-agent. |
| 8 | + |
| 9 | +However, there are a couple limitations to this workaround-- it is not an API that is easy to use or discover, you cannot modify a command line switch at runtime, and you cannot change the User Agent per WebView. In this document we describe the new API. We'd appreciate your feedback. |
| 10 | + |
| 11 | +# Description |
| 12 | + |
| 13 | +The User Agent (UA) is a piece of information regarding the user's OS, application, and version. The browser/webcontrol sends the User Agent to the HTTP server. |
| 14 | + |
| 15 | +The User Agent property lets developers modify WebView2's User Agent. A key scenario is to allow end developers to get the current User Agent from the WebView and modify it based on an event. |
| 16 | + |
| 17 | +Ex. Update the User Agent to emulate a different browser version upon navigation to a specific website. |
| 18 | + |
| 19 | +# Examples |
| 20 | + |
| 21 | +The following code snippet demonstrates how the User Agent property can be used: |
| 22 | + |
| 23 | +## Win32 C++ |
| 24 | + |
| 25 | +```cpp |
| 26 | +m_webView->add_NavigationStarting( |
| 27 | + Callback<ICoreWebView2NavigationStartingEventHandler>( |
| 28 | + [this](ICoreWebView2 *sender, |
| 29 | + ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT { |
| 30 | + static const PCWSTR url_compare_example = L"fourthcoffee.com"; |
| 31 | + wil::unique_cotaskmem_string uri; |
| 32 | + CHECK_FAILURE(args->get_Uri(&uri)); |
| 33 | + wil::unique_bstr domain = GetDomainOfUri(uri.get()); |
| 34 | + const wchar_t *domains = domain.get(); |
| 35 | + wil::com_ptr<ICoreWebView2Settings> settings; |
| 36 | + CHECK_FAILURE(m_webView->get_Settings(&m_settings)); |
| 37 | + if (wcscmp(url_compare_example, domains) == 0) { |
| 38 | + // Upon navigation to a specified url |
| 39 | + // Change the user agent to emulate a mobile device |
| 40 | + CHECK_FAILURE(settings->put_UserAgent(GetMobileUserAgent())); |
| 41 | + } else { |
| 42 | + // Change the user agent back to desktop |
| 43 | + CHECK_FAILURE(settings->put_UserAgent(GetDesktopUserAgent())); |
| 44 | + } |
| 45 | + return S_OK; |
| 46 | + }) |
| 47 | + .Get(), |
| 48 | + &m_navigationStartingToken); |
| 49 | +``` |
| 50 | +
|
| 51 | +## .NET and WinRT |
| 52 | +
|
| 53 | +```c # |
| 54 | +webView2Control.NavigationStarting += SetUserAgent; |
| 55 | +
|
| 56 | +private void SetUserAgent(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs e) |
| 57 | +{ |
| 58 | + var settings = webView2Control.CoreWebView2.Settings; |
| 59 | + // Note: Oversimplified test. Need to support idn, case-insensitivity, etc. |
| 60 | + if (new Uri(e.Uri).Host == "fourthcoffee.com") { |
| 61 | + settings.UserAgent = GetMobileUserAgent(); |
| 62 | + } else { |
| 63 | + settings.UserAgent = GetDesktopUserAgent(); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +# API Notes |
| 69 | + |
| 70 | +See [API Details](#api-details) section below for API reference. |
| 71 | + |
| 72 | +# API Details |
| 73 | + |
| 74 | +## Win32 C++ |
| 75 | + |
| 76 | +```IDL |
| 77 | +// This is the ICoreWebView2Settings interface. |
| 78 | +[uuid(684cbeef-47ba-4d4a-99f4-976113f9f10a), object, pointer_default(unique)] |
| 79 | +interface ICoreWebView2Settings2 : ICoreWebView2Settings { |
| 80 | + /// `UserAgent` . Returns the User Agent. The default value is the |
| 81 | + /// default User Agent of the Edge browser. |
| 82 | + [propget] HRESULT UserAgent([ out, retval ] LPWSTR * userAgent); |
| 83 | + /// Sets the `UserAgentString` property. This property may be overriden if |
| 84 | + /// the User-Agent header is set in a request. If the parameter is empty |
| 85 | + /// the User Agent will not be updated and the current User Agent will remain. |
| 86 | + [propput] HRESULT UserAgent([in] LPCWSTR userAgent); |
| 87 | +} |
| 88 | +``` |
| 89 | +## .NET and WinRT |
| 90 | + |
| 91 | +```c# |
| 92 | +namespace Microsoft.Web.WebView2.Core |
| 93 | +{ |
| 94 | + public partial class CoreWebView2Settings |
| 95 | + { |
| 96 | + public string UserAgent { get; set; }; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
0 commit comments