With this code, I can easily paste username and password automatically on facebook.com
The purpose is to automatically paste username and password for each site chosen by the user. Many apps do it, but I have not found the way. Thanks for your help
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
String user = "user";
String pwd = "pass";
view.loadUrl("javascript:(function(){document.getElementsByName('email')[0].value='"
+ user
+ "';document.getElementsByName('pass')[0].value='"
+ pwd + "';document.getElementsByTagName('form')[0];})()");
}
Like Speditz said not every login page uses the same name
/ id
element.
In general, most of the websites either use type="email"
or type="text"
for username, type="password"
for password to login, and you can do some field existence checking and perform injection after webpage loaded
view.loadUrl(
"javascript:window.onload= (function(){"
+ "var selectElementName = document.querySelector('input[type=\"email\"]');"
+"if(selectElementName){selectElementName.value = \"" + user + "\";}"
+"var selectElementName = document.querySelector('input[type=\"text\"]');"
+"if(selectElementName){selectElementName.value = \"" + user + "\";}"
+"var selectElementpassword = document.querySelector('input[type=\"password\"]');"
+"if(selectElementpassword){selectElementpassword.value = \"" + pwd + "\";}"
+"})();"
);
And this is above JavaScript in plain text for easier to understand:
window.onload= function(){
var selectElementName = document.querySelector('input[type="email"]');
if(selectElementName){selectElementName.value = "username";}
var selectElementName = document.querySelector('input[type="text"]');
if(selectElementName){selectElementName.value = "username";}
var selectElementpassword = document.querySelector('input[type="password"]');
if(selectElementpassword){selectElementpassword.value = "password";}
};