我正在开发一个应用程序,该应用程序需要一个网页并注入JavaScript来格式化移动网站。该代码在我的Nexus 5和Nexus 7上运行良好。但是在我的Moto G和LG G2上,它不会持续注入。有时页面加载正确,JavaScript改变了网站的外观。其他时候加载页面而不进行任何更改。我一直在寻找一周以上的原因,但一无所获。我正在使用的代码如下。
这是一个样本片段。
public class Summary extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View myFragmentView = inflater.inflate(R.layout.summary, container, false);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "FRC Manual", "Loading");
final WebView webView = (WebView) myFragmentView.findViewById(R.id.webview1);
String url = "http://google.com";
Functions.webViewSettings(webView);
Functions.zoom(webView);
Functions.cache(webView, getActivity());
webView.setWebViewClient(new MyWebViewClient() {
boolean loadingFinished = true;
boolean redirect = false;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap facicon) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
@Override
public void onPageFinished(WebView view, String url)
{
if(!redirect) {
loadingFinished = true;
Functions.javascript(webView, url);
Functions.javascript(webView, url);
Functions.javascript(webView, url);
pd.dismiss();
}
if(loadingFinished && !redirect) {
Functions.javascript(webView, url);
Functions.javascript(webView, url);
Functions.javascript(webView, url);
pd.dismiss();
}
else {
redirect = false;
Functions.javascript(webView, url);
Functions.javascript(webView, url);
Functions.javascript(webView, url);
pd.dismiss();
}
super.onPageFinished(webView, url);
}
});
webView.loadUrl(url);
return myFragmentView;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
这是处理所有webview设置的类
public class Functions {
public static void javascript(WebView view, String url) {
view.loadUrl("javascript:var con = document.getElementById('header_container'); "
+ "con.style.display = 'none'; javascript:var con = document.getElementById('footer'); "
+ "con.style.display = 'none'; javascript:var con = document.getElementsByClassName('ChapterTools RightPos')[0].style.visibility='hidden';"
+ "javascript:var con = document.getElementsByClassName('showhide_button1')[0].style.visibility='hidden';"
+ "javascript:var con = document.getElementsByClassName('colmid')[0].style.right='100%'; "
+ "javascript:var con = document.getElementsByClassName('colleft')[0].style.right='initial'; "
+ "javascript:var con = document.getElementsByClassName('col1')[0].style.width='98%'; "
+ "javascript:var con = document.getElementsByClassName('col1')[0].style.top='-50px'; "
+ "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.right='2%'; "
+ "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.width=''; "
+ "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i < img.length; ++i) {img[i].style.maxWidth='100%';} "
+ "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i < img.length; ++i) {img[i].style.height='';} "
+ "javascript:var img = document.getElementsByTagName('table'); for (var i = 0; i < img.length; ++i) {img[i].style.width='';} ");
}
public static void zoom(WebView view) {
if (MainActivity.getEnableZoom()==true)view.getSettings().setBuiltInZoomControls(true);
else view.getSettings().setBuiltInZoomControls(false);
}
public static void cache(WebView view, Activity test) {
if (!isNetworkAvailable(test))view.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );
else {
if(MainActivity.getUpdateCache()==true)view.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
else {
if(MainActivity.getEnableCache()==true)view.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );
else view.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
}
}
}
@SuppressWarnings("deprecation")
@SuppressLint({ "SetJavaScriptEnabled", "SdCardPath" })
public static void webViewSettings(WebView view) {
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.setVerticalScrollBarEnabled(false);
view.setHorizontalScrollBarEnabled(false);
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
view.getSettings().setRenderPriority(RenderPriority.HIGH);
view.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
view.getSettings().setAppCachePath("/data/data/de.app/cache");
view.getSettings().setAllowFileAccess( true );
view.getSettings().setAppCacheEnabled( false );
}
public static boolean isNetworkAvailable(Activity test) {
ConnectivityManager connectivityManager = (ConnectivityManager) test.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
答案 0 :(得分:0)
也许“旧的”webview类(Moto G和LG G2 =&gt; pre KitKat)在javascript注入方面存在一些问题。在你的nexus 5(KitKat)上运行一个新的更好的基于铬的webview。
也许您应该考虑使用JavascriptInterface:
webView.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
然后你可以用你的javascript代码调用java。
反过来说,我会严格地从java代码中分离js-code。因为您的本机代码中的一个错误或更改意味着您必须更新应用程序。但js-code中的错误或简单更改可以在服务器端解决而无需更新。
JavaScript的功能:
function foo() {
// do something
}
的java:
webView.loadUrl("javascript:foo()");
对于调试问题,我建议覆盖onConsoleMessage-method of the WebChromeClient