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");
} //
Related
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.
I've got a website (www.myexamplewebsite.com) that looks simplified like this:
<html>
<head>
</head>
<body>
<img src="img/speaker-icon.png" onclick="speakerFunction()">
<script>
function speakerFunction() {
var text = new SpeechSynthesisUtterance("This is a test.");
text.lang = 'en-US';
window.speechSynthesis.speak(text);
}
</script>
</body>
</html>
And I have also a simple Android-application which is basically a webview that displays my website:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("www.myexamplewebsite.com");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
// Use when the user clicks a link from a web page in the WebView
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.myexamplewebsite.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
My problem is the following:
When I visit the example website in a normal browser (Chrome, Firefox) on my desktop-pc it works how it should work. If I click on the image on the website, I can hear the text "This is a test."
If I visit the example website with the Chrome-App for Android on my smartphone it works also. But if I visit the website with the Firefox-App or within my own App with the webview (see above) then it doesn't work.
Update 1:
I specified the language explicitely and I tried it with the crosswalk framework but that also didn't solve the problem.
SpeechSynthesisUtterance is not supported by android webview
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance
Although SpeechSynthesis is supported but since SpeechSynthesis work based on the SpeechSynthesisUtterance object passed into it, it won't all still work.
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis
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);
Alright, so I'm working on an app that will fill out forms. I have no idea how to do this, other than the fact that I need to use Javascript for this. This code loads the web page, but does not input any values to the form.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
WebView myWebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
WebView myWebview = (WebView) findViewById(R.id.webview);
//hide loading image
findViewById(R.id.progress).setVisibility(View.GONE);
//show WebView
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
});
myWebview.loadUrl("https://www.panerabread.com/en-us/mypanera/registration-page.html");
myWebview.loadUrl("javascript:document.getElementById(\"join_first_name\").value=\"Hello World\";");
}
Below is my second class. Not sure what this does, I got it from the documentation though.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
}
Try putting the loadUrl("javascript..." into the onPageFinished listener. You're calling it before the page is loaded.
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");