WebView.InvokeScriptAsync code examples

ansalc 436 Reputation points
2020-04-22T16:57:01.327+00:00

Can someone provide some code examples for using WebView.InvokeScriptAsync

I am successfully using simple code like

            Dim scriptString As String = "document.getElementsByName('submit')[0].click()"
            Await WebView1.InvokeScriptAsync("eval", {scriptString})

However, I would like to be able to write more sofisticated code like the one below that I used in my old VB projects using Internet Explorer referencing the Microsoft HTML Object Library:

Public WithEvents Wbe As New SHDocVw.InternetExplorer

HTMLDoc = Wbe.Document

    iHTMLCol = HTMLDoc.getElementsByTagName("input")

    For Each iHTMLEle In iHTMLCol
        If Not iHTMLEle.getAttribute("name") Is Nothing Then
            If iHTMLEle.getAttribute("name").ToString = "sort" Then
                iHTMLEle.setAttribute("value", "1234")
                Exit For
            End If
        End If
    Next

I guess it is just a matter of the writing what I call above the scriptString. But some actual code examples will be helpful

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Fay Wang - MSFT 5,196 Reputation points
    2020-04-23T02:57:03.96+00:00

    Hello,

    Welcome to Microsoft Q&A!

    There are two main uses about webview.InvokeScriptAsync(), the first is to call a function that already exists in the HTML, and could pass the parameters or with the JavaScript eval function to inject content into the web page. If you want to achieve the function about the sofisticated code like the one above that you used in your old VB projects, you need to convert the code snippet from VB to the JavaScript and then put the JavaScript statements into webview.InvokeScriptAsync() method.

    About how to interact with the content of the WebView by using the InvokeScriptAsync method, you can refer to this document which contains some code samples.