webview loads webpage, but some contents get vanished after loading (Android Studio) - javascript

I tried to view http://artikelweb.com in a webview. The web page appears nicely. But, whenever I go to any author link from "Popular Authors" section, the web page appears, but, after loading, the quotes aren't showing.
In Google Chrome Browser(mobile) the quotes appear after loading,
but, in my app, the quotes are not showing in web view.
Code Snippet:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.artikelweb.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}

In your particular case you must enable the DOM Storage API
webSettings.setDomStorageEnabled(true);
So your code must become:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
myWebView.loadUrl("http://www.artikelweb.com");
myWebView.setWebViewClient(new WebViewClient());
}
That becouse the javascript that use the website you are accessing needs it in its javascript.
Of course you could take advantage of a third party library, but again you won't know why this exact case works in the third party one and not in the default webview.
The library you are using has this initial settings:
https://raw.githubusercontent.com/delight-im/Android-AdvancedWebView/master/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java
final WebSettings webSettings = getSettings();
webSettings.setAllowFileAccess(false);
setAllowAccessFromFileUrls(webSettings, false);
webSettings.setBuiltInZoomControls(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT < 18) {
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
}
webSettings.setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < 19) {
webSettings.setDatabasePath(databaseDir);
}
setMixedContentAllowed(webSettings, true);
setThirdPartyCookiesEnabled(true);
Enabling by default many options that the default webview has disable for security reason.

Set UserAgent to your Webview settings and try
webSettings.setUserAgentString("Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>");

Related

JavaScript in script tag doesn't work on Android 4.0.3 webview

JavaScript code in script tag doesn't work on Android 4.0.3 webview(It works on 4.4).
I wrote sample that shows webpage when clicking button by loadData, but alert in script tag doesn't work. I couldn't see any log about JavaScript.
// skip....
protected WebView view_webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// skip...
view_button = (Button)findViewById(R.id.button);
view_webview = (WebView)findViewById(R.id.webView);
WebSettings webSettings = view_webview.getSettings();
webSettings.setJavaScriptEnabled(true);
view_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// It doesn't work.
view_webview.loadData("<html><body><script>alert('hoge');</script></body></html>", "text/html; charset=utf-8", "utf-8");
}
});
}
Could you tell me any suggestion?

How can I fill out a form using Javascript on Android?

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.

Injecting JavaScript In WebView Android

I have the following WebView script :
This script load JavaScript and website url, when the application detect the phrase access_token it should redirect to another webpage with the data from access_token
for example:
the home page loaded
the user visted http://example2.com/?access_token=XXX
the app check the url and redirect user to http://example3.com/?token=XXX
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
String javascriptCodeUrl= "javascript:var token = \n"+
"top.location.href.split('access_token=')[1]; \n"+
"if (token) { \n"+
"top.location.href = 'http://example.com/?user=' + token;}";
mWebView.loadUrl(javascriptCodeUrl);
mWebView.loadUrl("http://default-webpage.com");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
}
You monitor the URLs in shouldOverrideURL method. There is no need to use Javascript injections. It can be done in Java.

android webview javascript not working

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");
} //

Android - YouTube AdSense does not render on an Android WebView

I have a WebView in Android, and most things render fine. My Youtube videos render fine, but the ads on them do not show up. I checked and JavaScript is enabled. So I am not sure why the ads do not render.
Here is the code that I use:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
WebSettings webSettings = webview.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webview.setWebChromeClient(new WebChromeClient(){});
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://javatester.org/javascript.html");
}
Would anyone know why the AdSense ads do not render, and how I can make it render?
Thanks!

Categories