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.
Related
I am having the following problem:
My website i prompting me with this JavaScript Popup
JS Popup Form
I need to create this wrapper for the webSite and I need somehow to process this dialog.
I have tried using ChromeWebView Client but my website is not loading at all with it.
I tried adding both WebView and ChromeWebView Clients to my WebView but it is still not working.
Here is some of my code:
String text = getIntent().getStringExtra("currentUrl");
this.myWebView.loadUrl(text);
WebViewClient webViewClientProceedSSL = new ProceedSSLClient(myWebView);
this.myWebView.setWebViewClient(webViewClientProceedSSL);
this.myWebView.setWebChromeClient(new WebChromeClient());
Do you have any ideas how can i fix that?
Edit 1:
I have forgotten to add my WebView Settings. Here are they:
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setGeolocationEnabled(true);
webSettings.setDomStorageEnabled(true);
Edit 2:
I have tried overriding both methods onJsAlert and onJsPrompt in the WebChromeClient class when setting it to the webView. This is not working as well...
myWebView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
I am not getting in that method at All - I tried logging something in it and debugging it. I never got in.
Add the following to the Webview
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
Also try adding this method in WebViewClient
webview.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);
}
});
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);
So, reading around i read that i could "inject" javascript into a loaded webpage to programatically fill a website's form, so i tried the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WebView;
WebView = (WebView) findViewById(R.id.webview);
WebView.setWebViewClient(new HelloWebViewClient());
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setBlockNetworkImage(false);
WebView.loadUrl("http://www.kingsage.es");
String username = "XXX";
String Password = "YYY";
WebView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';");
}
However, when i run this on the emulator i get an error as if the website could not be found. if i turn off the second loadUrl method i load the website, but i need to fill the login form programatically!, i figure i am doing something wrong but i haven't found and answer reading around. Any help would be appreciated!!
you need to wait until the webpage is loaded (use WebViewClient) then execute your javascript
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished (WebView webView, String url)
{
webView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';");
}
});
make also sure that you execute only once, not on every onPageFinished Event
I like JeanLuc's suggestion and it worked for me 98% of the way. I still needed to automatically log the user into the web page once their username and password had been entered so I did the following and its worked well, except for trying to remove the "do you want to remember the password' message.
#Override
public void onPageFinished (WebView webView, String url)
{
webView.loadUrl("javascript:document.getElementById('Login1_UserName').value='"+userName+"';javascript:document.getElementById('Login1_Password').value = '"+password+"';");
webView.loadUrl("javascript:document.getElementById('Login1_LoginButton').click();");
}
The second line selected the 'login' button and logged them in automatically. Hope it helps someone else.
Here is how I acheived it in my scenario where I had to browse a page, fill text boxes and submit.
WebView webView = FindViewById<WebView>(Resource.Id.webEspView);
webView.SetWebViewClient(new Client());
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl("http://192.168.4.1")
Then added the Client class and overloaded onPageFinished() method.
public class Client : WebViewClient
{
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
string ssid = Variables.GetInitialUsername();
ssid = ssid.Trim(new char[] {'\"'});
string pass = Variables.GetInitialPassword();
var script = $"document.getElementById(\"clientId\").value = \"{ssid}\";" +
$"document.getElementById(\"branchId\").value = \"{pass}\";" +
"document.querySelector('button[type=\"submit\"]').click()";
view.LoadUrl($"javascript: {script}");
}
}
Hope it helps.
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;
}
}
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");