-
Notifications
You must be signed in to change notification settings - Fork 64
DOMContentLoaded API review spec #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a7ba9d
DOMContentLoaded API review spec - draft
tofuandeve 84f712a
Merge pull request #504 from MicrosoftEdge/api-dom-contentloaded-draft
tofuandeve 8ef97a0
Address comments on DOMContentLoaded API spec
tofuandeve 18c2a31
Address reviews on DOMContentLoaded API spec
tofuandeve 6fba057
Change AppWindow to SampleAppWindow
tofuandeve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Background | ||
|
|
||
| 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. | ||
| In this document we describe the new API. We'd appreciate your feedback. | ||
|
|
||
| # Description | ||
| 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. | ||
|
|
||
| # Examples | ||
| ## Win32 C++ | ||
| ``` | ||
| ScenarioDOMContentLoaded::ScenarioDOMContentLoaded(SampleAppWindow* sampleAppWindow) | ||
| : m_sampleAppWindow(sampleAppWindow), m_webView(sampleAppWindow->GetWebView()) | ||
| { | ||
| //! [DOMContentLoaded] | ||
| // Register a handler for the DOMContentLoaded event. | ||
| // Event is raised when the DOM content is loaded | ||
| CHECK_FAILURE(m_webView->add_DOMContentLoaded( | ||
| Callback<ICoreWebView2DOMContentLoadedEventHandler>( | ||
| [this](ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args) | ||
| -> HRESULT { | ||
| m_webView->ExecuteScript( | ||
| LR"~( | ||
| let content=document.createElement("h2"); | ||
| content.style.color='blue'; | ||
| content.textContent="This text was added by the host app"; | ||
| document.body.appendChild(content); | ||
| )~", | ||
| Callback<ICoreWebView2ExecuteScriptCompletedHandler>( | ||
| [](HRESULT error, PCWSTR result) -> HRESULT { return S_OK; }) | ||
| .Get()); | ||
| return S_OK; | ||
| }) | ||
| .Get(), | ||
| &m_DOMContentLoadedToken)); | ||
| //! [DOMContentLoaded] | ||
| ``` | ||
|
|
||
| ## C# | ||
| ``` | ||
| webView.CoreWebView2.DOMContentLoaded += (object sender, CoreWebView2DOMContentLoadedEventArgs arg) => | ||
| { | ||
| _ = webView.ExecuteScriptAsync("let " + | ||
| "content=document.createElement(\"h2\");content.style.color=" + | ||
| "'blue';content.textContent= \"This text was added by the " + | ||
| "host app\";document.body.appendChild(content);"); | ||
| }; | ||
| webView.NavigateToString(@"<!DOCTYPE html><h1>DOMContentLoaded sample page</h1><h2>The content below will be added after DOM content is loaded </h2>"); | ||
|
|
||
| ``` | ||
|
|
||
| # API Notes | ||
|
|
||
| See [API Details](#api-details) section below for API reference. | ||
| # API Details | ||
|
|
||
| ## Win32 C++ | ||
|
|
||
| ```IDL | ||
| interface ICoreWebView2_2; | ||
| interface ICoreWebView2DOMContentLoadedEventArgs; | ||
| interface ICoreWebView2DOMContentLoadedEventHandler; | ||
|
|
||
| [uuid(9810c82b-8483-4f1c-b2f4-6244f1010c05), object, pointer_default(unique)] | ||
| interface ICoreWebView2_2 : ICoreWebView2 { | ||
|
tofuandeve marked this conversation as resolved.
|
||
| /// Add an event handler for the DOMContentLoaded event. | ||
| /// DOMContentLoaded is raised when the initial html document has been parsed. | ||
| /// This aligns with the the document's DOMContentLoaded event in html | ||
| /// | ||
| /// \snippet ScenarioDOMContentLoaded-Staging.cpp | ||
| HRESULT add_DOMContentLoaded( | ||
| [in] ICoreWebView2StagingDOMContentLoadedEventHandler* eventHandler, | ||
| [out] EventRegistrationToken* token); | ||
| /// Remove an event handler previously added with add_DOMContentLoaded. | ||
| HRESULT remove_DOMContentLoaded( | ||
| [in] EventRegistrationToken token); | ||
| } | ||
|
|
||
| /// Event args for the DOMContentLoaded event. | ||
| [uuid(E8BA4206-D6F8-42F1-9A6D-43C8A99C1F39), object, pointer_default(unique)] | ||
| interface ICoreWebView2DOMContentLoadedEventArgs : IUnknown { | ||
| /// The ID of the navigation which corresponds to other navigation ID properties on other navigation events. | ||
| [propget] HRESULT NavigationId([out, retval] UINT64* navigationId); | ||
| } | ||
|
|
||
| /// The caller implements this interface to receive the DOMContentLoaded | ||
| /// event. | ||
| [uuid(1E649181-785D-40B2-B4AE-AFACD3C6B8DD), object, pointer_default(unique)] | ||
| interface ICoreWebView2DOMContentLoadedEventHandler : IUnknown { | ||
| /// Called to provide the implementer with the event args for the | ||
| /// corresponding event. | ||
| HRESULT Invoke( | ||
| [in] ICoreWebView2* sender, | ||
| [in] ICoreWebView2DOMContentLoadedEventArgs* args); | ||
| } | ||
| ``` | ||
|
|
||
| ## .NET and WinRT | ||
|
|
||
| ```c# | ||
| namespace Microsoft.Web.WebView2.Core | ||
| { | ||
| runtimeclass CoreWebView2DOMContentLoadedEventArgs; | ||
|
|
||
| runtimeclass CoreWebView2DOMContentLoadedEventArgs | ||
| { | ||
| // CoreWebView2DOMContentLoadedEventArgs | ||
| UInt64 NavigationId { get; }; | ||
| } | ||
|
|
||
| runtimeclass CoreWebView2 | ||
|
tofuandeve marked this conversation as resolved.
|
||
| { | ||
| // CoreWebView2 | ||
| event Windows.Foundation.TypedEventHandler<CoreWebView2, CoreWebView2DOMContentLoadedEventArgs> DOMContentLoaded; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.