I am currently involved in a project that involves reading user input via voice and displaying it on a website. I am able to read all input IDs on the site using Jsoup.
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.clearCache(true);
webView.clearHistory();
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
new ParseURl().execute(new String[]{url});
}
});
webView.loadUrl(passed_site);
The method to read all input IDs is working perfectly fine.
private class ParseURl extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Elements inputs = doc.select("input[type=text]");
for (Element ele : inputs) {
listID.add(ele.attr("id"));
}
} catch (IOException e) {
e.printStackTrace();
}
if(listID.size()>0){
return true;
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result){
readMyVoice();
}else {
Toast.makeText(WebPage.this , "your site dont have input to fill" , Toast.LENGTH_LONG).show();
}
}
}
If the site contains an input field, it will start writing to it by taking user input.
private void readMyVoice() {
Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voicerecogize.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
startActivityForResult(voicerecogize, RESULT_SPEECH);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_SPEECH && requestCode == RESULT_OK) ;
{
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (results.size() > 0 ) {
StartFillSiteForm(listID.get(count),results.get(0));
count++;
if(listID.size() == count){
return;
}else{
readMyVoice();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
However, there is an issue. Everything works fine until the method tries to write the user input to the website, at which point it opens a new empty page instead of filling in the input field as expected.
private void StartFillSiteForm(String id , String value){
webView.loadUrl("javascript: document.getElementById('"+id+"').value='" + value + "';");
}
The tested site is Google.