Background
I am pretty new to using the webview2 controller but have found that you can run javascript to manipulate a webpage, click buttons, fill in forms, etc. which has been very helpful. Currently when I run javascript I use this format. In this case I made a function that finds an element and then clicks a child element. This will expand the element in a table:
Private Async Function Fully_Expand_Part(ByVal Part_name As String) As Task
Dim script = "var element = document.getElementById('TreeTable');
find_children(element);
function find_children(parent){
var children = parent.children;
for (let i = 0; i < children.length; i++) {
var child = children[i];
if(element.className !== ''){
if(child !== null){
if(child.innerText !== null){
if(child.innerText !== undefined){
if(child.innerText.toString().includes('" & Part_name & "')==true){
if(child.className =='table-cellText'){
child.click();
document.getElementById('ExpandElement').click();
}
}
}
}
}
}
find_children(child);
}
}
"
Dim WH As String = Await WebView21.CoreWebView2.ExecuteScriptAsync(script)
End Function
Problem
Now the above function works great at doing a task on a page but I am now trying to learn how to return values based on either: logic or webpage data. In this example, I have written it similarly to the last but want to know if an element is expanded or not (return a true or false):
Private Async Function Is_Part_Expanded(ByVal Part_name As String) As Task
Dim script = "var element = document.getElementById('TreeTable');
find_children(element, false);
function find_children(parent,allow_expand){
var children = parent.children;
for (let i = 0; i < children.length; i++) {
var child = children[i];
if(allow_expand == false){
if(element.className !== ''){
if(child !== null){
if(child.innerText !== null){
if(child.innerText !== undefined){
if(child.innerText.toString().includes('" & Part_name & "')==true){
if(child.className =='table-cellText'){
allow_expand = true;
child = child.parentElement.parentElement;
}
}
}
}
}
}
}
if(allow_expand == true){
if(child.id == 'miscCollapsedTree'){
return 'Collapsed';//~~~~~~~~~~~~~~~~~~~~~~~~~This is what I want to return to VB .NET!!!
}
}
find_children(child,allow_expand);
}
}
"
Dim WH As String = Await WebView21.CoreWebView2.ExecuteScriptAsync(script)
MsgBox(WH)
End Function
So based on the logic I would like the function to return the value "Collapsed" if it is collapsed and then "Expanded" if not found. Currently the returned value to the Msgbox is just undefined. How should I pass a value through to the VB .Net side?