Skip to content

Commit 1582e61

Browse files
authored
Merge pull request #516 from MicrosoftEdge/api-dom-contentloaded
DOMContentLoaded API review spec
2 parents 324cacd + 6fba057 commit 1582e61

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

specs/DOMContentLoaded.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Background
2+
3+
In response to consumers' requests for an event similar to the old [WebView DOMContentLoaded](https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview#mswebviewdomcontentloaded), the WebView2 team has introduced DOMContentLoaded API which indicates that the main DOM elements have finished loading.
4+
In this document we describe the new API. We'd appreciate your feedback.
5+
6+
# Description
7+
We propose adding DOMContentLoaded to CoreWebView2. This allows the developer to have an event that fires when the DOM is loaded after the WebView2 navigates to a page.
8+
9+
# Examples
10+
## Win32 C++
11+
```
12+
ScenarioDOMContentLoaded::ScenarioDOMContentLoaded(SampleAppWindow* sampleAppWindow)
13+
: m_sampleAppWindow(sampleAppWindow), m_webView(sampleAppWindow->GetWebView())
14+
{
15+
//! [DOMContentLoaded]
16+
// Register a handler for the DOMContentLoaded event.
17+
// Event is raised when the DOM content is loaded
18+
CHECK_FAILURE(m_webView->add_DOMContentLoaded(
19+
Callback<ICoreWebView2DOMContentLoadedEventHandler>(
20+
[this](ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args)
21+
-> HRESULT {
22+
m_webView->ExecuteScript(
23+
LR"~(
24+
let content=document.createElement("h2");
25+
content.style.color='blue';
26+
content.textContent="This text was added by the host app";
27+
document.body.appendChild(content);
28+
)~",
29+
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
30+
[](HRESULT error, PCWSTR result) -> HRESULT { return S_OK; })
31+
.Get());
32+
return S_OK;
33+
})
34+
.Get(),
35+
&m_DOMContentLoadedToken));
36+
//! [DOMContentLoaded]
37+
```
38+
39+
## C#
40+
```
41+
webView.CoreWebView2.DOMContentLoaded += (object sender, CoreWebView2DOMContentLoadedEventArgs arg) =>
42+
{
43+
_ = webView.ExecuteScriptAsync("let " +
44+
"content=document.createElement(\"h2\");content.style.color=" +
45+
"'blue';content.textContent= \"This text was added by the " +
46+
"host app\";document.body.appendChild(content);");
47+
};
48+
webView.NavigateToString(@"<!DOCTYPE html><h1>DOMContentLoaded sample page</h1><h2>The content below will be added after DOM content is loaded </h2>");
49+
50+
```
51+
52+
# API Notes
53+
54+
See [API Details](#api-details) section below for API reference.
55+
# API Details
56+
57+
## Win32 C++
58+
59+
```IDL
60+
interface ICoreWebView2_2;
61+
interface ICoreWebView2DOMContentLoadedEventArgs;
62+
interface ICoreWebView2DOMContentLoadedEventHandler;
63+
64+
[uuid(9810c82b-8483-4f1c-b2f4-6244f1010c05), object, pointer_default(unique)]
65+
interface ICoreWebView2_2 : ICoreWebView2 {
66+
/// Add an event handler for the DOMContentLoaded event.
67+
/// DOMContentLoaded is raised when the initial html document has been parsed.
68+
/// This aligns with the the document's DOMContentLoaded event in html
69+
///
70+
/// \snippet ScenarioDOMContentLoaded-Staging.cpp
71+
HRESULT add_DOMContentLoaded(
72+
[in] ICoreWebView2StagingDOMContentLoadedEventHandler* eventHandler,
73+
[out] EventRegistrationToken* token);
74+
/// Remove an event handler previously added with add_DOMContentLoaded.
75+
HRESULT remove_DOMContentLoaded(
76+
[in] EventRegistrationToken token);
77+
}
78+
79+
/// Event args for the DOMContentLoaded event.
80+
[uuid(E8BA4206-D6F8-42F1-9A6D-43C8A99C1F39), object, pointer_default(unique)]
81+
interface ICoreWebView2DOMContentLoadedEventArgs : IUnknown {
82+
/// The ID of the navigation which corresponds to other navigation ID properties on other navigation events.
83+
[propget] HRESULT NavigationId([out, retval] UINT64* navigationId);
84+
}
85+
86+
/// The caller implements this interface to receive the DOMContentLoaded
87+
/// event.
88+
[uuid(1E649181-785D-40B2-B4AE-AFACD3C6B8DD), object, pointer_default(unique)]
89+
interface ICoreWebView2DOMContentLoadedEventHandler : IUnknown {
90+
/// Called to provide the implementer with the event args for the
91+
/// corresponding event.
92+
HRESULT Invoke(
93+
[in] ICoreWebView2* sender,
94+
[in] ICoreWebView2DOMContentLoadedEventArgs* args);
95+
}
96+
```
97+
98+
## .NET and WinRT
99+
100+
```c#
101+
namespace Microsoft.Web.WebView2.Core
102+
{
103+
runtimeclass CoreWebView2DOMContentLoadedEventArgs;
104+
105+
runtimeclass CoreWebView2DOMContentLoadedEventArgs
106+
{
107+
// CoreWebView2DOMContentLoadedEventArgs
108+
UInt64 NavigationId { get; };
109+
}
110+
111+
runtimeclass CoreWebView2
112+
{
113+
// CoreWebView2
114+
event Windows.Foundation.TypedEventHandler<CoreWebView2, CoreWebView2DOMContentLoadedEventArgs> DOMContentLoaded;
115+
}
116+
117+
118+
}
119+
```

0 commit comments

Comments
 (0)