I'm making some sort of book reader in webView. I have used the JavaScript, which dynamically creates the <img> tags via for loop. Look at the code's for loop, and every img tag is loaded with a different image from the URL.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
progressDialog = ProgressDialog.show(MainActivity.this,
"Loading Book...!", "Please Wait");
String htnlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><script>var out = '';for (var counter = 1; counter <= 100; counter++){ out += '<img src=\"http://shiaislamicbooks.com/books_snaps/UR335/'+counter+'.jpg\"alt=\"Page No:'+counter+'\" width=\"100%\" />';}document.write(out);</script></body></html>";
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Completed",
Toast.LENGTH_SHORT).show();
super.onPageFinished(view, url);
}
});
wv.loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);
}
Look at the htnlString:
Now I want to display a book pages information in a textView. This means, while scrolling the webview the textView should update the txtPage.
the scrollTo(x,y) is some how useful but I want the scroll listener for the webview.
WebView webview;
yPos = webview.getScrollY();
xPos=webview.getScrollX();
Related
I want to get all text in body tag of a url but it doesn't work. I have searched many but i could not find anything. I have also added android.permission.INTERNET also.
so what is the problem?
This is my code:
public class Activity_Main extends Activity {
#SuppressLint("JavascriptInterface")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
final TextView contentView = (TextView) findViewById(R.id.contentView);
class MyJavaScriptInterface
{
private TextView contentView;
public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}
#SuppressWarnings("unused")
public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
});
webView.loadUrl("https://stackoverflow.com");
}
}
update #JavascriptInterface for your interface function and check
class MyJavaScriptInterface
{
private TextView contentView;
public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}
#JavascriptInterface
public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}
and there is another way to do this is this for above KITKAT
webView.evaluateJavascript("alert('pass here some ...')", new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
}
});
this is update solution for executing js in android
full code is here
final WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
final TextView contentView = (TextView) findViewById(R.id.contentView);
class MyJavaScriptInterface
{
private TextView contentView;
public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}
#JavascriptInterface
public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view,url);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("document.getElementsByTagName('body')[0].innerText", new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
contentView.setText(s);
}
});
}
else {
webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText)");
}
}
});
webView.loadUrl("https://stackoverflow.com");
To call a JavaScript function from Android you don't need to add interface prefix:
webView.loadUrl("javascript:processContent(document.getElementsByTagName('body')[0].innerText);");
Note: You must implement the called javascript function in your HTML page. So If you are calling a third party website in your webview, you have only access to the current existing functions in that page.
e.g https://stackoverflow.com has no javascript function named INTERFACE.processContent!
On the contrary when you want to call and Android method from javascript then you need that prefix:
<script>INTERFACE.myAndroidMethod()</script>
Finally if you want to proccess the HTML content of a thirdparty webpage you can not extract the content using a Webview and JavaScript function (that not exists in that page) but you need Android codes to load the webpage content without the WebView mediation.
I am trying to get the src of a captcha image found in a webview but when i type the below code the output says:
Uncaught TypeError: Cannot read property 'src' of null
and another question if i get the image src how can i put the image in an image view?
any help will be really appreciated
here is my code so far:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.wv) ;
mImgCaptcha = (ImageView) findViewById(R.id.imgCaptcha);
done = (Button) findViewById(R.id.done);
contentView = (TextView) findViewById(R.id.textView);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.password);
code = (EditText) findViewById(R.id.code);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.getSettings().setDomStorageEnabled(true);
wv.loadUrl("https://noor.moe.gov.sa/NOOR/Login.aspx");
wv.loadUrl("javascript:var a = document.getElementById('imgCaptcha').src;");
System.out.println(wv.getUrl());
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
wv.loadUrl("javascript:var x = document.getElementById('tbPublic').value = '" + username.getText().toString() + "';");
wv.loadUrl("javascript:var x = document.getElementById('tbPrivate').value = '" + password.getText().toString() + "';");
wv.loadUrl("javascript:var x = document.getElementById('tbCaptcha').value = '" + code.getText().toString() + "';");
try {
Thread.sleep(2000);
wv.loadUrl("javascript:(function(){" +
"l=document.getElementById('btnLogin');" +
"e=document.createEvent('HTMLEvents');" +
"e.initEvent('click',true,true);" +
" l.dispatchEvent(e);" +
"})()");
} catch (InterruptedException e) {
e.printStackTrace();
}
It's because the page is not loaded yet when you are trying to query the DOM. You need to query the DOM when you can be sure that the page has loaded.
We need to set the WebViewClient for the webview and then listen to onPageFinished event. The code might look something like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// you can now query the DOM.
webView.loadUrl("javascript:var a = document.getElementById('imgCaptcha').src;");
}
});
Further reading: https://developer.android.com/reference/android/webkit/WebViewClient#onPageFinished(android.webkit.WebView,%2520java.lang.String)
Following is the code which is I am implementing:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.webTask);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSaveFormData(false);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
else webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("javascript:document.getElementById('user_id').value='" + new String ("xxx")+ "';javascript:document.getElementById('password').value = '" + new String("xxx") + "';");
}
});
webView.loadUrl(URL);
But nothing happens infact web view turns blank and shows text written on its left corner "xxx"
Please help. already searched 3 to 4 hours and of no avail
At last i have solved the problem. the javascript was not being run because my targeted sdk was >= KITKAT.
So, in order to avoid the problem you have to use webView.evaluateJavascript(yourScript,null); for devices running on API level 19 or above.
Sample Code
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.evaluateJavascript(yourScript,null);
}
else
{
webView.loadUrl(yourScript);
}
I am trying to use JavaScript in an Android webview. I used following code. I did not get color changed or offset height of body.
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new myJavaScriptInterface(webview), "jsInterface");
webview.loadUrl("file:///android_asset/sample/contents.html");
//webview.loadData("file:///android_asset/sample",convertStreamToString(inputStream), "text/html", "UTF-8");
webview.loadUrl("javascript:" +
"document.getElementsByTagName('p')[0].style.color='red';" +
"");
wbview.loadUrl("javascript:window.jsInterface.EchoText(document.body.offsetHeight);");
//webview.
}
class myJavaScriptInterface
{
WebView mywebview;
public myJavaScriptInterface(WebView webview)
{
this.mywebview=webview;
}
public void EchoText(String message)
{
Toast.makeText(mywebview.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
What am I missing?
I think this is because your page is not loaded (still loading) when you are using webview.loadUrl("javascript:" + ...) statements.
Try the following...
webview.loadUrl("file:///android_asset/sample/contents.html");
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView webview, String url) {
webview.loadUrl("javascript:" +
"document.getElementsByTagName('p')[0].style.color='red';");
wbview.loadUrl("javascript:" +
"window.jsInterface.EchoText(document.body.offsetHeight);");
}
});
With reference to this WebView tutorial, in particular this method
private void setupWebView(){
String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + ","+ mostRecentLocation.getLongitude()+ ")";
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url){
webView.loadUrl(centerURL);
}
});
webView.loadUrl(MAP_URL);
}
I've noticed that if I place the webView.loadUrl(centerURL); directly after
webView.loadUrl(MAP_URL); like this
private void setupWebView(){
String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + "," + mostRecentLocation.getLongitude()+ ")";
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url){
//DO NOTHING
}
});
webView.loadUrl(MAP_URL);
webView.loadUrl(centerURL);
}
it no longer works. So the centreAt(..) javascript method is contained int the MAP_URL.
I'm wondering if the webView.loadUrl(..) method returns before the url has actually been loaded.
It looks that way since the top method waits for it to load fully before running the javascript
Yes, webView.loadUrl() is asynchronous: it returns immediately and WebView keeps working in it's own thread.
To monitor WebView page loading use WebViewClient.onPageFinished(..):
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do something here
}
});