title: Webview JavaScript loads inconsistantly
body: <p>I'm working on an app that takes a webpage and injects JavaScript to format the website for mobile. The code works fine on both my Nexus 5 and Nexus 7. However on my Moto G and LG G2 it does not inject consistently. Sometimes the pages load correctly with the JavaScript changing the look of the website. Other times the page is loaded without any changes being made. I have been looking for over a week for the cause of this but have found nothing. The code I am using is below.</p>
  
  <p>This is a sample fragment.</p>
  
  <pre><code>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 &amp;&amp; !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;
          }
      }
  
  }
  </code></pre>
  
  <p>This is the class that handles all the webview settings</p>
  
  <pre><code>    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 &lt; img.length; ++i) {img[i].style.maxWidth='100%';} "
                   + "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i &lt; img.length; ++i) {img[i].style.height='';} "
                   + "javascript:var img = document.getElementsByTagName('table'); for (var i = 0; i &lt; 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;
      }
  
  }
  </code></pre>