### Use WebEngine executeScript with Maps

To execute JavaScript methods within a WebView, a list of maps in conjuction with a Sub can be used to execute.
Define the Globals
`Sub Process_Globals
Private lvwJSActions As ListView
Private wvwMain As WebView
...
End Sub`
Define the List of Maps with the JavaScript Methods
' Build the list of commands as defined in a map
lstMethods.Initialize
lstMethods.Add(CreateMap("title": "Home", "method": "window.open('http://www.google.com')", "show": False))
Dim s As String = "'file:///" & File.DirApp & "\webviewtest.html'"
s = s.Replace("\", "/") ' If using local files ensure to replace / by \
lstMethods.Add(CreateMap("title": "File", "method": "window.open(" & s & ")", "show": False))
lstMethods.Add(CreateMap("title": "Back", "method": "history.back()", "show": False))
lstMethods.Add(CreateMap("title": "Forward", "method": "history.forward()", "show": False))
lstMethods.Add(CreateMap("title": "Location", "method": "location.href", "show": True))
lstMethods.Add(CreateMap("title": "Home", "method": "window.alert('Moin Moin')", "show": False))
' Populate the list view with title key from the list of maps
For i = 0 To lstMethods.Size - 1
Dim m As Map = lstMethods.Get(i)
lvwJSActions.Items.Add(m.Get("title"))
Next
' Select the first entry which will result in loading the page
lvwJSActions.SelectedIndex = 0
...
Define the Sub to execute the JavaScript Method
'Execute JavaScript method: like
' history.back(), history.forward(), history.go(+/-n), history.length, location.href
' Parameter: WebView, JavaScript function, Show result in a Message Window
' Returns an Object which can be used further if required
`Sub runJSMethod(wv As WebView, cmd As String, show As Boolean) As Object
If cmd.Length = 0 Then Return Null
Try
Dim joWV As JavaObject = wv
Dim result As Object = joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(cmd))
If show = True Then gMsg.Show("Message", result)
Return result
Catch
Log(LastException.Message)
lblInformation.Text = LastException.Message
Return Null
End Try
End Sub`
Execute a JavaScript
'Execute a JavaScript as defined in the list of maps
`Sub lvwJSActions_SelectedIndexChanged(Index As Int)
If Index = -1 Then Return
Dim m As Map = lstMethods.Get(Index)
runJSMethod(wvwMain, m.Get("method"), m.Get("show"))
lblInformation.Text = "Action: " & lvwJSActions.Items.get(lvwJSActions.SelectedIndex)
End Sub`

Changed: , Category: WebView, File: T6C284M2141.htm, ID: 2141