Because of VB WebBrowser limitation I use now CefSharp, but got a problem. I'm not familiar with JavaScript language, but have to write code with it. Target is fill form and click "add" button, but to fill form I use variables from parsed CSV. How I can inject these variables into script string. Any ideas?
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim csv As String = OpenFileDialog1.FileName
Dim laskut As String() = IO.File.ReadAllLines(csv).ToArray
For Each line In laskut
Dim Fields = line.Split(";"c)
Dim hinta As String = Fields(10)
Dim maksaja As String = Fields(5)
Dim laitos As String = Fields(6)
Dim asiakas As String = Fields(4)
Dim osoite As String = Fields(7)
Dim index As String = Fields(8)
Dim city As String = Fields(9)
Dim postal As String = index & " " & city
Dim day As Date = DateTime.ParseExact(Fields(1).Replace("""", ""), "dd.MM.yyyy HH.mm.ss", Globalization.CultureInfo.InvariantCulture)
Dim pvm = day.ToString("dd.MM.yyyy")
Dim js As String = <js><![CDATA[document.getElementById("w_invoice_t_new_inv_name1").value = asiakas;
document.getElementById("w_invoice_t_new_inv_street_address1").value = osoite;
document.getElementById("w_invoice_t_new_inv_postal_address").value = postal;
document.getElementsById("addProductRow")[0].click();]]></js>.Value
browser.GetMainFrame.ExecuteJavaScriptAsync(js)
Next
End Sub
You can insert variable values into XML literals with <%= someVariable %>
- as long as it isn't in a CDATA section, from my brief testing. CDATA hasn't been need to hide JavaScript for a long time.
Dim asiakas = "hello"
Dim osoite = "there"
Dim postal = "world"
Dim js = <js>
document.getElementById("w_invoice_t_new_inv_name1").value = '<%= asiakas %>';
document.getElementById("w_invoice_t_new_inv_street_address1").value = '<%= osoite %>';
document.getElementById("w_invoice_t_new_inv_postal_address").value = '<%= postal %>';
document.getElementsById("addProductRow")[0].click();
</js>.Value
Console.WriteLine(js)
outputs:
document.getElementById("w_invoice_t_new_inv_name1").value = 'hello';
document.getElementById("w_invoice_t_new_inv_street_address1").value = 'there';
document.getElementById("w_invoice_t_new_inv_postal_address").value = 'world';
document.getElementsById("addProductRow")[0].click();
You probably want to put quotes in there (as shown, or double-quotes if preferred) to make them literal strings in the JavaScript.
Documentation: XML Literals Overview