的WebView没有内置的事件,支持检测的内容发生变化。您可以使用带有JavaScript eval函数的InvokeScriptAsync以使用HTML事件处理程序,并使用HTML事件处理程序中的window.external.notify通过WebView.ScriptNotify通知应用程序。
关于如何检测由JavaScript改变HTML内容,你可以参考DOM mutation events replacement
MDN->Web technology for developers->Web APIs->MutationObserver
我做了一个简单的代码示例为您提供:
<WebView Source="ms-appx-web:///HTMLPage1.html" ScriptNotify="WebView_ScriptNotify"></WebView>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function detectContent()
{
var target = document.getElementById('div');
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
window.external.notify('div changed');
update("div changed");
});
});
var config = { attributes: true, childList: true, characterData: true };
observer.observe(target, config);
}
function update(e)
{
var div = document.getElementById("div");
div.innerHTML = e;
}
</script>
</head>
<body onload="detectContent();">
<div id="div" style="margin-top:100px;"></div>
<br />
<input value="update" onclick="update('abc');" type="button"/>
</body>
</html>
private void WebView_ScriptNotify(object sender, NotifyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Value);
}
然后如果Div的内容被改变,WebView的ScriptNotify事件将被触发。在我的代码示例中,Webview只是加载本地html页面。
就你而言,如果网站不属于你,你需要使用InvokeScriptAsync在WebView navigatstarting事件中注入javascript代码。