android webView autofill username and password not working - android

I am passing username and password to the activity with webView. The problem is url loading is successful but i want to fill(autofill) username and password. I am a beginner, please help or give me any suitable github link.
WebSettings webSettings = superWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
superWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById('username').value = '"+userName+"';" +
"document.getElementById('password').value='"+password+"';");
}
});
superWebView.loadUrl(url);

use this webview settings
htmlWebView.getSettings().setAppCacheEnabled(true);
htmlWebView.getSettings().setAppCachePath("/data/data" + getPackageName() + "/cache");
htmlWebView.getSettings().setSaveFormData(true);
htmlWebView.getSettings().setDatabaseEnabled(true);
htmlWebView.getSettings().setDomStorageEnabled(true);
CookieManager.getInstance().acceptCookie();
replace htmlWebview With yourown webview

call javascript as a function instead of this.
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: (function() {document.getElementById('username').value= '"+userName+"'; document.getElementById('password').value='"+password+"'; }) ();" );
}
Though the form must have "value" attribute.

Related

view-source of a web link in android

I trying to load "view-source:http://goo.gl/lEYQQf" link in webview. If I load this link in google chrome from PC, the link changes into this "view-source:https://www.facebook.com/connect/login_success.html#access_token=CAAAACZAVC6ygBAKYZBIixFdEDGOHMUhTMxoBcN4pCaPu137s02iHtD0kgKUlW59cU7qAuLgsIXZBOvJ8OLevA3zArPx8v2TkUxbG493Bq7hMiHKHYZC66cbEvQsDBzhAbZCVIx8jOGUc4k3ynW4f65cUZB6m5IZA5tWrnQhnBcGUHMjKtmLZBpsmBzxOZA9ZCwmFgZD&expires_in=0"
I need this access token. I have tried this code:
wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
Log.e("testing", url);
}
});
wv.loadUrl("view-source:http://goo.gl/lEYQQf");
This is not loading the url. When I remove the "view-source:" part from the given url, in output it is giving me an another link which does not contains the access token. I have tried two more ways but I am not able to get that token. How can I get it?

Android, How to retrieve redirect URL from a WebView?

First i surfed the web but no answer was found matching my case.
I want to grab a specific redirect url from a webview and then keep the webview from opening it. The case is an Instagram redirect url after the user authorized my app. How to do it? Here is my code:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://api.instagram.com/oauth/authorize/?client_id=my_client_id&redirect_uri=my_redirect_uri&response_type=code");
You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.
mWebView.setWebViewClient(new WebViewClient()
{
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
return shouldOverrideUrlLoading(url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
{
Uri uri = request.getUrl();
return shouldOverrideUrlLoading(uri.toString());
}
private boolean shouldOverrideUrlLoading(final String url)
{
Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
// Here put your code
return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
}
});
mWebView.loadUrl("Your URL");
Checkout the example code for handling redirect urls and open PDF without download, in webview.
https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500
You should override shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
if (url.indexOf('api.instagram.com') > -1) //check if that's a url you want to load internally
{
webView.loadUrl(url);
return true;
}
else
{
return false; //Let the system handle it
}
}
});
Finally thanks to the answer given to my question by Udi I, with a little change, i managed to find a solution to the problem. Here is the code which worked for me:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
return (url.indexOf("some part of my redirect uri") > -1);
}
});
webView.loadUrl(myUrl); //myUrl is the initial url.
Using the above code, if there will be any url containing redirect uri, webView won't load it. Else, webView will load it. Also thanks to Kamil Kaminski.
Simply set WebViewClient for your WebView, and override shouldOverrideUrlLoading in WebViewClient. In that method you can grab your url, and decide whether or not your WebView should follow redirection url. Here's example:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// here you can assign parameter "url" to some field in class
// return true if you want to block redirection, false otherwise
return true;
}
});
You can compare URL strings / particular word, and take actions accordingly -
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (url.contains(".pdf")
openPDF(url); // handle pdf opening
if(url.startsWith("tel:")
callNumber(url); // handle call processing
...
}
Just to add more, you can prevent webview from showing traditional error message screens, or bypass known errors -
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == -10) {
System.out.println("Error occured: Error code: "
+ errorCode + "..Ignoring this error");
return;
}
else
{
// Show your custom screen to notify error has occured
}
...
}

Detect a click of the search button in google Android

I have a webview that display the google page and i want to do something when the search button in the google page is pressed is it possible ?
here is my webview :
WebView wb=(WebView) findViewById(R.id.webView);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadsImagesAutomatically(true);
wb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wb.loadUrl("https://www.google.com");
You can use WebViewClient to listen for url changes. It doesn't exactly allow to listen for that specific button, but you can just check the url, like so:
WebView wv = (WebView) findViewById(R.id.webView);
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
WebViewClient wvc = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean isSearch = url.startsWith("https://www.google.com/search");
if (isSearch) {
Log.d("WebView", "search clicked");
return true;
}
return false;
}
};
wv.setWebViewClient(wvc);
wv.loadUrl("https://www.google.com");
I know this is late, but yes you can. You have to set up a JavaScript event listener as a url query to load on Android's side.
..
webView.addJavascriptInterface(new JSInterface(), "SearchClicked");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
String query =
"document.getElementsByClassName('BwoPOe').item(0).addEventListener('click',
function() {SearchClicked.doTasks()}, false);";
webView.loadUrl("javascript:" + query);
}
});
webView.loadUrl("https://images.google.com/");
..
Here when loading the WebView, you're getting the Google search button by getting a class name called BwoPOe, and setting it's event listener to call the JSInterface method doTasks.
You can find out the button class name using the Chrome's inspect element tool.
private class JSInterface {
#JavascriptInterface
public void boundMethod(String html) {
// do something...
}
}

Android WebView doesn't store the cookies

seems that Android WebView doesn't store the cookies, how do I enable them?
I used this code to test it:
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://www.w3schools.com/php/showphp.asp?filename=demo_cookie1");
After I reload (webView.reload()) the page I do see "Cookie 'user' is set!" but after I close the application and start it again I see "Cookie 'user' is NOT set!". Weirdly enough sometimes I do see it set when I first start the app. So what's going on here? Is there a delay when cookies are stored or am I missing something?
Thanks!
+1 for eXistenZ' answer, but now in 2020 CookieSyncManager is deprecated. Now you should use CookieManager.getInstance().flush() or write something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().flush();
} else {
CookieSyncManager.getInstance().sync();
}
}
});
Seems that there is a delay indeed with the cookies so I have to use this code:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
Toast.makeText(getApplicationContext(), "Page loading complete", Toast.LENGTH_LONG).show();
}
});
Now it works fine.

Remove sign in button in google docs webview in android

I am showing PDF files by using google docs in WebView in android.
How to remove or hide "Sign In" button? I have attached screenshot below. Thanks in advance.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?url=http://www.ex.com/terms.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Add the embedded=true parameter.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=https://orimi.com/pdf-test.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Note: This answer was actually proposed by user3777879. Tested, and working, he should get the credit - Stuart
Try this
I have tried to many answers but did not get good answer.
Finally got the solution with adding few code in when loading the pdf into the webview .
final WebView wv_webview= (WebView) view.findViewById(R.id.wv_webview);;
wv_webview.getSettings().setJavaScriptEnabled(true);
wv_webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
});
String your_pdf_link="https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf";
wv_webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=" + your_pdf_link);
Note:- It will show only few milliseconds when pdf loads into webview
Output:-
webview.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('drive-viewer-toolstrip')[0].style.visibility='hidden'; })()");
I'd self-host your terms document, and I'd host it as .html file format rather than .pdf.
If you do not have a domain in which to self-host the file, check other file hosting services and see if they offer a public option that won't request login. Potential services that may work for you include Mediafire, Cramitin, Hotfile, Rapidshare, etc. (this is not an ordered or comprehensive list, do your own search).

Categories

Resources