1- #Background
1+ # Background
22The User Agent is a client-side piece of information that the browser/webcontrol sends to the server/website a user visits.
33It contains information about user’s system and is modifiable by the user.
44
@@ -16,7 +16,7 @@ For more info about the ‘—user - agent’ flag visit : https
1616 In this document we describe the new API
1717 .We'd appreciate your feedback.
1818
19- #Description
19+ # Description
2020The Settings component will change the UA per WebView2 via Chrome Developer
2121 Protocol command.(CDP)
2222 A key scenario is to allow end developers to get the current user
@@ -25,77 +25,83 @@ such as navigating to a specific website and setting the user agent to
2525emulate a different browser version
2626 .
2727
28- #Examples
28+ # Examples
2929
3030The following code snippet demonstrates how the environment APIs can be used
3131:
3232
33- ##Win32 C++
33+ ## Win32 C++
3434
35- ```cpp m_webView->add_NavigationStarting(
36- Callback<ICoreWebView2NavigationStartingEventHandler >(
37- [ this] (ICoreWebView2 * sender,
38- ICoreWebView2NavigationStartingEventArgs * args) -> HRESULT {
39- static const PCWSTR url_compare_example = L"foo.org";
40- wil::unique_bstr domain = GetDomainOfUri(uri.get());
41- const wchar_t * domains = domain.get();
42- if (wcscmp(url_compare_example, domains) == 0) {
43- wil::com_ptr<ICoreWebView2Settings > settings;
44- CHECK_FAILURE(m_webView->get_Settings(&m_settings));
45- LPCWSTR mobile_ua =
46- "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) "
47- "AppleWebKit/537.36 (KHTML, like Gecko) "
48- "Chrome/62.0.3202.84 Mobile Safari/537.36";
49- CHECK_FAILURE(settings->put_UserAgent(mobile_ua));
50- LPCWSTR received_ua;
51- CHECK_FAILURE(settings->get_UserAgent(&received_ua));
52- EXPECT_EQ(base::Value(received_ua), base::Value(mobile_ua))
53- }
54- return S_OK;
55- })
56- .Get(),
57- &m_navigationStartingToken);
58- ``` ##.NET and WinRT
59- ```c #private void SetUserAgent(CoreWebView2 sender,
60- CoreWebView2UserAgentArgs e) {
61- var settings = webView2Control.CoreWebView2.Settings;
62- settings.UserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F "
63- "Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) "
64- "Chrome/62.0.3202.84 Mobile Safari/537.36";
65- }
66- ```
35+ ``` cpp
36+ m_webView->add_NavigationStarting (
37+ Callback<ICoreWebView2NavigationStartingEventHandler>(
38+ [this](ICoreWebView2 *sender,
39+ ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT {
40+ static const PCWSTR url_compare_example = L"foo.org";
41+ wil::unique_bstr domain = GetDomainOfUri(uri.get());
42+ const wchar_t *domains = domain.get();
43+ if (wcscmp(url_compare_example, domains) == 0) {
44+ wil::com_ptr<ICoreWebView2Settings> settings;
45+ CHECK_FAILURE(m_webView->get_Settings(&m_settings));
46+ LPCWSTR mobile_ua =
47+ "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) "
48+ "AppleWebKit/537.36 (KHTML, like Gecko) "
49+ "Chrome/62.0.3202.84 Mobile Safari/537.36";
50+ CHECK_FAILURE(settings->put_UserAgent(mobile_ua));
51+ LPCWSTR received_ua;
52+ CHECK_FAILURE(settings->get_UserAgent(&received_ua));
53+ EXPECT_EQ(base::Value(received_ua), base::Value(mobile_ua))
54+ }
55+ return S_OK;
56+ })
57+ .Get(),
58+ &m_navigationStartingToken);
59+ ```
60+
61+ ## .NET and WinRT
62+
63+ ```c #
64+ private void SetUserAgent(CoreWebView2 sender,
65+ CoreWebView2UserAgentArgs e) {
66+ var settings = webView2Control.CoreWebView2.Settings;
67+ settings.UserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F "
68+ "Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) "
69+ "Chrome/62.0.3202.84 Mobile Safari/537.36";
70+ }
71+ ```
6772
68- #API Notes
73+ # API Notes
6974
7075See [ API Details] ( #api-details ) section below for API reference.
7176
72- #API Details
77+ # API Details
7378
7479## Win32 C++
7580
76- ```IDL
77- // This is the ICoreWebView2Settings Staging interface.
78- [uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
79- interface ICoreWebView2StagingSettings : IUnknown {
80- /// `UserAgent` . Returns the User Agent. The default value is the
81- /// default User Agent.
82- [propget] HRESULT UserAgent([ out, retval ] LPCWSTR * userAgent);
83- /// Sets the `UserAgentString` property. This property may be overriden if
84- /// the User-Agent header is set in a request.
85- [propput] HRESULT UserAgent([in] LPCWSTR userAgent);
86- }
87- ```
88- ## .NET and WinRT
81+ ``` IDL
82+ // This is the ICoreWebView2Settings Staging interface.
83+ [uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
84+ interface ICoreWebView2StagingSettings : IUnknown {
85+ /// `UserAgent` . Returns the User Agent. The default value is the
86+ /// default User Agent.
87+ [propget] HRESULT UserAgent([ out, retval ] LPCWSTR * userAgent);
88+ /// Sets the `UserAgentString` property. This property may be overriden if
89+ /// the User-Agent header is set in a request.
90+ [propput] HRESULT UserAgent([in] LPCWSTR userAgent);
91+ }
92+ ```
93+ ## .NET and WinRT
8994
90- ```c #namespace Microsoft.Web.WebView2.Core {
95+ ``` c#
96+ namespace Microsoft .Web .WebView2 .Core {
97+ public
98+ partial class CoreWebView2 {
99+ // There are other API in this interface that we are not showing
91100 public
92- partial class CoreWebView2 {
93- // There are other API in this interface that we are not showing
94- public
95- CoreWebView2Settings UserAgent {
96- get;
97- set;
98- };
99- }
101+ CoreWebView2Settings UserAgent {
102+ get ;
103+ set ;
104+ };
100105 }
101- ```
106+ }
107+ ```
0 commit comments