Android : Javascript not working properly in my WebView implementation

on Sunday, March 22, 2015


I'm trying to solve a problem with javascript and Disqus, I had to implement my WebView and all I do is just download the html from the url along with some string replaces



html = html.replaceFirst("<div", "<div id=\"headerApp\"></div><div");
html = html.replace("<head>", "<head> <style>"+cssContent +"</style>");
html = html.replace("class='hidden-phone'"," ");
html = html.replace("class=\"btn-mobile-pager visible-phone\"","class=\"btn-mobile-pager hidden-phone\"");


and then call



loadDataWithBaseURL("blarg://ignored", html, "text/html", "utf-8", "");


But the disqus comments are strange and look like this :


enter image description here


I believe this is some sort of javascript problem, here's what I do :



WebSettings settings = myWebView.getSettings();
settings.setSupportZoom(false);
settings.setBuiltInZoomControls(false);
settings.setLoadsImagesAutomatically(true);

myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {

MyWebView myView = null;

if(viewx instanceof MyWebView) {
myView = (MyWebView) viewx;


if(urlx.contains("https://twitter") || urlx.contains("action=em_fb_edit"))
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
startActivity(browserIntent);
myView.stopLoading();
return true;
}
else if (!urlx.startsWith("http://www.efficacemente")) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
startActivity(browserIntent);
return false;
} else if (urlx.contains("@facebook")) {
//Fai l'intent a facebook se puoi
if (isAppInstalled("com.facebook.katana")) {
String uri = "fb://page/101189567644";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
return false;
} else {
String url = "http://www.facebook.com/101189567644";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return false;
}
} else if (urlx.contains("@twitter")) {
if (isAppInstalled("com.twitter.android")) {
String uri = "twitter://user?screen_name=EfficaceMente";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
return false;
} else {
String url = "http://www.twitter.com/Efficacemente";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return false;
}

} else if (urlx.contains("@bookclub")) {
if (isAppInstalled("com.facebook.katana")) {
String uri = "fb://page/256384041207653";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
return false;
} else {
String url = "http://www.facebook.com/256384041207653";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return false;
}
} else {
myView.myLoad(urlx);
return true;
}
}
else
return false;

}
});
myWebView.clearCache(true);
myWebView.clearHistory();
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);


Where myWebView is an instance of MyWebView class that I created, where I simply download the html to the html and load it with loadDataWithBaseURL


Any hints? Thanks!


0 comments:

Post a Comment