User Input (to function parameters)


#1

Does anyone know of a way to include variables from out side the JS text to be run against a website. The above code has helped me get the information I want from a webpage but I would like to be able to set some parameters for my function dynamically from user input. Is this possible?


How are you interacting with the WebView?
#2

Here’s an example of getting user input.

let nameInput = new Alert();
nameInput.title = "Input Example";
nameInput.message = "Please enter your details";
nameInput.addTextField("Forename");
nameInput.addTextField("Surname");
nameInput.addAction("OK");
await nameInput.present();
console.log("Hello " + nameInput.textFieldValue(0) + " " + nameInput.textFieldValue(1));

#3

I should have been more specific. I have have some JS I have wrapped in let v1 = function editDa(p1, p2, p3){.... in order to run the evaluateJavaScript method using v1 as a parameter on a local html file loaded into a webview. The JS evaluates properly but I want a way to pass user input into the function that is wrapped in ``. Can I use string interpolation? I am at a loss of how to pass my data into a function that is now considered a string by JS.


#4

You can concatenate your string with the needed values inbetween or use ${expression} inside the template literal ``. But keep in mind that objects would need to be passed through JSON.stringify() beforehand.


#5

Thank you this helped me a lot. I ended up taking input from a shortcut because the interface looked better but passing that data into JSON.stringify() and then putting the variables in my function string as function myfunction(${p1} ) worked great. Thank you.