I have a simple javascript where I want to give value to these input which is username and password. I open the website by using webview. When the webview fully loaded, the username and password place doesn't change at all.
The website : http://www.malaysiakini.com/login/en/form
My source code for the javascript :
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript: {" +
"$('input#username').val(" + user + ");\n" +
"$('input#username').val(\"password\");\n" +
"document.querySelectorAll(\"button[type='submit']\")[0].click();}");
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript: {" +
"$('input#username').val('" + user + "');\n" +
"$('input#username').val(\"password\");\n" +
"document.querySelectorAll(\"button[type='submit']\")[0].click();}");
}
Silly me, I missed the single quotes
Related
I am using a WebView to display a website into my application.
When I'm loading my WebView, I first need to fill the authentification form and then redirect to the original URL.
My WebView keep reloading again and again.
Could anyone explain to me how to use a WebView with "automatic" filling please?
final WebView mWebView;
//Show webview into the app via a popup
AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(context);
mAlertDialog.setTitle(R.string.menuBuyCredit);
mWebView = new WebView(context);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(ConstantsClass.URL_BUY_CREDIT);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//In order to automatically fill the form
final String mJavaScript = "javascript:" +
"document.getElementById('Login').value = '" + ClientSingleton.getInstance().getmLogin() + "';" +
"document.getElementById('Password').value = '" + ClientSingleton.getInstance().getmPassword() + "';" +
"document.getElementById('Btn_Envoyer').click()";
view.loadUrl(mJavaScript);
view.loadUrl(ConstantsClass.URL_BUY_CREDIT);
view.pageDown(true);
}
});
mAlertDialog.setView(mWebView);
mAlertDialog.setNegativeButton(R.string.closeDrawer, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mAlertDialog.show();
} ```
You should use something like
Put this in global
boolean loadedAlready = false;
inside onPageFinished method
if (view.getUrl().equals(ConstantsClass. URL_BUY_CREDIT) && !loadedAlready) {
loadedAlready= true;
view.load(ConstantsClass.URL_BUY_CREDIT);
}
This is my code for handling login:
wblogin.getSettings().setJavaScriptEnabled(true);
wblogin.loadUrl("http://mywebsite.com/login.php");
wblogin.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
wblogin.loadUrl("javascript: {" +
"document.getElementById('email').value = '" + user_name + "';" +
"document.getElementById('password').value = '" + pass_name + "';" +
"document.getElementById('xyz').click();" +
"};");
}
});
When I logout from the website running in Webview it starts to login automatically
Have you turned on a setting like "Remember my login" for the login page? Maybe it is a default checkbox set?
Second idea: Maybe the auto login after logout happens because of cookies set. Find information how to disable cookies in a webview here and here
There is no really pretty and short way in doing so.
The problem is that everytime you load a new page you run the code for logging in on every single page.
To work around that problem you should save wether you already logged into the application, simplest approach is as follows:
wblogin.setWebViewClient(new WebViewClient() {
private boolean shallLogin = true;
#Override
public void onPageFinished(WebView view, String url) {
if (shallLogin == true) {
wblogin.loadUrl("javascript: {" +
"document.getElementById('email').value = '" + user_name + "';" +
"document.getElementById('password').value = '" + pass_name + "';" +
"document.getElementById('xyz').click();" +
"};");
shallLogin = false;
}
}
});
As the login might fail or the user might move around without being logged in a more proper way would be to register a JS Callback and on page reload check if the login were successful by finding an element by ID which is only visible when logged in like the userimage or something.
...
#Override
public void onPageFinished(WebView view, String url) {
wblogin.loadUrl("javascript: {" +
"window.ANDROID_CALLBACK.login(document.getElementsByClassName('.my-user-img').length > 0);"
"};");
}
...
class LoadListener {
public LoadListener (MyWebViewClient myWebViewClient) {
}
public void login(boolean loginWorked) {
wblogin.shallLogin = !loginWorked;
}
}
...
wblogin.addJavascriptInterface(new LoadListener(wblogin), "ANDROID_CALLBACK")
I have a webview with following settings :
mView.getSettings().setLoadWithOverviewMode(true);
mView.getSettings().setUseWideViewPort(false);
mView.getSettings().setJavaScriptEnabled(true);
mView.getSettings().setAllowContentAccess(true);
mView.getSettings().setDomStorageEnabled(true);
After loading a webpage, when I try to execute Javascript:
mView.loadUrl("javascript:document.querySelector('input[type=password]').value ='" + password + "'");
It opens a new tab or window instead of setting the value of password field.
Output of document.querySelector('input[type=password]') (from console) :
<input type="password" name="userpassword" id="userpassword" maxlength="6" style="width: 128px; height: 18px">
I tried executing the same javascript from console. It works!
I have also configured webviewclient:
mView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, " shouldOverrideUrlLoading :" + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d(TAG, "onPageStarted " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
}
});
loadUrl method, loads the provided URL in the WebView. What you have to do is execute the evaluateJavascript method.
webView.evaluateJavascript("document.querySelector('input[type=password]').value ='" + password + "'", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
/// This callback is called after a result is returned by the script.
}
});
As of Android Kitkat (4.4, API level 19), you can use evaluateJavascript.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("js_code", null);
} else {
webView.loadUrl("javascript:js_code");
}
You are forgetting quotes on your CSS selector.
It should be:
document.querySelector('input[type="password"]');
See jsfiddle
I have used below code to avoid white space when my webview expands
it is working well in 5.0
I have added suppresslint #SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" }) to avoid error
But still white space remains in Android 4.2,4.3 devices
can someone suggest me to solve this issue.
web_content_url.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
pbar2.setVisibility(view.VISIBLE);
view.loadUrl(s[1]);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
pbar2.setVisibility(view.GONE);
view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
pbar2.setVisibility(view.VISIBLE);
}
});
web_content_url.addJavascriptInterface(this, "MyApp");
I'm developing one application in which a URL is loaded in webview of the app. Through JavaScript I'm slicing out the header and footer of the webpage and showing content in the webview. My problem is that, it takes some time to slice out the header and footer of the webpage, for about 1-2 secs the header is visible and hide that immediately. I'm attaching my WebViewClient class :
private class CustomWebClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
doGet(url);
return true; // super.shouldOverrideUrlLoading(view, url);
}
public void onLoadResource(WebView view, String url) {
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('header')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('footer')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('section').search_again.style.display = 'none'; "
+ "})()");
}
}
On onLoadResource() method I'm injecting JS.
Anyone has got some idea about the problem I'm facing, please help me in overcoming this problem.
You can use a css file with media queries for android to hide the header/footer over css