Issue
I'm creating some code which saves form data from any website and the fills it in when the user requests.
I am using java with webView to load a web page and inject JavaScript to perform the operation.
Everything works as expected, but in some webpages, after the inputs have been filled, the data disappears when selecting another input field.
private void injectScriptInputValue(){
//Current web address
String address = myWebView.getUrl();
ArrayList<PayInputs> payInputs = new ArrayList<>();
//Check for data which matches the current web address.
for (int i = 0; i < payPages.size(); i++) {
//Stored web address.
String payPageAddy = payPages.get(i).getAddress();
if (payPageAddy.compareTo(address) == 0) {
payInputs = payPages.get(i).getPayInputs();
break;
}
}
StringBuilder javaScriptInputs = new StringBuilder();
//Loop through the saved inputs and create javaScript code.
for (int i = 0; i < payInputs.size(); i++) {
String id = payInputs.get(i).getId();
String value = payInputs.get(i).getValue();
//javaScriptInputs.append("document.getElementById('").append(id).append("').focus()").append(";");
//javaScriptInputs.append("document.getElementById('").append(id).append("').select()").append(";");
javaScriptInputs.append("document.getElementById('").append(id).append("').value = '").append(value).append("';");
}
StringBuilder sb = new StringBuilder();
sb.append("(function()");
sb.append("{");
sb.append(javaScriptInputs);
sb.append("}");
sb.append(")");
sb.append("()");
myWebView.loadUrl("javascript:" + sb.toString());
}
Is there something that could be causing this on the website and is there a way to overcome this issue? Is the input checking the data is formatted in a certain way and can this be overridden?
Solution
I managed to solve this issue by selecting the input first then adding the value then deselecting.
//Loop through the saved inputs and create javaScript code.
for (int i = 0; i < payInputs.size(); i++) {
String id = payInputs.get(i).getId();
String value = payInputs.get(i).getValue();
javaScriptInputs.append("document.getElementById('").append(id).append("').select()").append(";");
javaScriptInputs.append("document.getElementById('").append(id).append("').value = '").append(value).append("';");
javaScriptInputs.append("document.getElementById('").append(id).append("').blur()").append(";");
}
Answered By - Mazz
Answer Checked By - Willingham (JavaFixing Volunteer)