0

We have an app that uses a form for oauth, just for dev purposes I want to eliminate typing in the user name and password so I added some code like this for when the page finishes loading:

mWebView.loadUrl("javascript:document.getElementById('UserName').value='" + txtUser.getText()+"'");

But for some reason it doesn't fill in the form it makes a new page and just writes out the value of txtUser instead of filling in the input field? Why and how can I fix this?

arinte
  • 3,660
  • 10
  • 45
  • 65

2 Answers2

2

If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:

mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");
Bobby Hargett
  • 481
  • 3
  • 10
0

I faced a similar issue and since I couldn't find a proper solution, I used the following hack -

  1. Fetch the html source text.
  2. Replace </body> tag with </body><script>YOUR JS CODE</script>
  3. webview.loadDataWithBaseURL(url, editedString, "text/html", "UTF-8", null);

Sorry for the hacky solution, but in case you can't find a solution, this could help :)

Tushar Nallan
  • 784
  • 6
  • 16