forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScenarioDOMContentLoaded.cpp
66 lines (58 loc) · 2.51 KB
/
ScenarioDOMContentLoaded.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ScenarioDOMContentLoaded.h"
#include "AppWindow.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioDOMContentLoaded.html";
ScenarioDOMContentLoaded::ScenarioDOMContentLoaded(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
//! [DOMContentLoaded]
// Register a handler for the DOMContentLoaded event.
// Check whether the DOM content loaded
CHECK_FAILURE(m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webView2)));
CHECK_FAILURE(m_webView2->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]
// Turn off this scenario if we navigate away from the sample page
CHECK_FAILURE(m_webView->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](
ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioDOMContentLoaded::~ScenarioDOMContentLoaded()
{
m_webView2->remove_DOMContentLoaded(m_DOMContentLoadedToken);
m_webView->remove_ContentLoading(m_contentLoadingToken);
}