Skip to content

Commit cab754f

Browse files
Merge pull request #638 from MicrosoftEdge/api-uastring
api review fixes
2 parents 8128531 + dbfac03 commit cab754f

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

specs/APIReview_UAString.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ However, there are a couple limitations to this workaround-- it is not an API th
1010

1111
# Description
1212

13-
The User Agent (UA) is a client-side piece of information regarding the user's OS, application, and version. The browser/webcontrol sends the User Agent to the HTTP server.
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.
1414

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, such as navigating to a specific website and setting the User Agent to emulate a different browser version.
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.
1618

1719
# Examples
1820

@@ -25,7 +27,9 @@ m_webView->add_NavigationStarting(
2527
Callback<ICoreWebView2NavigationStartingEventHandler>(
2628
[this](ICoreWebView2 *sender,
2729
ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT {
28-
static const PCWSTR url_compare_example = L"foo.org";
30+
static const PCWSTR url_compare_example = L"fourthcoffee.com";
31+
wil::unique_cotaskmem_string uri;
32+
CHECK_FAILURE(args->get_Uri(&uri));
2933
wil::unique_bstr domain = GetDomainOfUri(uri.get());
3034
const wchar_t *domains = domain.get();
3135
wil::com_ptr<ICoreWebView2Settings> settings;
@@ -47,9 +51,17 @@ m_webView->add_NavigationStarting(
4751
## .NET and WinRT
4852
4953
```c #
50-
private void SetUserAgent(CoreWebView2 sender, CoreWebView2UserAgentArgs e) {
54+
webView2Control.NavigationStarting += SetUserAgent;
55+
56+
private void SetUserAgent(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs e)
57+
{
5158
var settings = webView2Control.CoreWebView2.Settings;
52-
settings.UserAgent = GetUserAgent();
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+
}
5365
}
5466
```
5567

@@ -62,15 +74,15 @@ See [API Details](#api-details) section below for API reference.
6274
## Win32 C++
6375

6476
```IDL
65-
// This is the ICoreWebView2Settings Staging interface.
66-
[uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
67-
interface ICoreWebView2StagingSettings : IUnknown {
77+
// This is the ICoreWebView2Settings interface.
78+
[uuid(684cbeef-47ba-4d4a-99f4-976113f9f10a), object, pointer_default(unique)]
79+
interface ICoreWebView2Settings2 : ICoreWebView2Settings {
6880
/// `UserAgent` . Returns the User Agent. The default value is the
6981
/// default User Agent of the Edge browser.
7082
[propget] HRESULT UserAgent([ out, retval ] LPWSTR * userAgent);
7183
/// Sets the `UserAgentString` property. This property may be overriden if
72-
/// the User-Agent header is set in a request. If the parameter is empty
73-
/// the User Agent will not be updated and the current User Agent will remain.
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.
7486
[propput] HRESULT UserAgent([in] LPCWSTR userAgent);
7587
}
7688
```

0 commit comments

Comments
 (0)