How to interact with DOM on a UWP WebView?

回眸只為那壹抹淺笑 提交于 2019-12-13 07:27:57

问题


I have a UWP XAML Page and a WebView

<WebView Name="webview1" Margin="10"
             Source="http://www.apelosurgentes.com.br/en-us/" 
             LoadCompleted="WebView_LoadCompleted" />

How to read and manipulate the DOM of the document that was loaded on this WebView?


回答1:


How to interact with DOM on a UWP WebView?

You can use InvokeScriptAsync with the JavaScript eval function to use the HTML event handlers, and to use window.external.notify from the HTML event handler to notify the application using WebView.ScriptNotify.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    string functionString = String.Format("document.getElementById('nameDiv').innerText = 'Hello, {0}';", nameTextBox.Text);
    await webView1.InvokeScriptAsync("eval", new string[] { functionString });
}

Scripts in the web view content can use window.external.notify with a string parameter to send information back to your app. To receive these messages, handle the ScriptNotify event.

public MyPage()
{
    this.InitializeComponent();
    MyWebView.ScriptNotify += MyWebView_ScriptNotify;

    // Here we have to set the AllowedScriptNotifyUri property because we are 
    // navigating to some site where we don't own the content and we want to 
    // allow window.external.notify() to pass data back to the app.
    List<Uri> allowedUris = new List<Uri>();
    allowedUris.Add(new Uri("http://www.bing.com"));
    MyWebView.AllowedScriptNotifyUris = allowedUris;
}

void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
    // Respond to the script notification.
}

For more, Please refer to UWP WebView.



来源:https://stackoverflow.com/questions/46961670/how-to-interact-with-dom-on-a-uwp-webview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!