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.
Related
I want execute JavaScript in my WebView for remove a element in html.
It's my class :
private void WebViewProfil (){
String url = "http://google.com/XXX";
final WebView webview = (WebView) findViewById(R.id.webview52);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url){
String javaScript ="javascript:document.getElementsByClassName('navbar navbar-inverse navbar-fixed-top')[0].style.display='none'";
webview.loadUrl(javaScript);
Log.d("[WV]", " JS OK");
}
});
webview.loadUrl(url);
Log.d("[WV]", " WEB OK");
}
Webview is loading correctly but when the JS code is execute, i see "none" in my WebView.
Why that's not remove the element from webpage ?
Thank's
String javaScript ="javascript:document.getElementsByClassName('navbar navbar-inverse navbar-fixed-top')[0].style.display='none';void(0);";
You need to add void(0) at the end of the script
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");
} //
Suppose I load a 3rd party URL through webview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyWebChromeClient());
webview.loadUrl("http://ebay.com");
}
Is it possible for me to inject something into this WebView to replace the ebay logo with my own?
To expand on CommonsWare's correct answer:
WebView webview = new WebView();
webview.setWebViewClient(new WebClient());
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("stackoverflow.com");
then in WebClient:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// Obvious next step is: document.forms[0].submit()
view.loadUrl("javascript:document.forms[0].q.value='[android]'");
}
}
In a nutshell, you wait for the page to load. Then you loadUrl("javascript:[your javascript here]").
Not directly. You can invoke Javascript code in the context of the current Web page, via loadUrl(), much like a bookmarklet does. However, you do not have direct access to the DOM from Java code.
Since API level 19 there is a handy function evaluateJavascript (String script, ValueCallback<String> resultCallback) that to me makes more sense to use (but I guess the result will be the same). If you just want to run som javascript in the WebView (to modify the DOM for example) the resultCallback can be left empty.
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
webView.evaluateJavascript(
"const images = document.getElementsByTagName(\"img\");\n" +
"for(var i=0; i<images.length; i++){\n" +
" if(images[i].alt == \"eBay Logo\"){\n" +
" images[i].src = \"my_logo.png\";\n" +
" }\n" +
"}", null);
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://ebay.com");
WebView webview = new WebView();
//Maybe need to set to enabled javascript?
webView.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebClient());
webview.loadUrl("stackoverflow.com");