So, i need to detect if in a webview is loaded a reCaptcha or not.
I don't know if i should do this with some java code or with a javascript injection. Here's my code:
webView = findViewById(R.id.webview);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(paypal_url);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
loadJs(view, js_1);
}
}
A simple Toast with a message like "reCaptcha loaded" would be great.
Using Android Studio I am trying to set the value of a textbox in a login form of a web page which is not mine.
Here's the webView i use to work on the page:
final WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript: document.getElementById('j_password').value = 'something'");
}
});
webView.loadUrl("http://sg18224.scuolanext.info/");
the problem is that when the page has finished loading instead of setting the textbox value, the whole page becomes white and it only displays the string i was trying to set as value (in this case "something").
If i include the js line in an alert like this:
webView.loadUrl("javascript: alert(document.getElementById('j_password').value = 'something')");
it works! But also displays an alert i don't want to display.
This should help you:
final String js = "javascript:document.getElementById('j_password').value='something';";
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (Build.VERSION.SDK_INT >= 19) {
view.evaluateJavascript(js, new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) { }
});
} else {
view.loadUrl(js);
}
}
});
I am not sure with this answer but you may try it. typically it should be the answer. Long time no interaction with javascript though.
you may try
webView.loadUrl("javascript: document.getElementById('j_password').innerHTML = 'something'");
please comment if its not working.
I am developing an Android app and want to fill a form in a WebView automatically using JavaScript.
final WebView webView = (WebView) findViewById(R.id.MyWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:document.getElementById('tbBenutzer').value = 'myUser';");
webView.loadUrl("javascript:document.getElementById('tbKennwort').value = 'myPwd';");
webView.loadUrl("javascript:document.getElementById('form1').submit();");
}
});
webView.loadUrl(my_url);
The website loads fine and I can see it for a short time, but instead of filling the Input, it replaces the whole content of the WebView with my username. There is nothing else left.
I don't know why, but after some research I found out, that if I put all of my JavaScript code into an function it works.
final WebView webView = (WebView) findViewById(R.id.MyWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() {document.getElementById('tbBenutzer').value='myUser';}) ();");
webView.loadUrl("javascript:(function() {document.getElementById('tbKennwort').value='myPwd';}) ();");
webView.loadUrl("javascript:(function() {document.getElementById('form1').submit();}) ();");
}
});
webView.loadUrl(my_url);
I am using simple webview.Where i have given link to login page of my web application.
The webview work fine until i try custom webviewClient. By adding webviewClient it stops processing of javascript.I am able to see only html part.
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my web application login page with jscript");
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I am able to login successfully.There is javascript in login page,so i think it works.But after logging in it redirects to home page where onpage load javascript will append some data to html table.The data which is appended by jscript is visible in other browser,but webview not showing that data.
Try the below that enables javascript
public class WebViewDemo extends Activity {
private static final String URL_TO_LOAD = "http://google.com";
private static final String LOCAL_RESOURCE = "file:///android_asset/html/HelloWorld.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_demo);
WebView view= (WebView) findViewById(R.id.browser1);
loadResource(view, LOCAL_RESOURCE);
}
private void loadResource(WebView view, String resource) { wv.loadUrl(resource);
view.getSettings().setJavaScriptEnabled(true);
view.setWebChromeClient(new CustomChromeclient());
view.setWebViewClient(new CustomWebViewclient(this));
view.addJavascriptInterface(new JavaScriptInterface(view), "JSI");
} //
In my application I am using WebView and in that I am using JavaScript alert( ) method but its not working, no pop-up appears.
in my manifest file I have added
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
and in activity file I have added
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/demo.html");
In layout xml file I have added
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Any clue how to enable full JavaScript in WebView?
Update
Thanks mark
the alert() method in the html file are working now :) .
Now there are two issues in WebView :
1: I am using a <textarea> in the html file that i am loading in WebView , and trying to write in Hindi language font in it, but when i try to write Hindi text it displays as symbols ( rectangle symbols like [] ) .
when i do the same in firefox browser on desktop it works fine.
any clue how to give support for multiple language in textarea in WebView ?
2: When I am clicking submit and trying to open the value of text in alert() method in another java script it doesn't work , does it mean even after using WebChromeClient
its applicable only for current loaded html page and not javascripts called from that page ?
As others indicated, setting the WebChromeClient is needed to get alert() to work. It's sufficient to just set the default WebChromeClient():
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
Thanks for all the comments below. Including John Smith's who indicated that you needed to enable JavaScript.
Check this link , and last comment , You have to use WebChromeClient for your purpose.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
The following code will work:
private WebView mWebView;
final Activity activity = this;
// private Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
mWebView.loadUrl("file:///android_asset/raw/NewFile1.html");
}
You can try with this, it worked for me
WebView wb_previewSurvey=new WebView(this);
wb_previewSurvey.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
if you wish to hide URL from the user, Show an AlertDialog as below.
myWebView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(TAG, "onJsAlert url: " + url + "; message: " + message);
AlertDialog.Builder builder = new AlertDialog.Builder(
mContext);
builder.setMessage(message)
.setNeutralButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
}).show();
result.cancel();
return true;
}
}