|
1 | 1 | # Background |
2 | 2 |
|
3 | | - |
4 | 3 | Currently, a developer can pass the --user-agent browser args to the CreateWebView2EnvironmentWithDetails function. |
5 | 4 | Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\"myUA\"", ...); |
6 | 5 | For more info about the ‘—user - agent’ flag visit: https://peter.sh/experiments/chromium-command-line-switches/#user-agent. |
7 | 6 |
|
8 | 7 | However, there are a couple limitations to this workaround-- you cannot modify a command line switch at runtime, nor can you change the user agent per webview. In this document we describe the new API. We'd appreciate your feedback. |
9 | 8 |
|
10 | | --- |
11 | | - |
12 | | -The Settings component will change the UA per WebView2 via Chrome Developer Protocol command (CDP). A key scenario is to allow end developers to get the current user agent from the webview and modify it based on some sort of event, such as navigating to a specific website and setting the user agent to emulate a different browser version. |
13 | | - |
14 | | - |
15 | 9 | # Description |
16 | 10 |
|
17 | 11 | The User Agent (UA) is a client-side piece of information that the browser/webcontrol sends to the server or website a user visits. It contains information about user’s system and is modifiable by the user. |
18 | 12 |
|
19 | 13 | The User Agent String API allows developers to modify WebView's user agent string via Chrome Developer Protocol (CDP). The user agent can be changed based on different events such as navigation to a specific website. |
20 | 14 |
|
| 15 | +The Settings component will change the UA per WebView2 via Chrome Developer Protocol command (CDP). A key scenario is to allow end developers to get the current user agent from the webview and modify it based on some sort of event, such as navigating to a specific website and setting the user agent to emulate a different browser version. |
| 16 | + |
21 | 17 | # Examples |
22 | 18 |
|
23 | 19 | The following code snippet demonstrates how the environment APIs can be used |
@@ -45,7 +41,7 @@ m_webView->add_NavigationStarting( |
45 | 41 | CHECK_FAILURE(settings->put_UserAgent(mobile_ua)); |
46 | 42 | LPCWSTR received_ua; |
47 | 43 | CHECK_FAILURE(settings->get_UserAgent(&received_ua)); |
48 | | - EXPECT_EQ(base::Value(received_ua), base::Value(mobile_ua)) |
| 44 | + EXPECT_STREQ(received_ua, mobile_ua); |
49 | 45 | } |
50 | 46 | return S_OK; |
51 | 47 | }) |
@@ -76,7 +72,7 @@ See [API Details](#api-details) section below for API reference. |
76 | 72 | interface ICoreWebView2StagingSettings : IUnknown { |
77 | 73 | /// `UserAgent` . Returns the User Agent. The default value is the |
78 | 74 | /// default User Agent. |
79 | | - [propget] HRESULT UserAgent([ out, retval ] LPCWSTR * userAgent); |
| 75 | + [propget] HRESULT UserAgent([ out, retval ] LPWSTR * userAgent); |
80 | 76 | /// Sets the `UserAgentString` property. This property may be overriden if |
81 | 77 | /// the User-Agent header is set in a request. |
82 | 78 | [propput] HRESULT UserAgent([in] LPCWSTR userAgent); |
|
0 commit comments